Calculator

I have created basic code for a calculator but I am trying to make it do a single calculation but it always makes the answer 1 please help


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
#include <iostream>

using namespace std;

int main()
{
	cout << "Welcome to Calculator v 1.5\n\n";
	cout << "Maths Symbols";
	cout << "Add : +\n";
	cout << "Subtract : -\n";
	cout << "Multiply : *\n";
	cout << "Divide : /\n";

	char x;
	char y;
	char z;

	system("title Calculator");
	system("color 4f");

	cout << "\n\nEnter a your sum\n";

	cout << "\n>";

	cin >> x; y; z;
	
	system("pause");

	cout << "\n\nThe answer is \n\n" << x; y; z;

	cout <<"\n";

return 0;

}


Thanks
On line 25 you meant cin >> x >> y >> z;
and on line 29 cout << "\n\nThe answer is \n\n" << x << y << z; I suppose...
But that will only show the input of the user.
change x and z to double and output the calculation based on the value of y
Last edited on
what do you mean by that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double x,z, result;
char y;
cin >> x >> y >> z;
switch (y)
{
     case '+':
          result = x+z;
          break;
     case '-':
          result = x-z;
          break;
     case '*':
          result = x*z;
          break;
     case '/':
          result = x/z;
          break;
}
cout << "\nThe result is: " << result;
I get these errors:

1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(25) : error C2371: 'x' : redefinition; different basic types
1> c:\documents and settings\timbo\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(14) : see declaration of 'x'
1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(25) : error C2371: 'z' : redefinition; different basic types
1> c:\documents and settings\timbo\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(16) : see declaration of 'z'
1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(26) : error C2086: 'char y' : redefinition
1> c:\documents and settings\timbo\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(15) : see declaration of 'y'
1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(27) : error C2088: '>>' : illegal for class
1>c:\documents and settings\timbo\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(45) : error C2088: '<<' : illegal for class

you should delete previous declarations of x, y and z from your code
Topic archived. No new replies allowed.