2016-10-09 8 views
0
#include <stdio.h> 

int main() 
{ 

    int input, i, sum = 0; 

    printf("Please enter a number between 1 and 5: "); 
    scanf_s("%d", &input); 

    for (i = 0; i < 5; i++) 
    { 
     sum += input + i; 
    } 

    printf("Sum = %d\n", sum); 

    return 0; 
} 

私はそれを開くと、質問をしますが、数字を入力するとすぐに、何も印刷せずに閉じます。私のプログラムは印刷されません。数字を入力した直後に閉じます。

+0

あなただけ、質問をするのを忘れて、ここで野生の推測、あなたは[これは、Visual Studio IDEからWindows上で実行している](https://stackoverflow.com/questions/13505173/visual-studio-20 10-プログラムクローズ - 実行直後)? – WhozCraig

+0

ああ、私の間違い、私の質問は何ですか、私はそれが私の合計を与える前に私が欲しい番号を入力した後にそれを閉じることができます。と私はビジュアルスタジオコミュニティの最新バージョンを使用しています。 –

+0

もしあなたがそうであれば、私の以前のコメントの**リンク**(https://stackoverflow.com/questions/13505173/visual-studio-2010-program-closes-immediately-after-running)が役に立つでしょう。ケース。 – WhozCraig

答えて

0

ctrl + F5を使用してプログラムを実行します。

0

TurboCコンパイラを使用している場合は、getch();を使用します。

#include<stdio.h> 
#include<conio.h> 

int main() 
{ 
    //your code 
    getch(); 
    return 0; 
} 

またはコンパイラGCCです - あなたは(自分のgetchはを実装するには、次のコードを使用することができます)。そして、ここでconio.h
を含める必要はGCC

#include <termios.h> 
#include <stdio.h> 

static struct termios old, new; 

/* Initialize new terminal i/o settings */ 
void initTermios(int echo) 
{ 
    tcgetattr(0, &old); //grab old terminal i/o settings 
    new = old; //make new settings same as old settings 
    new.c_lflag &= ~ICANON; //disable buffered i/o 
    new.c_lflag &= echo ? ECHO : ~ECHO; //set echo mode 
    tcsetattr(0, TCSANOW, &new); //apply terminal io settings 
} 

/* Restore old terminal i/o settings */ 
void resetTermios(void) 
{ 
    tcsetattr(0, TCSANOW, &old); 
} 

/* Read 1 character - echo defines echo mode */ 
char getch_(int echo) 
{ 
    char ch; 
    initTermios(echo); 
    ch = getchar(); 
    resetTermios(); 
    return ch; 
} 

/* 
Read 1 character without echo 
getch() function definition. 
*/ 
char getch(void) 
{ 
    return getch_(0); 
} 
int main(){/*your statements*/ getch(); return 0} 

のコードではありません参考 gotoxy(), clrscr(), getch() and getche() functions for GCC Linux.

関連する問題