Home > Back-end >  Why I goto statement does not perform well
Why I goto statement does not perform well

Time:10-13

#include
Int main ()
{
Double num1, num2;
Char character, the command;
A double value.
Lable:
Printf (" please input the first number: \ n ");
Lf the scanf (" % ", & amp; Num1);
Printf (" please enter the second number: \ n ");
Lf the scanf (" % ", & amp; Num2);
Printf (" please enter the symbol \ n ");
The fflush (stdin);
The scanf (" % c ", & amp; Character).
The switch (character)

{
Case: '+'
value=https://bbs.csdn.net/topics/num1+num2;
Printf (" two type combined results as follows: % lf \ n ", value);
break;
In case the '-' :
value=https://bbs.csdn.net/topics/num1-num2;
Printf (" subtraction results for the two type: % lf \ n ", value);
break;
Case: '*'
Num2 value=https://bbs.csdn.net/topics/num1 *;
Printf (" multiplication results for the two type: % lf \ n ", value);
break;
Case '/' :

value=https://bbs.csdn.net/topics/num1/num2;
Printf (" % to the result of the two type floor lf \ n ", value);



break;

}
Printf (" still need to continue operation, please input below yes/no \ n ");
The fflush (stdin);
The scanf (" % c ", & amp; The command);

If (the command=='yes')
{
The fflush (stdin);

Goto lable.

}
The else
Printf (" please close the program \ n ");



}

CodePudding user response:

Because of failure, the command took the 'y', only the scanf (" % c ", & amp; The command);

CodePudding user response:

The fflush (stdin); Not for all compilers are effective, because the compiler that the function operations stdin is undefined behavior,

Suggest using getchar (); Replace the fflush ();

Did not perform goto because execute goto statement did not meet the conditions of, probably fflush (stdin) didn't work, suggest using getchar ();

In addition, the command is char types, "yes" is not a string, this is a bug,

 # include & lt; stdio.h> 

Int main ()
{
Double num1, num2;
Char character, the command;
A double value.
Lable:
Printf (" please input the first number: \ n ");
Lf the scanf (" % ", & amp; Num1);
Printf (" please enter the second number: \ n ");
Lf the scanf (" % ", & amp; Num2);
Printf (" please enter the symbol \ n ");
//fflush (stdin);
getchar();//for '\ n'
The scanf (" % c ", & amp; Character).
getchar();//for '\ n'
The switch (character)
{
Case: '+'
value=https://bbs.csdn.net/topics/num1+num2;
Printf (" two type combined results as follows: % lf \ n ", value);
break;
In case the '-' :
value=https://bbs.csdn.net/topics/num1-num2;
Printf (" subtraction results for the two type: % lf \ n ", value);
break;
Case: '*'
Num2 value=https://bbs.csdn.net/topics/num1 *;
Printf (" multiplication results for the two type: % lf \ n ", value);
break;
Case '/' :

value=https://bbs.csdn.net/topics/num1/num2;
Printf (" % to the result of the two type floor lf \ n ", value);



break;

}
Printf (" still need to continue operation, please input below y/n \ n ");
The fflush (stdin);
The scanf (" % c ", & amp; The command);
getchar();//for '\ n'

//if the command=='yes')
If (the command=='y')
{
//fflush (stdin);

Goto lable.

}
The else
Printf (" please close the program \ n ");

return 0;
}


For your reference ~
  • Related