2016-10-09 2 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; 
} 

내가 열 때 질문을하지만 번호를 입력하자마자 합계가 인쇄되지 않고 닫힙니다.내 프로그램이 인쇄되지 않고 숫자를 입력 한 직후 닫습니다. (C)

+0

이며, conio.h와 및 필요 포함 없습니다 여기에 야생 짐작, 당신은 [Windows에서 Visual Studio IDE에서 실행] (https://stackoverflow.com/questions/13505173/visual-studio-20)입니까? 10- 프로그램 종료 - 실행 직후)? – WhozCraig

+0

오, 내 실수, 내 질문은, 내가 내 합계를주기 전에 내가 원하는 번호를 입력 한 후 종결을 멈추게하려면 어떻게해야합니까? 그리고 Im은 Visual Studio 커뮤니티의 최신 버전을 사용하고 있습니다. –

+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을 구현하기 위해 다음과 같은 코드를 사용할 수 있습니다; 여기
단지, 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.

관련 문제