String math

I am creating a calculator program to learn more about C++, What I need to do is, I need this string number to spit out it's number, And modifier to spit out it's modifier, And number 2 to spit out it's number.

So, For example, If the user entered 4 into number, And * into modifier, And 7 into number 2, I need this program to do the math of that. I need it to make it look like I just wrote 4 * 7 into my compiler.

#include <iostream>
#include <string>

using namespace std;

string number;
string modifier;
string number2;
string input;

void Calculator() {
cout << "Welcome to the Calculator!\nPlease enter your calculation below!" <<endl << endl;
cout << ">";
cin >> number; // The first entered number, Such as 4
cout << ">";
cin >> modifier; // The entered modifier, Such as *
cout << ">";
cin >> number2; // The second entered number, Such as 7
number modifier number2; // These numbers combined and written to be calculated, As if I wrote 4 * 7;
cout << number << modifier << number2;


}

int main() {
Calculator();
}
Last edited on
Um, Hi?
to convert the string numbers to its value you may use stringstream:

http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

like so:
1
2
3
4
5
6
7
stringstream ss(number);
int val;
ss >> val;
if(ss.good())
  ... // do somehing with val
else
  cout << "Invalid number";
If you're wanting to ask for these values one at a time you don't need to use a string, use std::cin with an int type for your numbers and use std::cin with a char type, try using a switch to test the modifier and complete your maths using its outcome.
How would I do that Satsuma?
closed account (Dy7SLyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//lets assume i have all qualified
//namespaces and appropriate headers

int stoi(string &Line)
{
	for(char Counter : Line)
		if(!isnum(Counter))
		{
			cerr<<"error: please input only numbers"<< endl;
			return -1
		}

	istringstream Stream(Line);

	int ReturnValue;
	Stream >> ReturnValue;

	return ReturnValue;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//Assume headers are included and this is
//just a snippet taken out of a function or such

double num1, num2;
char op;

std::cout << "Enter first number: ";
std::cin >> num1;
std::cout << "Enter operation symbol: ";
std::cin >> op;
std::cout << "Enter second number: ";
std::cin >> num2;

switch(op){
case '+':
    std::cout << num1 << " + " << num2 << " = " << num1 + num2;
    break;
case '-':
    std::cout << num1 << " - " << num2 << " = " << num1 - num2;
    break;
case '*':
    std::cout << num1 << " * " << num2 << " = " << num1 * num2;
    break;
case '/':
    std::cout << num1 << " / " << num2 << " = " << num1 / num2;
    break;
default:
    std::cout << "Sorry, That's an invalid operator\n";
    break;
}


That is pretty much it, if you don't yet know about switch statements or the iostream I suggest a little research is in order on your end.
You can pretty much just stick this in a function and call it on a loop if you want your program to loop. You may also want to fiddle about with the output layout

WARNING: this method is more primitive than that posted by @DTSCode, I have tested this code before and a RunTime crash occurs if you try to enter a character key instead of a string of number keys. This of course can be protected against by various methods but it came across to me you've not been doing this for long so I thought simplicity and understanding might be better
Last edited on
closed account (Dy7SLyTq)
yeah my mistake. i forgot to add guards (well i actually did, but its pretty bad) to stoi. ill fix that
Sorry I didn't see you before MissValeska. I hope you haven't given up on this yet.

Here is your code with a couple small modifications to get you going in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
using namespace std;

void Calculator() {
  int a, b, c;
  char op;
  
  cout << "Welcome to the Calculator!\n"
          "Please enter your calculation below!\n\n";
	  
  cout << "> ";
  
  cin >> a >> op >> b;
  if (!cin) {
    cout << "Invalid input!\n";
    return;
  }
  
  switch (op) {
    case '*': c = a * b; break;
    default: 
      cout << "Invalid operator!\n";
      return;
  }
  
  cout << c << "\n";
}

int main() {
  Calculator();
}


@DTSCode
The stream extraction operator will do the "check for numbers" stuff for you.
closed account (Dy7SLyTq)
oh it does? i wrote it off the cuff. ok well i know for sure it converts a string of ints to ints.
It converts a single int, after checking that it contains only digits.
Topic archived. No new replies allowed.