Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

this is supposed to be a calculator using c++ but why does the calculations come out wrong?

+1 vote
asked Sep 11, 2021 by Ali Tarek (130 points)
#include <iostream>
#include <string>

using namespace std;
void PrintMessage (string Message)
{
    cout <<"****************************\n";
    cout << Message <<"\n";
    cout <<"****************************\n";
}

int main()
{
    cout <<"Welcome to the calculator!\n";
    char key = '`';
    while(key != 'x')
    {
        int y =0 ;
        cout <<"Enter the number of integers you want to use.\n";
        cin >> y ;
        const int x = y;
        int GroupOfNumbers [x] , j = 1;
        long Sum =0;
        for (int i =0 ; i < x ; i++)
        {
            cout <<"Enter integer NO." <<j <<"\n";
            j++;
            cin >>GroupOfNumbers[i];
            char operator1 = 't';
            cout <<"Enter operator\n";
            cin >> operator1;
            switch (operator1)
            {
             case '+':
               Sum = Sum + GroupOfNumbers[i];
               break;
             case '-':
                Sum = Sum - GroupOfNumbers[i];
                break;
             case '*':
                Sum = Sum * GroupOfNumbers[i];
                break;
             case '/':
                Sum = Sum / GroupOfNumbers[i];
                break;
             default:
                PrintMessage("Error \nPlease restart the program.");
                break;
            }
        }
        cout<<"Sum = " <<Sum <<"\n" ;
        PrintMessage("Press any key to continue or 'x' to exit");
        cin >> key ;
    }
    return 0;
}

1 Answer

0 votes
answered Sep 20, 2021 by Peter Minarik (84,720 points)

You are working with binary operators: one operator needs two operands. E.g. 2 + 3 --> [operand] [operator] [operand]

On the other hand, your code (incorrectly) asks for an operator after every operand (number). 2 + 3 +. Your code applies the first operator to the first operand (number) and the existing Sum variable (which is initialized to 0).

One solution would be the following:

  1. After having the first number, you set Sum := firstNumber.
  2. When the used needs to provide n pieces of numbers, then you ask for n-1 pieces of operators, not asking for any more operators after the nth number.

Also, for input, you have a problem: reading a single character messes up the input stream buffer. Try the following instead:

#include <cstdio> // for sscanf
// ...
cin >> buffer;
char operator1;
sscanf(buffer.c_str(), "%c", &operator1);

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...