For some reason my program doesn't read plus sign at all and it doesn't matter if I do it this way:
#include <stdio.h>
#include <math.h>
int main (){
int nextInt,number,boolean;
char sign;
scanf("%d", &number);
boolean = 1;
while(boolean == 1){ //+43 *42 %37
scanf("%c %d", &sign, &nextInt);
switch ((int)sign){
case 43:{ number += nextInt; printf("+= %d\n", number); break;}
case 42:{ number *= nextInt; printf("*= %d\n", number); break;}
case 37:{ printf("mod %d\n", number);
number %= nextInt;
boolean = 0; break;}
}
}
printf("%d", number);
return 0;
}
Or switch ascii with symbols for switch (sign). I'm kinda clueless what is really the source of the problem right now. What am I doing wrong?
EDIT: Entire code pasted
Since 1806 % 37 = 30, you can verify that the following works.
#include <stdio.h>
int main (){
int nextInt=0;
int number=0;
int boolean;
char sign;
boolean = 1;
while(boolean == 1){ //+43 *42 %37
scanf(" %c %d", &sign, &nextInt);
switch (sign){
case '+':{ number += nextInt; printf("+= %d\n", number); break;}
case '*':{ number *= nextInt; printf("*= %d\n", number); break;}
case '%':{ printf("mod %d\n", number);
number %= nextInt;
boolean = 0; break;}
}
}
printf("%d", number);
return 0;
}
The reason your program didn't read plus is that it wasn't in the loop. You can verify that the program works:
+43
+= 43
*42
*= 1806
%37
mod 1806
30
Process finished with exit code 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.