Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to control integer variables not to receive characters in C language
How to control integer variables not to receive characters in C language
I modified it for you. Look at the bold part of jm () function.

# include & ltstdio.h & gt

# include & ltconio.h & gt

# include & ltprocess.h & gt// main interface

int jm()

{

int x; Printf ("This is a simple calculator, which can complete the operations of+,-,*,/of two numbers \ n");

printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");

Printf ("\ t \ t * * * * * * main menu * * * * * * * \ n ");

Printf( "\t\t 1, addition \ n ");

Printf( "\t\t2, subtraction \ n ");

Printf( "\t\t3, multiplication \ n ");

Printf( "\t\t4, division operation \ n ");

Printf( "\t\t5,exit \ n ");

printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");

Printf ("Please enter the operation to be performed (the data entered is between 1~5) \ n");

scanf( "%d ",& ampx); If (x! = 1。 & ampx! = 2 & amp& ampx! = 3 & amp& ampx! = 4 & amp& ampx! =5 )// Control the input to ensure the input is correct.

{

Printf ("You typed it wrong! Please re-enter \ n ");

fflush(stdin); //Add this sentence to empty the input buffer. Otherwise, the characters will remain in the buffer.

scanf( "%d ",& ampx);

} returns x;

}//addition function

int jiafa()

{

int a,b,c; Printf ("You are currently adding \ nPlease enter the two data you want to calculate (please separate the two numbers with spaces when entering) \ n");

Scanf( "%d%d ",&i, & b);

c = a+b; Return to c;

}//subtraction function

Int jianfa ()

{

int a,b,c; Printf ("You are currently performing subtraction \ nPlease enter the two data you want to calculate (please separate the two numbers with spaces when entering) \ n");

Scanf( "%d%d ",&i, & b);

c = a-b; Return to c;

}//Multiplication function

Int Chengfa ()

{

int a,b,c; Printf ("You are currently performing multiplication \ nPlease enter two data to be calculated (please separate the two numbers with spaces when entering) \ n");

Scanf( "%d%d ",&i, & b);

c = a * b; Return to c;

}//division operation function

Floating chufa ()

{

Floating a, b, c; Printf ("You are currently performing division \ nPlease enter two data to be calculated (please separate the two numbers with spaces when entering) \ n");

Scanf( "%d%d ",&i, & b);

c = a/b; Return to c;

}//main function

int main()

{

int x; x = JM(); And (x! = 5 )

{

Switch (x)

{

Case 1:

Printf ("Result: %d\n ",jiafa());

Break;

Case 2:

Printf ("Result: %d\n ",jianfa());

Break;

Case 3:

Printf ("Result: %d\n ",Chengfa ());

Break;

Case 4:

Printf ("Result: %f\n ",chufa());

Break;

} printf ("\ t \ t \ tpress any key to return to the main menu! \ n ");

getch();

System ("CLS"); x = JM();

If (x == 5)

Printf ("Welcome to use this software! Thank you. Bye! ”);

}}