How do I detect an operator vs. int in C using scanf?

Your scanf reads the whole string. It's the following switch that judges by the first character and misses that in 2 To improve it you can use the strtol function. It will parse an integer out of the string and return to you the location where the integer ended - if that's still not the end of the string, there may be an operator there A similar function for floating point numbers is strtod Here's some sample code of strtol applicable to your example: include #include #include int main() { char* input = "25+"; char* endptr; int val = strtol(input, &endptr, 10); if (*endptr == '\0') { printf("Got only the integer: %d\n", val); } else { printf("Got an integer %d\n", val); printf("Leftover: %s\n", endptr); } return 0; }.

Your scanf reads the whole string. It's the following switch that judges by the first character and misses that + in 2+. To improve it you can use the strtol function.It will parse an integer out of the string and return to you the location where the integer ended - if that's still not the end of the string, there may be an operator there.

A similar function for floating point numbers is strtod. Here's some sample code of strtol applicable to your example: #include #include #include int main() { char* input = "25+"; char* endptr; int val = strtol(input, &endptr, 10); if (*endptr == '\0') { printf("Got only the integer: %d\n", val); } else { printf("Got an integer %d\n", val); printf("Leftover: %s\n", endptr); } return 0; }.

I read the syntax but I'm not familiar on how to use it. This isn't working: scanf("%s",s); n = strtol(s, 0, 10); switch(n){} – patrick Jan 28 '10 at 11:32 @unknown (google): updated with a code example – Eli Bendersky Jan 28 '10 at 13:39 Thank you for your help! Got it running correctly.

– patrick Jan 28 '10 at 16:16 @unknown: glad I helped. Good luck – Eli Bendersky Jan 28 '10 at 16:27.

I'm not sure if I fully understood your question, but you could iterate through the string like this: for(i = 0; I.

I really didn't understand your code. If expect the user to enter one character each time, I mean one character + enter, you should use a simple char instead of char. And if you pretend to use a string you should receive it and parse it pzico said.

You could do something like that. The problem would be in the treatment of numbers with multiple digits, but thinking a little bit you can fix this problem. I wrote an attempt, but I'm pretty sure it's not going to work.

Printf("\nRPN Calculator\n"); printf("Enter 'i' for integer mode\n"); printf("Enter 'f' for floating point mode\n"); printf("Enter 'q' to quit\n"); scanf("%c", s); switch(*s){ case 'i': printf("(Integer Mode)\n"); break; case 'f': printf("(Floating Point Mode)\n"); break; case 'q': printf("Bye Bye\n"); return; break; } printf("Enter the expression one character each time\n"); do { scanf("%c", s); switch(s) { case '+': a = pop(); be = pop(); printf("%d\n", a+b); push(a+b); break; case '-': a = pop(); be = pop(); printf("%d\n", b-a); push(b-a); break; case '*': a = pop(); be = pop(); printf("%d\n", a*b); push(a*b); break; case '/': a = pop(); be = pop(); if(a == 0){ printf("Cannot divide by zero\n"); break; } printf("%d\n", b/a); push(b/a); break; case '. ': a = pop(); push(a); printf("Current value on top of stack: %d\n", a); break; default: a = pop()*10+atoi(s); push(a); } } while (s! = 'q'); Another problem in your code is in your pop function.

What do you want to do with this test: if(p.

Rather than default assumes a digit (try '%' for s), use the isdigit() function. Remember that the default: case is for anything that does not match any of the other cases in the switch statement. Another solution is to add case statements for each character in '0' ... '9', such as: case '0': case '1': – Thomas Matthews Jan 28 '10 at 19:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions