Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- //most important here is how to get rid of unwanted input in scanf and getchar, lines 23-26, 56-57
- int main(void)
- {
- char ch;
- int n,m;
- int sc_res,flag,count;
- printf("1) +, 2) -, 3) *, 4) /, Q for quit\n");
- while((ch=getchar())!='Q')
- {
- if(ch>'4'||ch<'1')
- {
- printf("wrong input, try again\n");
- }
- else
- {
- flag = 1; //*****
- count = 0;
- while(scanf("%d %d",&n,&m)!=2)
- {
- //scanf("%*[^\n]");//next 3 lines equals? and gets rid of bufer?threat?, so that next time scanf
- //can take new input
- //fflush(stdin);
- rewind(stdin);
- printf("Error Input 2 numbers again\n");
- }
- switch(ch)
- {
- case '1':
- printf("%d plus %d is %d\n",n,m,n+m);
- break;
- case '2':
- printf("%d minus %d is %d\n",n,m,n-m);
- break;
- case '3':
- printf("%d multiplied by %d is %d\n",n,m,n*m);
- break;
- case '4':
- {
- while(!m) //if user input 0 as divider ***
- {
- printf("enter non-zero value\n");
- scanf("%d",&m);
- }
- printf("%d divided by %d is %d\n",n,m,n/m);
- }
- }//end of switch
- }//end of big else
- printf("1) +, 2) -, 3) *, 4) /, Q for quit\n");
- while(getchar()!='\n')//getting rid of the rest of input,including newline ***
- continue;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement