Help making a basic calculator.

I'm stuck because I can make the calculator just find but the user will have to press enter after every number, what is s solution to this?

#include <iostream>
#include <string>
using namespace std;
int a = 1, b, c;
string symbol;
int multiplyFunction();
int main()
{
// This is the problem, the user has to press enter after everything, making it a tedious process.
cin >> a;
cin >> symbol;
cin >> b;
if (symbol == "*");
{
cout << multiplyFunction();
}
system("pause");

return 0;
}
// Function to multiply, followed by function to divide, add, etc.
int multiplyFunction()
{
c = a * b;
return c;
}
You don't have to press enter every time. cin uses the space character as a delimiter. So just seperate your entries with a space. Also you need to prompt the user, so they know what to input.
Thank you, that's really helpful information, but now I have this problem of the program performing all of the functions
#include <iostream>
#include <string>
using namespace std;
int a, b, c, w = 1;
string symbol;
int multiplyFunction();
int divideFunction();
int addFunction();
int substractFunction();
int main()
{
while (w = 1)
{
// The program runs all of this code so I get 5 + 5 will get 10125 (5 + 5, 5 / 5, and 5 * 5
cin >> a;
cin >> symbol;
cin >> b;
if (symbol == "+");
{
cout << addFunction();

}
if (symbol == "/");
{
cout << divideFunction();
}
if (symbol == "*");
{
cout << multiplyFunction();
}
}
system("pause");

return 0;

}
// Function to multiply, followed by function to divide, add, etc.
int substractFunction()
{
c = a + b;
return c;
}
int addFunction()
{
c = a + b;
return c;
}
int divideFunction()
{
c = a / b;
return c;
}
int multiplyFunction()
{
c = a * b;
return c;
}
I don't know what you were having trouble with, but I saw that you had made an error in the substractFuntion(), which I corrected and added to the main. I am on Mac, so you will have to add SysPAUSE again. I also added a user prompt.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
using namespace std;
int a, b, c, w = 1;
string symbol;
int multiplyFunction();
int divideFunction();
int addFunction();
int substractFunction();
int main()
{
cout << "Enter Your Full Equation, Then Press ENTER: ";
while (w == 1)
{
// The program runs all of this code so I get 5 + 5 will get 10125 (5 + 5, 5 / 5, and 5 * 5
cin >> a;
cin >> symbol;
cin >> b;
if (symbol == "+")
{
cout << addFunction();

}
if (symbol == "/")
{
cout << divideFunction();
}
if (symbol == "*")
{
cout << multiplyFunction();
}
if (symbol == "-")
{
cout << substractFunction();
}
}


return 0;
}
// Function to multiply, followed by function to divide, add, etc.
int substractFunction()
{
c = a - b;
return c;
}
int addFunction()
{
c = a + b;
return c;
}
int divideFunction()
{
c = a / b;
return c;
}
int multiplyFunction()
{
c = a * b;
return c;
}
Last edited on
Thank you, did it work fine with you? or am I correct in assuming that because strings are not a native c++ data type that you can't check them in this manner?
Last edited on
I had no issues with it.
I get 101250, using C++ express.
Last edited on
Topic archived. No new replies allowed.