Calculator Program Trouble

Ok, in my C++ Console Project, I am prompting the user to make a choice of multiplication, addition, subtraction, or division, and then enter two numbers to perform the selected operation. But I am having some syntax errors I am not sure how to fix. Here is my code:

[code=cpp]
#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>

using namespace std;

void Wait(int seconds)
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}

int main( void )

{
//variables
int number1;
int number2;
int answer;
char choice;
char keepplaying = "y";
//code
while(keepplaying == "y")
{
cout << "Enter your choice of math (+ , - , /, x): ";
cin >> choice;
if(choice = "+")
{
cout << "Enter two numbers to add together:" << endl;
cin >> number1;
cout << "+" << endl;
cin >> number2;
cout << endl;
answer = number1 + number2;
cout << endl << "Calculating...";
Wait( 5 );
cout << " ... Done Calculating! Your answer is... " << answer << endl << endl;
system("PAUSE");
}
else if(choice = "-")
{
cout << "Enter two numbers to subtract:" << endl;
cin >> number1;
cout << "-" << endl;
cin >> number2;
cout << endl;
answer = number1 - number2;
cout << endl << "Calculating...";
Wait( 5 );
cout << " ... Done Calculating! Your answer is... " << answer << endl << endl;
system("PAUSE");
}
else if(choice = "/")
{
cout << "Enter two numbers to divide:" << endl;
cin >> number1;
cout << "/" << endl;
cin >> number2;
cout << endl;
answer = number1 / number2;
cout << endl << "Calculating...";
Wait( 5 );
cout << " ... Done Calculating! Your answer is... " << answer << endl << endl;
system("PAUSE");
}
else if(choice = "x")
{
cout << "Enter two numbers to add together:" << endl;
cin >> number1;
cout << "x" << endl;
cin >> number2;
cout << endl;
answer = number1 * number2;
cout << endl << "Calculating...";
Wait( 5 );
cout << " ... Done Calculating! Your answer is... " << answer << endl << endl;
system("PAUSE");
}
else
{
cout << "That is not a valid option." << endl << endl << endl << endl;
}
cout << "Play again? (Y/N)" ;
cin >> keepplaying;
}

}
[/code]

And here is the build log.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
1>Compiling...
1>Test.cpp
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\test\test\test.cpp(24) : error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char'
1>        There is no context in which this conversion is possible
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\test\test\test.cpp(26) : error C2446: '==' : no conversion from 'const char *' to 'int'
1>        There is no context in which this conversion is possible
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\test\test\test.cpp(26) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\test\test\test.cpp(30) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1>        There is no context in which this conversion is possible
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\test\test\test.cpp(43) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1>        There is no context in which this conversion is possible
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\test\test\test.cpp(56) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1>        There is no context in which this conversion is possible
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\test\test\test.cpp(69) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1>        There is no context in which this conversion is possible
1>Build log was saved at "file://c:\Documents and Settings\Brad\My Documents\Visual Studio 2008\Projects\Test\Test\Debug\BuildLog.htm"
1>Test - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========





I am using Windows with Microsoft Visual C++ 2008 Express Edition. This is a console project.
I am running MinGW IDE, but I make the following changes to the code and it work. hehe.


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>

using namespace std;

void Wait(int seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}

int main( void )

{
	//variables
	int number1;
	int number2;
	int answer;
	char choice;
	char keepplaying = 'y';
	//code
	while(keepplaying == 'y')
	{
	cout << "Enter your choice of math (+ , - , /, x): ";
	cin >> choice;
	if(choice == '+')
	{
		cout << "Enter two numbers to add together:" << endl;
		cin >> number1;
		cout << "+" << endl;
		cin >> number2;
		cout << endl;
		answer = number1 + number2;
		cout << endl << "Calculating...";
		Wait( 5 );
		cout << "   ... Done Calculating! Your answer is... " << answer << endl << endl;
		system("PAUSE");
	}
	else if(choice == '-')
	{
		cout << "Enter two numbers to subtract:" << endl;
		cin >> number1;
		cout << "-" << endl;
		cin >> number2;
		cout << endl;
		answer = number1 - number2;
		cout << endl << "Calculating...";
		Wait( 5 );
		cout << "   ... Done Calculating! Your answer is... " << answer << endl << endl;
		system("PAUSE");
	}
	else if(choice == '/')
	{
		cout << "Enter two numbers to divide:" << endl;
		cin >> number1;
		cout << "/" << endl;
		cin >> number2;
		cout << endl;
		answer = number1 / number2;
		cout << endl << "Calculating...";
		Wait( 5 );
		cout << "   ... Done Calculating! Your answer is... " << answer << endl << endl;
		system("PAUSE");
	}
	else if(choice == 'x')
	{
		cout << "Enter two numbers to add together:" << endl;
		cin >> number1;
		cout << "x" << endl;
		cin >> number2;
		cout << endl;
		answer = number1 * number2;
		cout << endl << "Calculating...";
		Wait( 5 );
		cout << "   ... Done Calculating! Your answer is... " << answer << endl << endl;
		system("PAUSE");
	}
	else
	{
		cout << "That is not a valid option." << endl << endl << endl << endl;
	}
	cout << "Play again? (Y/N)"  ;
	cin >> keepplaying;
	}
	
}
I compiled and following changes in visual studion 6.0, Please check.


#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>

using namespace std;

void Wait(int seconds)
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}

int main( void )

{
//variables
int number1;
int number2;
int answer;
char choice;
char keepplaying = 'y';
//code
while(keepplaying == 'y')
{
cout << "Enter your choice of math (+ , - , /, x): ";
cin >> choice;
if(choice = '+')
{
cout << "Enter two numbers to add together:" << endl;
cin >> number1;
cout << "+" << endl;
cin >> number2;
cout << endl;
answer = number1 + number2;
cout << endl << "Calculating...";
Wait( 5 );
cout << " ... Done Calculating! Your answer is... " << answer << endl << endl;
system("PAUSE");
}
else if(choice = '-')
{
cout << "Enter two numbers to subtract:" << endl;
cin >> number1;
cout << "-" << endl;
cin >> number2;
cout << endl;
answer = number1 - number2;
cout << endl << "Calculating...";
Wait( 5 );
cout << " ... Done Calculating! Your answer is... " << answer << endl << endl;
system("PAUSE");
}
else if(choice = '/')
{
cout << "Enter two numbers to divide:" << endl;
cin >> number1;
cout << "/" << endl;
cin >> number2;
cout << endl;
answer = number1 / number2;
cout << endl << "Calculating...";
Wait( 5 );
cout << " ... Done Calculating! Your answer is... " << answer << endl << endl;
system("PAUSE");
}
else if(choice = 'x')
{
cout << "Enter two numbers to add together:" << endl;
cin >> number1;
cout << "x" << endl;
cin >> number2;
cout << endl;
answer = number1 * number2;
cout << endl << "Calculating...";
Wait( 5 );
cout << " ... Done Calculating! Your answer is... " << answer << endl << endl;
system("PAUSE");
}
else
{
cout << "That is not a valid option." << endl << endl << endl << endl;
}
cout << "Play again? (Y/N)" ;
cin >> keepplaying;
}

}
@dgirdhar: Please put in a [c0de] [/c0de] tag, but spell code correctly.

@cheesebeefy:this probably will not solve your problem, but try using a switch statement, as follows:

1
2
3
4
5
6
7
8
9
10
switch(choice)
{
case '+':
//do what ever you need to do, etc.
break;// MAKE SURE TO BREAK EVERY TIME YOU FINISH YOUR CASE!!!!
//other cases...
default:
cout << "That is not a valid operation.\n";
break;
}


EDIT: char 'keepplaying' should be char keepplaying == 'y', as opposed to "y". Note the diff.
Same for everywhere else you used double quotes to declare/compare a char type variable.
Last edited on
Thank you everyone! That fixed it. And I'll try out that switch statement QWERTY. :)
Topic archived. No new replies allowed.