I got an error while compiling your code, because you're declaring a variable "ssum" but using it as "sssum". What the heck is that supposed to mean? You should be giving all of your variables meaningful names.
When I fixed that, I got the following warnings:
Code:
/*
main.c||In function 'main':|
main.c|10|warning: format '%20s' expects type 'char *', but argument 2 has type 'char (*)[21]'|
main.c||In function 'evaluate':|
main.c|56|warning: label 'LINE3' defined but not used|
main.c|41|warning: label 'LINE2' defined but not used|
main.c|22|warning: unused variable 'j'|
||=== Build finished: 0 errors, 4 warnings ===|
*/
The last one is not a big deal.
The two before that are a bit strange - what's with the labels? They aren't needed for anything.
The first means you should not have the '&' in your "scanf()" call on line 10. You need to provide a pointer to the variable that will store the scanned input ... but an array name by itself acts as a pointer to its first element.
Code:
char expr[21];
// ...
scanf("%20s",expr);
Next, I have a complaint that might make me seem like a cantankerous old man. This line:
Code:
printf("Input ur string:\n");
The word is "your", not "ur". This is a computer program, not a text message. You are free to use whatever language you want in your programs, of course ... but I am also free to voice my opinion when I see something that irks me personally.
You should strive for clarity when programming a machine that interfaces with other human beings. Especially when it threatens to make you look like a fool in front of others. Serious programmers are professionals, and childish shorthand like this will not earn you points when it matters.
That was a bit harsh, and I do apologize for it. Especially since you so far seem to be taking advice given to you to heart. My only defense is, in the words of Al Stewart; "This is my nature ... to it, I have to be true".
Anyway, moving on.
Look at your "while(1)" loop. That is an infinite loop, so you need to take care of when to break out of it. So far, you only break out of it if the (next) character is an operator. (And I'd suggest checking for an operator
first there, and not incrementing "i" in that loop unless you found a digit.)
But what happens when it reaches the last value? The last character is presumably a digit, and you
only break out of the infinite loop when an operator is found. So you're not breaking out of the infinite loop at the end of the string ... and you keep going, accessing your "expr" array out of bounds.
Furthermore, your technique of building up an integer from a string using "expr[i]-'0" is generally alright ... but you're trying to store the results in a float. Building up a floating point number in this fashion is a totally different beast. If your input is only supposed to be integers, I would suggest declaring those variables as ints.
This will go a part of the way towards finding your problems, but there are still others to be had.
I would suggest you consider planning out the logic more before trying to write code. This would be a good time to learn flow-charts. You can plan the logic, step by step, by hand on paper, and verify (and understand) it before comitting it to code.