Need Teachers = )

I'm having problem with looping . I don't quite know how to loop this calculator program . Need help .

This is what i've done with the calculator program so far

void main ( )

{

// Declare the variables

float Number_1;

float Number_2;

float Result;

int Which_Calculation = 1;
// Give Instructions

cout << "Press 1 to Add , 2 to subtract , 3 to multiply , 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");



}
Hi, first off you should really be using code tags to make your code much easier to read. Put these either side of your code: [code][/code]

Also what exactly are you trying to do? Make the program continuously repeat until the user decides to close?

cout << "Press 1 to Add , 2 to subtract , 3 to multiply , 4 to divide" << endl;

I this line tell the user to press whatever key to exit. Then have an if statement checking if the key was pressed and if it was, change a bool variable from false to true. Have a do while statement around most of the code, the condition of which is while variable == false.

Have a read up on this as well:
http://www.cplusplus.com/doc/tutorial/control/
/Yeah make the program repeats continuously . Umm code tags ?i'm currently using microsoft visual C++ 2008 . it is the same rite ? .. Thanks = )
Ah sorry I should have explained better. Put these around your code when you are posting to the forums. It lets the website know that it should be code, and it displays it in a way that is much easier to read.

[code]code goes here[/code]

Did the rest of what I said make sense to you?
umm i still can't really understand how i can do a while-loop . what do i need to declare and what do i need to write in ' while () ' ?
It is probably a 'do while' loop that you want instead of a while loop, the difference between the two, is that a do while will always run at least once. It can still be very easily do with a while loop though. Have a look at this though, it explains a lot of the different control structures and things, and shows you the correct layout of them.

http://www.dreamincode.net/downloads/ref_sheets/cpp_reference_sheet.pdf

Again, if you still can't work out how to do a do while loop let me know.
Oh i have to use a while loop cuz it is part of the assignment that i was given . Umm if i were to make the program say "Press 1 to continue , 2 to stop ".

how do i make 1 to repeat and 2 to stop ?

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Would you like to do another task ? (Type 1 for yes , 2 for no)" << endl;

				cin >> Decision;

				if (Decision == Decision_1)

						
			if (Decision == Decision_2)
			{
				cout << "Thank you ." << endl;
				
				system ("PAUSE");
			}


what should i write onif (Decision == Decision_1) ?
you could have declared a bool called some thing along the lines of 'exitProgram' set it as false while you declare it and have then within your if statement, exitProgram = true. Then in your while loop have the condition exitProgram == false. Then when exitProgram is set to true, your program will be able to get past the end of the while loop.

Make sense?
umm could you write an example for me ? .Oh thank you for your patience by the way >o< .
Yea no problem.

Should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	bool exitLoop = false;
	
	while(exitLoop == false){
		int decision;
		::std::cout << "do you want to exit the loop? press 1 to exit, 2 to loop" << ::std::endl;
		::std::cin >> decision;
		
		if(decision == true){
			exitLoop = true;
		}else{
			//can put stuff here, but for this example I don't need to
		}		
	}
	
	return 0;
}


I haven't compiled it so I might have missed a detail here or there, but that should give you an idea.
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Calculator, by Your Name

 

// Include the iostream library

#include <iostream>

 

//Use the standard namespace

using namespace std;

 

int main ( )

{
	

			   // Declare the variables
				
				

							

			   float Number_1;

			   float Number_2;

			   float Result;

				int Decision;	

			   int Decision_1 = 1;

			   int Decision_2 = 2; 

			   int Which_Calculation = 1;
				// Give Instructions
				bool exitProgram = false;

				while (exitProgram == false)
				{


			   cout << "Press 1 to Add , 2 to subtract , 3 to multiply , 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 ? (Type 1 for yes , 2 for no)" << endl;

					cin >> Decision;

					if (Decision == false)
					{
						exitProgram = false;
						
					}

					else 
					{ 
						exitProgram = true;
						cout << "Thank you." << endl;
						system ("PAUSE");
					}
			
			
			

				}
}



umm this is what i did so far , can you take a look at it and guide me through ?
Most of that seems to be right. Probably the main thing you've done wrong is your if and else statements at the end.

You have if (Decision == false) however you had told the user to input 1 or 2. so if should be:

if (Decision == 2)

Although you don't actually need that bit there at all. i would just do:
1
2
3
4
5
6
7
	cout << "Would you like to do another task ? (Type 1 for yes , 2 for no)" << endl;
	cin >> Decision;

	if (Decision == 1)
	{
		exitProgram = true;
	}


and put the thank you part at the very end of your main function.

You really should try and improve your use of white space as well.

Read this wikipedia article. And as a beginner you probably think it's just me being awkward and that it doesn't really matter. But as you start to write more complicated code, it's the difference between the code being easy to understand, and even you yourself not having a clue what is going on.

read it.

http://en.wikipedia.org/wiki/Indent_style

EDIT: also there are several different styles in that article, it pretty much doesn't matter which you choose, just pick one that looks good to you and be consistent through out each program.
Last edited on
Oh thanks alot ! i think i'm done with 1 last question . how do i make it close after system("CLOSE)" ? cuz it loops back either way .
Last edited on
I have never used system("CLOSE)" before and can't say I know anything about it. Generally any of the system functions are considered bad practice so it would maybe be best to not use it. Once the program gets to the end of your main function it should just finish. If it doesn't it will be something strange with your compiler or settings, not actually c++. I'd probably not be able to help, but if your still not sure, explain the problem as best you can and hopefully someone will help out.
Oh i'm so SRY ! i meant system("PAUSE") . it doesn't end after that .
Here is a little cleaner way to write it. You should really work on practicing error correcting too because it helps go a long way when you write real programs and better grades in class. That is why I used the while statements in some of your choices. It isn't perfected, I leave that for you to work on but it is a great start. This is also an important feature of a while statement you can never overlook. Here, this while statement naturally would go on forever but if you use break, you can exit anytime. This is great for larger programs where you have numerous reasons to break a while statement and one variable is too much of a hassle to use. Also the best reason, it is one variable less to have in your program which means a smaller program and also less chances of mistakes later. Here is how it would look.

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
#include <iostream>
using namespace std;

int main()
{
	while(true)
	{
		float Number_1, Number_2, Result;
		int Decision = 0, Decision_2 = 0, Decision_2 = 0, Which_Calculation = 0;

		while(Which_Calculation < 1 || Which_Calculation > 4)
		{
			cout << "Press 1 to Add , 2 to subtract , 3 to multiply , 4 to divide" << endl;
			cin >> Which_Calculation;
		}

		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 << Result << endl;

		cout << "Would you like to do another task ? (Type 1 for yes , 2 for no)" << endl;
		cin >> Decision;

		if (Decision == 2)
		{
			cout << "Thank you." << endl;
			system ("PAUSE");
			break;
		}
	}
}


I also agree not to use the system command when possible. Try either getch() or getchar(). Either one will work and will work on non windows operating systems.
Thanks WilliamW1979 And TheMeerkat ! You guys helped alot !
Topic archived. No new replies allowed.