0
#include<iostream>
#include<cmath>
#include<string>
#include<fstream>
using namespace std;    

struct Calculations //structure to hold the numbers and operators from 'equation' 
{
    double num1;
    char   operators;
    double num2;
    double answer;
};
Calculations myCalculationArray[SIZE]; // the array of calculations
int main()
{
    
    while (i = 0; i <= 5; i++;)
    {
        cout << "Enter equation: \n";

        getline(cin, equation);

        cout << equation;
    }
}

Alright so I'm trying to build a calculator where the user inputs an equation like "22/2", then to have it like assign the first number to num1 = ' 22 ' and the operator =' / ' to operators etc.

usedcarmax
  • 23
  • 1
  • 1
  • 7
  • Is it homework? It would be ok to ask about a more specific part of what you need, maybe like: "what should be the parts for this solution?" but currently it sounds like "could you please solve this problem for me". – Amir Kirsh Nov 11 '20 at 03:57
  • Yes it is homework, I don't understand how to parse the string to give me the individual parts. – usedcarmax Nov 11 '20 at 04:07
  • `while` should be replaced with `for` and `;` not need after `i++` – csavvy Nov 11 '20 at 04:22
  • @sravs why should I use "for" instead of "while" – usedcarmax Nov 11 '20 at 04:56
  • you have written three parts(initialization;condition check;update) as in `for`. `while` takes only part condition check like `while(i <= 5)` – csavvy Nov 11 '20 at 04:59
  • So maybe ask the question about the parsing. Then you can get an answer on that. – Amir Kirsh Nov 11 '20 at 04:59
  • @sravs oh thank you i appreciate it. – usedcarmax Nov 11 '20 at 05:04
  • Given a `double` to read into, the `>>` operator will read until it finds something that cannot be a in a `double` +, -, *, and / are good candidates. So you can turn the line from `getline` into an `istringsream` and then use `>>` on the `istringstream` to pull out the pieces you need. Use [Option 2 of this answer](https://stackoverflow.com/a/7868998/4581301) for inspiration. – user4581301 Nov 11 '20 at 05:26
  • [std::stoi, std::stol, std::stoll](https://en.cppreference.com/w/cpp/string/basic_string/stol) and [std::string::find_first_of](https://en.cppreference.com/w/cpp/string/basic_string/find_first_of) and [find_first_not_of](https://en.cppreference.com/w/cpp/string/basic_string/find_first_not_of) look very useful. – David C. Rankin Nov 11 '20 at 06:05

2 Answers2

0

Since it seems like it is an homework for a student I will give you another solution for a calculator and you can modify it as you like ;)

#include<iostream>
#include<cmath>
using namespace std;    


int main() {
char op;
float num1, num2;

cout << "Enter two operands: ";
cin >> num1 >> op >> num2;

switch(op)
{
    case '+':
        cout << num1+num2;
        break;

    case '-':
        cout << num1-num2;
        break;

    case '*':
        cout << num1*num2;
        break;

    case '/':
        cout << num1/num2;
        break;

    default:
        // If the operator is other than +, -, * or /, error message is shown
        cout << "Error! operator is not correct";
        break;
} }
Emma
  • 27,428
  • 11
  • 44
  • 69
the_thing
  • 1
  • 1
0

You can take the help of strtok, strchr for parsing the string (separating out the operation symbol and the two numeric operands). To convert the numeric string such as "34" to 34 , you can use the functions like atoi and atod etc.

Ravi Prakash
  • 313
  • 1
  • 8