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.
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;
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.
//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