Hello to all!

I decided to study C ++ , but there is a problem. I wrote a calculator, but it does not work. As a result of the action writes "0".

Here is the code:

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <locale> #include <conio.h> float a, b, c; char d; int p; int main() { while (p != 2) { printf("1 = calculator \n 2 - exit \n"); scanf("%d", &p); switch (p) { case 1: { float c; printf("First number "); scanf("%d", &a); printf("Action with numbers "); scanf("%s", &d); printf("Second number "); scanf("%d", &b); if (d == '+') c = a + b; if (d == '-') c = a - b; if (d == '*') c = a * b; if (d == '/') c = a / b; system("cls"); } case 2: { break; } } printf("Result = %2.f \n", c); } } 
  • one
    1. Format the code. 2. Error in mismatch of format string of scanf function and variable types. - paulgri
  • We decided to study C ++, but write at the same time on C. Couples of plus inclusions are not enough for this. - αλεχολυτ

2 answers 2

You read d as a string, while it is char . For it, you must use the %c specifier. Further, not %d for float , but %f . And why do not you use double - tea, the court is not the 80s ... :)

Well, I would use the switch instead of the if 's ladder.

 case 2: break 

You will be thrown out of switch , but not from while . Again, when entering the while value of p is undefined ...

I would do something like that - true, there is no protection against a fool (wrong input), but this is my own :)

 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { double a, b, c; char d; int p = 1; while (p != 2) { printf("1 - calculator\n2 - exit\n"); scanf("%d", &p); if (p != 1) break; printf("Write expression (without spaces): "); scanf("%lf%c%lf", &a,&d,&b); while(getchar() != '\n'); switch(d) { case '+': c = a + b; break; case '-': c = a - b; break; case '*': c = a * b; break; case '/': c = a / b; break; default: printf("Wrong action\n"); continue; } printf("Result = %lf\n", c); } } 

Just type 2*2 and that's it.

    Since you are learning C ++ , it’s better to use cout and cin instead of printf and scanf . They do not need to worry about the format of the received and displayed values, plus there is no such problem as getting the wrong character into the input buffer. In your case, instead of %d you need to use %f to read float values, and to read the %c symbol. During the operation of your code after correcting the formatting, for example, instead of reading the operation character, I read a newline character, and the input immediately switched to the second number. To avoid this, you should clear the input buffer with the fflush(stdin) command. However, all these issues are resolved using cout and cin .

    Next, you have twice declared the variable c , one is global, and the second is in scope case 1: Inside case 1: you assign the result to a local variable, and the command to output the result to the screen is outside the scope of this variable, and displays the global variable c , which has not been assigned anything, hence the constant result 0 .

    Here is my version of the code:

     #include <iostream> using namespace std; float a, b, c; char d; int p; int main() { while (p != 2) { cout << "1 = calculator \n 2 - exit \n"; cin >> p; switch (p) { case 1: { cout <<"First number "; cin >> a; cout << "Action with numbers "; cin >> d; cout << "Second number "; cin >> b; if (d == '+') c = a + b; if (d == '-') c = a - b; if (d == '*') c = a * b; if (d == '/') c = a / b; } case 2: { break; } } cout << "Result =" << c <<endl; } }