I just started to learn programming in C. Was trying to make a programm to evaluate simple calculations. It's working but I can't end this program. My "End of the calculations"doesn't work. Can someone tell me where I am wrong.
Thank you.
I just started to learn programming in C. Was trying to make a programm to evaluate simple calculations. It's working but I can't end this program. My "End of the calculations"doesn't work. Can someone tell me where I am wrong.
Thank you.
What's wrong with that?Code:./temp S - to set number to Accumulator E - to end calculation Begin Calculation 8S =8.00 4+ =12.00 3- =9.00 0E =9.00 End of the calculations
I must be doing something wrong see my output
S - to set number to Accumulator
E - to end calculation
Begin Calculation
10S
=10.00
5+
=15.00
2-
=13.00
0E
Uknown operator
Surely your runtime isn't treating 0E as a valid float? What happens if you do "0E0E"?
If you want to see what your system thinks the operator is, print it in your unknown operator message.
I don't see anything wrong with the code. I mean your output is formatted rather ponderously, but whatever.
You are right it worked with 0E0E. and it work when I replace uknown operator message on " End of calculations ".
Fix:
Code:// program to evaluate simple calculatins #include<stdio.h> #include<ctype.h> int main(void) { float total=0; // accumulator float a; char op; char buffer[256]; printf("S - to set number to Accumulator\n"); printf("E - to end calculation\n\n"); printf("Begin Calculation\n"); do{ if(fgets(buffer, sizeof buffer, stdin) == NULL) break; if(toupper(buffer[1]) == 'E' && buffer[2] == '\n') { printf("=%.2f\n",total); printf("End of the calculations\n"); break; } sscanf(buffer, "%f%c",&a,&op); switch(op) { case'S': //Set accumulator printf("=%.2f\n",a); total=a; break; case'+': //Addition total=total+a; printf("=%.2f\n",total); break; case'-': //Subtraction total=total-a; printf("=%.2f\n", total); break; case'*': //Multiplication total=total*a; printf("=%.2f\n", total); break; case'/': //Division if (a==0) printf("Division by zero\n"); else total=total/a; printf("=%.2f\n", total); break; default: //default printf("Uknown operator\n"); break; } } while(op!='E'); return 0; }
Thank you tabstop and master5001. All additions to the code what master5001 advised are new to me, I mean we did no learn that yet, anyway that great to understand that there is someone who always will help you. Again thanks guys.
To give you a quick overview of what my code does differently is that it copies the whole line into a memory buffer before it runs scanf(). I use sscanf() because unlike scanf() it reads a chunk of memory instead of the keyboard input (which is highly over simplifying what scanf() really does, but I like to keep things simple for explanation's sake).
After it reads in the line it checks to see if the second character in the line is 'e' or 'E' (it is not case sensitive in my code) and also checks to make sure that 'e' is the last character in the line (just to make sure to complain if the user goes crazy with their input). If the line is in the appropriate form, it ends the calculation before even trying to see what operators you may have tried to use.
I also killed off all your global variables as they need not be globals to begin with. Here is a cool trick that doesn't change your code very much at all.
Code:// program to evaluate simple calculatins #include<stdio.h> int main(void) { float total=0; // accumulator float a; char op; printf("S - to set number to Accumulator\n"); printf("E - to end calculation\n\n"); printf("Begin Calculation\n"); do{ if (scanf("%f%c",&a,&op) != 2) { printf("=%.2f\n",total); printf("End of the calculations\n"); break; } switch(op) { case'S': //Set accumulator printf("=%.2f\n",a); total=a; break; case'+': //Addition total=total+a; printf("=%.2f\n",total); break; case'-': //Subtraction total=total-a; printf("=%.2f\n", total); break; case'*': //Multiplication total=total*a; printf("=%.2f\n", total); break; case'/': //Division if (a==0) printf("Division by zero\n"); else total=total/a; printf("=%.2f\n", total); break; default: //default printf("Uknown operator\n"); break; } } while(op!='E'); return 0; }