New C++ user with a question about a calculator program

Pages: 12
Hi, i have been assigned to add to a program i already have (here it is below)

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
//Calculator by Me

//Includes the iostream library 
#include <iostream>

//Use the standard namespace
using namespace std;

void main (	)
{	//Declares the Variables
	float Number_1;
	float Number_2;
	float Result;
	int Which_Calculation;

	//Give instructions
	cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
	cin >> Which_Calculation;

	//Get numbers
	cout << "Please enter the first number" << endl;
	cin >> Number_1;
	cout << "Please enter the second number" << endl;
	cin >> Number_2;

	if (Which_Calculation == 1)
{
	//Calculate the result
	Result = Number_1 + Number_2;
}

	if (Which_Calculation == 2)
{
	//Calculate the result
	Result = Number_1 - Number_2;
}

	if (Which_Calculation == 3)
{
	//Calculate the result
	Result = Number_1 * Number_2;
}

	if (Which_Calculation == 4)
{
	//Calculate the result
	Result = Number_1 / Number_2;
}
	//Print the answer is...
	cout << "The answer is..." << endl;


	//Print the result
	cout << Result << endl;


	system ("PAUSE");
}


I have to add one "While" loop to this code, and also make it so that when i ask the user "Would you like to do another task?" the person clicks "1" the program will restart, and if the user hits "2" the program will close. any suggestions how to do that? thanks in advance!
Last edited on
Put your code in the code tags please.
sry, like i said, i'm new here. lol. didn't know how to do that.
I think you mean press 1 not click one, as the program is a console application. And please, DON'T declare main as a void! Declare it as an int, and return 0 at the end to signify that it worked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << "Would you like to do another task?" << endl; // ask
int reply; // the users reply
cin >> reply; // get the input

switch(reply) // in case you're not familiar with switch statements, its a short way of doing loads of if statements about the same variable
{
        case 1: // if they typed 1...
        {
                main(); // repeat the main function again
        }
        case 2: // if they typed 2...
        {
                return 0; // exit the program, and tell it that nothing went wrong
        }
}


I have to add one "While" loop to this code

Does it have to be any old while loop, or does it have to do something?
ok, and yes, my teacher says i have to have a "while" loop in it. We haven't learned about "switch" yet, so maybe i should stick with the "if" statements for now...

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
 while (Guess != Secret_Number)

   {

      //This code repeats until the condition is no longer true

   

      // Get the user's guess

      cout << "Guess the secret number." << endl;

      cin >> Guess;

 

      //If the guess is too high, tell the user

      if (Guess > Secret_Number)

      {

         cout <<"Your guess is too high." << endl;

      }

 

      // If the guess is too low, tell the user

      if (Guess < Secret_Number)

      {

         cout <<"Your guess is too low." << endl;

      }

 

      //If the user guessed the right number, congratulate them

      if (Guess == Secret_Number)

      {

         cout <<"Good job! You guessed the secret number!" << endl;

      }

   } 


that's what one of my "while" loops looked like during another program i created... so something similar to that if possible. so switch System ("PAUSE") with return 0;?

thx for the help!
Replace System(PAUSE) with cin.get(). Follow the link to see why: http://www.gidnetwork.com/b-61.html

Instead of void main have main as an integer: int main. This way you can use return to give 0 for success or 1 for failure. So return 0; needs to be at the end of your program.
You could insert the while loop before line 17, which is:

cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;

You'll also want to include a check number to be a condition for the while loop (should be initialized to 1).

At the end of the loop, ask the user if they would like to enter another calculation (1 for another, 2 for exit)

Again, declare main as an int, and return 0 after the while loop.

It would look like:

1
2
3
4
5
6
7
8
9
int check = 1;
while (check == 1)
{
       cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" 
       //the rest of your program

}

return 0;
Ok, so i tweaked it a little... how does this look?? (i know there's going to be things wrong with it, so let me know what is... :)

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
//Calculator by me

//Includes the iostream library 
#include <iostream>

//Use the standard namespace
using namespace std;

	int main (	)
	{	//Declares the Variables
		float Number_1;
		float Number_2;
		float Result;
		int Which_Calculation;
int check = 1;
while (check == 1)
{
			//Give instructions
			cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
			cin >> Which_Calculation;

			//Get numbers
			cout << "Please enter the first number" << endl;
			cin >> Number_1;
			cout << "Please enter the second number" << endl;
			cin >> Number_2;

			if (Which_Calculation == 1)
		{
			//Calculate the result
			Result = Number_1 + Number_2;
		}

			if (Which_Calculation == 2)
		{
			//Calculate the result
			Result = Number_1 - Number_2;
		}

			if (Which_Calculation == 3)
		{
			//Calculate the result
			Result = Number_1 * Number_2;
		}

			if (Which_Calculation == 4)
		{
			//Calculate the result
			Result = Number_1 / Number_2;
		}
			//Print the answer is...
			cout << "The answer is..." << endl;


			//Print the result
			cout << Result << endl;

			cout << "Would you like to do another task?" << endl; // ask

			int reply; // the users reply
			cin >> reply; // get the input

			switch(reply) 
			{
				case 1: // if they typed 1...
				{
						main(); // repeat the main function again
				}
				case 2: // if they typed 2...
				{
						return 0; // exit the program.
				}
		        }
	}

		cin.get();

		return	0;
}
Last edited on
This should work. Using the reply at the end with switch would be redundent as you already have "check" which initiates the loop. Once "check" is not equal to 1 the program skips the loop, thus ending the program.

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
//Calculator by me

//Includes the iostream library
#include <iostream>

//Use the standard namespace
using namespace std;

	int main (	)
	{	//Declares the Variables
		float Number_1;
		float Number_2;
		float Result;
		int Which_Calculation;
int check = 1;
while (check == 1)
{
			//Give instructions
			cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
			cin >> Which_Calculation;

			//Get numbers
			cout << "Please enter the first number" << endl;
			cin >> Number_1;
			cout << "Please enter the second number" << endl;
			cin >> Number_2;

			if (Which_Calculation == 1)
		{
			//Calculate the result
			Result = Number_1 + Number_2;
		}

			if (Which_Calculation == 2)
		{
			//Calculate the result
			Result = Number_1 - Number_2;
		}

			if (Which_Calculation == 3)
		{
			//Calculate the result
			Result = Number_1 * Number_2;
		}

			if (Which_Calculation == 4)
		{
			//Calculate the result
			Result = Number_1 / Number_2;
		}
			//Print the answer is...
			cout << "The answer is..." << endl;


			//Print the result
			cout << Result << endl;

			cout << "Would you like to do another task? (1 for yes, 2 for no)" << endl; // ask

			cin >> check; // get the input

	}

		cin.get();

		return	0;
}
Oh dear. Redundant recursion... :)

Here are a few changes I might suggest, if I may make an input.

@Lines 63-72: Believe it or not, none of these are necessary. In fact, they kill the purpose of your while loop.

@Line 58: Maybe output some sort of indication that the user has to type 1 to repeat the loop?

@Line 60: You don't need this. You'll see why next line.

@Line 61: Try cin >> check; This way, you not only make use of that variable, but you achieve virtually the same results, and your code is shorter and better optimized for memory usage before the compiler does anything to it.

EDIT:
mookial beat me to it, but I would like to ask in the future that he/she doesn't give out code solutions. :)

-Albatross

@programmer47:
That switch is somewhat wrong, because while it seems like it would work I think you thought switch works a different way that it actually does. Just a hunch. Also, I would suggest avoiding recursion to repeat main() unless you're 100% certain you know what you're doing. Here, you seemed only 99% certain. :/
http://msdn.microsoft.com/en-us/library/k0t5wee3(VS.80).aspx
Last edited on
Albatross wrote:
EDIT: mookial beat me to it, but I would like to ask in the future that he/she doesn't give out code solutions. :)


Ahh! Sorry, I knew I shouldn't have gave the answer but I realized this a little to late. I'm a new programmer as well so I know the OP's pain =P So from one newbie to another I wanted to help.

Even so, your input is probably far better than mine.
Last edited on
Thank you all! you were a big help :)
ok, well now this is the error it is giving me.. Error 1

error C2059: syntax error : 'return' c:\users\user\recources for c++\calculator\calculator.cpp 69

Here is the code again after the changes...

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
//Calculator by me

//Includes the iostream library 
#include <iostream>

//Use the standard namespace
using namespace std;

	int main (	)
	{	//Declares the Variables
		float Number_1;
		float Number_2;
		float Result;
		int Which_Calculation;
int check = 1;
while (check == 1)
{
			//Give instructions
			cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
			cin >> Which_Calculation;

			//Get numbers
			cout << "Please enter the first number" << endl;
			cin >> Number_1;
			cout << "Please enter the second number" << endl;
			cin >> Number_2;

			if (Which_Calculation == 1)
		{
			//Calculate the result
			Result = Number_1 + Number_2;
		}

			if (Which_Calculation == 2)
		{
			//Calculate the result
			Result = Number_1 - Number_2;
		}

			if (Which_Calculation == 3)
		{
			//Calculate the result
			Result = Number_1 * Number_2;
		}

			if (Which_Calculation == 4)
		{
			//Calculate the result
			Result = Number_1 / Number_2;
		}
			//Print the answer is...
			cout << "The answer is..." << endl;


			//Print the result
			cout << Result << endl;

			cout << "Would you like to do another task? Press 1 if you would, press 2 if you would like to exit the program." << endl; // ask

			
			cin >> check; // get the input
		
		    }
	}

	
		int cin,get();

		return	0;

Last edited on
@Line 67: Doesn't something here look a bit different from your previous bit of code @Line 76?

@Line 70: You're missing something. ;)

@Line 64: You've got something extra here. Maybe it's been misplaced?

-Albatross
Last edited on
yes, i changed it to a "," because the computer said that "." was an error, and that i needed a ","

i wondered. haha. hmm...
i think i deleted something or did something to that line by accident...
That int wasn't there either... <_<

-Albatross
yeah, i just took it off again... and returned all those lines back to normal... it looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Would you like to do another task? Press 1 if you would, press 2 if you would like to exit the program." << endl; // ask

			
			cin >> check; // get the input
		
		    }
	}

	
		cin.get();

		return	0;

}


and when i debugged it, here are the errors.

Error 4 error C2059: syntax error : '}' 71
Error 6 error C2059: syntax error : '}' 71
Error 3 error C2059: syntax error : 'return' 69
Error 1 error C2143: syntax error : missing ';' before '.' 67
Error 5 error C2143: syntax error : missing ';' before '}' 71
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 67
Last edited on
Count your brackets. This is a tiny little thing, but it can wreak major headaches if you have one } too many somewhere.

-Albatross
ok...
Pages: 12