Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone! I'm the beginner in C. So i have the problem with the program to take two numbers and display the sum. Here is the code!

#include <stdio.h>
main()
{
      int a,b,sum;
      printf("\nType your frist number: ");
      scanf("%d" , &a);
      printf("\nType your second number: ");
      scanf("%d", &b);
      sum=a+b;
      printf("\n The sum of two number you have just type is %d", sum);
      printf("\nPress enter to continue");
}


I compile it successfully in Dev-C++. The problem is: when i run it and input 2 numbers. The program won't stop at the result line: "The sum of two number...." for me to view the result. It terminate immediately after that line show up.

I running Dev-C++ (version 4.9.9.2) on Windows 7 64bit.

Thank you very much and sorry for my bad English. (I'm from Vietnam, you know ^^)
Posted

Just add
getch();
after the last printf.
 
Share this answer
 
v3
Comments
Minh Hikari 7-Sep-12 8:19am    
Thank you very much!!! And...uh....Can you explain more clearly about "getch();" for me!!!
JF2015 7-Sep-12 8:21am    
Basically it just waits for you to press a key and therefore the program does not close. See here for some more information: http://www.programmingsimplified.com/c/conio.h/getch
Minh Hikari 7-Sep-12 8:25am    
ok! Like "Press any key to continue..." :D..Thank you very much!!! :D
Mihai Vrinceanu 7-Sep-12 16:35pm    
If you want to see "Press any key to continue...", add this line after printf:
system("pause");
Espen Harlinn 7-Sep-12 8:29am    
5'ed!
There is no code that let the program stop anywhere after you have entered the two numbers.

If you want to let it run for another calculation, use a while loop until a specific condition is met (both numbers null here):
C++
main()
{
    int a,b,sum;
    do
    {
        printf("\nType your frist number: ");
        scanf("%d" , &a);
        printf("\nType your second number: ");
        scanf("%d", &b);
        sum=a+b;
        printf("\n The sum of two number you have just type is %d", sum);
        printf("\nEnter null for both numbers to terminate");
    }
    while (a != 0 || b != 0);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900