Do while loop problem

I made this for class, and my do while statement is not functioning properly.
Could someone please take a look and advise me as to where I went wrong?

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
/*************
Program:	Assignment 9
Author: 	Alan P. Matie                      
Date:       20 Mar 2009
Description: 
Write a program that does simple mathematics.
It must:
1. Ask for an operation 
2. Ask for two numbers
3. Perform the calculation specified on the numbers
4. Check for division by zero and display an error message if encountered
5. Use a switch statement for the determination of mathematical operation and the operation itself
6. Use a post-test loop that stops upon the user entering ‘Q’
7. Displays beginning and ending messages printed only once each - before and after the loop

New Concepts: Switch
Challenges:
Last Modified: 24 Mar 2009
**************/

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants

// Function Prototypes


int main()
{
	// Declare variables below here
	double x, z, result;
	char operand;

	
	// Initialization Section for real number output. DO NOT MOVE!
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);
	// Begin your "main processing" below here
	cout <<"Welcome to the mathematical selector program!"<<endl;
	do
	{
		cout <<"Please enter the operand of the problem you would like to solve:"<<endl;
		cout <<"+ for addition"<<endl;
		cout <<"- for subtraction"<<endl;
		cout <<"* for multiplication"<<endl;
		cout <<"/ for division"<<endl;
		cout <<"Enter Q to quit"<<endl;
		cout <<"Enter your choice ==> ";
		cin>> operand;
		cout <<"Please enter the two numbers ==> ";
		cin >> x >> z;
	
		switch (operand)
		{
			case '+':
				result = x+z;
				cout <<"The answer is: " << result <<endl;
				
			break;
			case '-':
				result = x-z;
				cout <<"The answer is: " << result <<endl;
			break;
			case '*':
				result = x*z;
				cout <<"The answer is: " << result <<endl;
			break;
			case '/':
				if (z ==0 )
				{
					cout <<"That is an invalid operation" <<endl;
				}
				else
				{
				result = x/z;
				cout <<"The answer is: " << result <<endl;
				}
			break;
			default :
				cout <<"That is an invalid operation" <<endl;
			break;
		}
		
	}while (operand != 'Q');
	cout <<"End of Program!"<<endl;
		
	
	return 0;
	
}

// function definitions below here 

Last edited on
There is no syntactical error in your code. The undesired results that you are seeing stem from the fact that you
input operand at the beginning of the do while loop and you check whether or not to exit at the end.
Regardless of whether or not that input is 'Q', the rest of the code in the loop will continue executing. If you
would like the loop to exit immediately, you need to modify or rearrange your code.

Also, please use [code][/code] blocks to post your code in the future. It makes it much easier to read.
Last edited on
Thanks for the response, although I don't know where/how to rearrange the code to make this end on 'Q'. Also I apologize for not using code blocks, but I honestly don't know what you are talking about; would you be willing to elaborate for me, so that I can post properly next time? Also I've noticed other people posting here and keeping their original indenting, and I was unable to do so, do I need to post in a method other than copy/paste; or manually redo all indenting?
Also my loop is required to be a post-test loop, unless I misread my textbook then that means a do-while; correct?
Last edited on
when ever you post any code select that code and press '#' on the right side, this will make it code block and it will look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants

// Function Prototypes


int main()
{
// Declare variables below here
double x, z, result;
char operand;


// Initialization Section for real number output. DO NOT MOVE!
cout <<setiosflags(ios::fixed | ios::showpoint);
cout <<setprecision(2);
Thank you, I went back and edited my original post; it does make it much easier to read.
You could ask the user to continue at the end of the loop and get a character from them. If the character is 'Q' then the loop will exit.

Yes post-test means do-while().
ran the program it seems to run fine as it is now even if you choose Q as your choice right off you have to enter 2 numbers before the do while loop works.

one solution is just prompt the user in the case statements to enter the number
do this for each case statement in your switch statement. Also as a case statement for the Quit option. Here is your program with the changes i described above

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
// test.cpp : Defines the entry point for the console application.
//

/*************
Program:	Assignment 9
Author: 	Alan P. Matie                      
Date:       20 Mar 2009
Description: 
Write a program that does simple mathematics.
It must:
1. Ask for an operation 
2. Ask for two numbers
3. Perform the calculation specified on the numbers
4. Check for division by zero and display an error message if encountered
5. Use a switch statement for the determination of mathematical operation and the operation itself
6. Use a post-test loop that stops upon the user entering ‘Q’
7. Displays beginning and ending messages printed only once each - before and after the loop

New Concepts: Switch
Challenges:
Last Modified: 24 Mar 2009
**************/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants

// Function Prototypes


int main()
{
	// Declare variables below here
	double x, z, result;
	char operand;

	
	// Initialization Section for real number output. DO NOT MOVE!
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);
	// Begin your "main processing" below here
	cout <<"Welcome to the mathematical selector program!"<<endl;
	do
	{
		cout <<"Please enter the operand of the problem you would like to solve:"<<endl;
		cout <<"+ for addition"<<endl;
		cout <<"- for subtraction"<<endl;
		cout <<"* for multiplication"<<endl;
		cout <<"/ for division"<<endl;
		cout <<"Enter Q to quit"<<endl;
		cout <<"Enter your choice ==> ";
		cin>> operand;
		
	
		switch (operand)
		{
                        case 'Q':
                          break;
			case '+':
				cout <<"Please enter the two numbers ==> ";
		                cin >> x >> z;
				result = x+z;
				cout <<"The answer is: " << result <<endl;
				
			break;
			case '-':
				cout <<"Please enter the two numbers ==> ";
		                cin >> x >> z;
				result = x-z;
				cout <<"The answer is: " << result <<endl;
			break;
			case '*':
				cout <<"Please enter the two numbers ==> ";
		                cin >> x >> z;
				result = x*z;
				cout <<"The answer is: " << result <<endl;
			break;
			case '/':
				cout <<"Please enter the two numbers ==> ";
		                 cin >> x >> z;
				if (z ==0 )
				{
					cout <<"That is an invalid operation" <<endl;
				}
				else
				{
				result = x/z;
				cout <<"The answer is: " << result <<endl;
				}
			break;
			default :
				cout <<"That is an invalid operation" <<endl;
			break;
		}
		
	}while (operand != 'Q');
	cout <<"End of Program!"<<endl;
		
	
	return 0;
	
}

// function definitions below here  

Last edited on
Thanks for all the replies, I really appreciate all the help.
I recieved a 19 out of 20 points, thanks for the input everyone!!
BTW I posted my instructors comments below:


Ending problem - two problems:

you ask for the numbers when you might have a Q; you have to ask for the numbers in the case
you do not have a case for the Q, so the default gives an error message

Topic archived. No new replies allowed.