We have a lot of problems with our calculators, and we solve them.
This section addresses the issue of user exit when the error is lost. We should be in the user error, prompt and let it re-enter, if always wrong always prompt, until the loser. It is appropriate to use the While loop statement, as follows:
//Prompts the user to enter the first number, which needs to be looped until the user enters the correct number: intR while(1){printf("Please enter the first number: \ n"); R =scanf("%d", &number1);///See if the scanf received the correct number if(r==0){printf("Don't be naughty, it's not fun, I'm gone.");//clears the input stream. Without this sentence, the remaining characters in the input stream will cause scanf not to wait for user input. intC while((c = GetChar ())! =' \ n '&& c! = EOF); }Else{//Lose the right, jump out of the loop Break; }}
This code will cause the user to lose the wrong time, always loop, re-let the user input, until the loss to stop the loop.
Each time the loop body is executed (that is, the code inside the curly braces), it goes back to the while (1), re-checks the condition, and enters the loop body again if the condition is true. While (1) this line, the parentheses are the same as the IF statement, is the condition, if true, executes the code in curly braces. It is always the real, so always execute the code in the large expansion number, that is, always execute the loop body. The loop is not jumped out until the user loses the pair and executes a break. Note that you jump out of the loop directly from the break, and the code behind the break is no longer executed.
while ((c = GetChar ())! = ' \ n ' && c! = EOF); This sentence is to empty the input stream, if the user input is not a number, then in fact, scanf did not remove the word from the input stream, resulting in the next time with scanf fetch, directly return 0 instead of waiting for user input. This type of writing is cross-platform-generic, with fflush (stdin) empty words can only be used under Windows.
After you add the processing of the second number and operator, themain function is this:
intMainvoid){//Save the number of user input intNumber1;intnumber2;intOpt//Operator //Prompts the user to enter the first number, which needs to be looped until the user enters the correct number: intR while(1){printf("Please enter the first number: \ n"); R =scanf("%d", &number1);///See if the scanf received the correct number if(r==0){printf("Don't be naughty, it's not good to play, again \");//clears the input stream. Without this sentence, the remaining characters in the input stream will cause scanf not to wait for user input. intC while((c = GetChar ())! =' \ n '&& c! = EOF); }Else{//Lose the right, jump out of the loop Break; } }//Prompts the user to enter a second number while(1){printf("Please enter a second number: \ n"); R =scanf("%d", &number2);///See if the scanf received the correct number if(r==0){printf("Don't be naughty, it's not good to play, again \");//clears the input stream. Without this sentence, the remaining characters in the input stream will cause scanf not to wait for user input. intC while((c = GetChar ())! =' \ n '&& c! = EOF); }Else{//Lose the right, jump out of the loop Break; } }intResult//Store calculation results //Prompt user input operator while(1){printf("Please enter operator (%d%d%d%d corresponds to subtraction): \ n", Jia,jian,cheng,chu); R =scanf("%d", &opt);if(r==0){printf("Operators are numbers too, don't be mistaken, okay? How do you make me do this? Re-enter it \ n ");//clears the input stream. Without this sentence, the remaining characters in the input stream will cause scanf not to wait for user input. intC while((c = GetChar ())! =' \ n '&& c! = EOF);//back to loop start Continue; }//follow-up operator for different operations Switch(opt) { CaseJia//Plusresult = Number1+number2; Break; CaseJian//Minusresult = Number1-number2; Break; CaseCheng//Multiplyresult = Number1*number2; Break; CaseChu//Except, now only divisibleresult = Number1/number2; Break;default:printf("operator must be one of the 1,2,3,4!" Again \ n ");Continue;//Go back to the loop and start over again}//When executing here, the operator check succeeds and should jump out while. Break; }//Output Results printf("Result:%d\n", Number1,number2,result);return 0;}
Can see a new thing:continue , its function is to jump back to the beginning of the loop, that is, while (1) this line. Re-judge the conditional statement, if true, re-enter the execution loop body.
These are the 6th editions of our calculator program.
Previous: Become a C + + Master of the macro and enumeration
Become a C + + master while loop