Home c++ C++ Basic Calculator

C++ Basic Calculator

Author

Date

Category

Hello everyone!

I decided to learn C++ , but here’s the problem. I wrote a calculator, but it doesn’t work. As a result of the actions, it writes “0”.

Here’s the code:

# define _CRT_SECURE_NO_WARNINGS
#include & lt; iostream & gt;
#include & lt; locale & gt;
#include & lt; conio.h & gt;
float a, b, c;
char d;
int p;
int main ()
{
  while (p! = 2)
  {
    printf ("1 = calculator \ n 2 - exit \ n");
    scanf ("% d", & amp; p);
    switch (p)
    {
    case 1:
    {
      float c;
      printf ("First number");
      scanf ("% d", & amp; a);
      printf ("Action with numbers");
      scanf ("% s", & amp; d);
      printf ("Second number");
      scanf ("% d", & amp; 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);
  }
}

Answer 1, authority 100%

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 don’t you use double – tea, it’s not the 80s … 🙂

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

case 2: break

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

I would do something like this – however, there is no protection from the fool (incorrect input), but this is already on its own 🙂

# define _CRT_SECURE_NO_WARNINGS
#include & lt; stdio.h & gt;
int main ()
{
  double a, b, c;
  char d;
  int p = 1;
  while (p! = 2)
  {
    printf ("1 - calculator \ n2 - exit \ n");
    scanf ("% d", & amp; p);
    if (p! = 1) break;
    printf ("Write expression (without spaces):");
    scanf ("% lf% c% lf", & amp; a, & amp; d, & amp; 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.


Answer 2

Since you are learning C++ , it is better to use cout and cin instead of printf and scanf . They do not need to worry about the format of the received and output values, plus there is no such problem as hitting the wrong character in the input buffer. In your case, instead of % d , you need to use % f to read values ​​of the float type, and to read the character % c … When your code was running, after a formatting amendment, for example, instead of reading the operation symbol, a newline was read, and the input went immediately to the second number. To avoid this, the input buffer should be flushed with the fflush (stdin) command. However, all of these issues are resolved using cout and cin .

Next, you have c declared twice, one globally, and the other in scope case 1: . Inside case 1: , you assign the result to a local variable, and the command to display the result to the screen is outside the scope of this variable, and displays the global variable c , to which nothing has been assigned , hence the permanent result 0 .

Here’s my version of the code:

# include & lt; iostream & gt;
using namespace std;
float a, b, c;
char d;
int p;
int main ()
{
  while (p! = 2)
  {
    cout & lt; & lt; "1 = calculator \ n 2 - exit \ n";
    cin & gt; & gt; p;
    switch (p)
    {
    case 1:
    {
      cout & lt; & lt; "First number";
      cin & gt; & gt; a;
      cout & lt; & lt; "Action with numbers";
      cin & gt; & gt; d;
      cout & lt; & lt; "Second number";
      cin & gt; & gt; 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 & lt; & lt; "Result =" & lt; & lt; c & lt; & lt; endl;
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions