Greetings , to cut it short , below is the code
The Code
// This program would sum up all the integer provided by user
#include <stdio.h>
int main(void)
{
int sum = 0; // initialize and declare variable
int num;
char check;
int status;
printf("Please enter an integer to be summed :"); // prompt user for an integer
status = scanf("%d" , &num);
while(status == 1) //If user enter an integer , while loop test condition is true
{
sum = sum + num; //sum the input integer
printf("Do you wanna add more integer?(y/n) :"); //Asking for user next action
scanf("%c" , &check);
if(check == 'y') //Check user's answer
status = scanf("%d" , &num);
else
status = 0;
}
return 0;
}
The problem
The first thing I do when I run the program is to provide an integer , after that it would just print out the control-string Do you wanna add more integer?(y/n) :
.Then the problem arouse , instead of waiting for me to type y
or n
, the program would just end itself , which mean in Windows command prompt it would give me the line Press any key to continue....
.
I keep read through the code line by line to find any semantic error (assume there's no syntax error since compiler would've complained if there's one) or any logical error that I made , but to no avail.So I suspect is it there's any rules of C that mention We shouldn't prompt use if()
with character as test value when it is enclosed in while()
loop or any other thing that I've neglected??
Thanks for keeping yourself tied up to read my problem , hope I would really learn from you guys, do point out any mistake I made as I'm a learner who ready to accept any comment or teaching that is correct.