do-while loop not looping

The code runs fine in regard to the functions and calculations. However, it doesn't seem to be doing the 'while' part of the loop which asks the user if they wish to try another calculation. The code is broken down into a header file and two .cpp files (functions and driver).

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//Header.h
#include <iostream>
#include <string>
#include <math.h>

void WriteHeader ();   //function Prototype
int WriteMenu ();
double CalcTri (double triHeight, double triLength);
double CalcRec (double recLength, double recWidth);
double CalcConeVolume (double coneRadius, double coneHeight);
double CalcSurfAre (double coneRadius, double coneHeight);
void WriteResults (char UserChoice, double input1, double input2, double result);
void WriteResults (double height, double radius, double volume, double SurfArea);

//Functions.cpp

#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;

#include "Header.h"

//Writes Output to Screen
void WriteHeader(){
cout << "This program allows the user to enter the dimensions needed to calculate certain formulas." << endl;
cout << "Choose an option from the menu, enter dimensions in inches and follow instructions to obtain formula answers. \n" << endl;
}
int WriteMenu (){
     char UserChoice;
     cout << "1. Calculate the hypotenuse of a right angled triangel." << endl;
     cout << "2. Calculate the area of a rectangle." << endl;
     cout << "3. Calculate the volume and total surface area of a cone. \n" 
     <<  endl;
     cout << "Please enter the option of which you would like to calculate (1,
     2, or 3): ";
     cin >> UserChoice;
     return UserChoice;
}
//Do the calculation
//Hypotenuse of right triangle
double CalcTri (double triHeight, double triLength){
     double hypotenuse = 0.0;
     hypotenuse = sqrt((triHeight * triHeight) + (triLength * triLength));
     return hypotenuse;
}
//Area of a rectangle
double CalcRec (double recLength, double recWidth){
     double area = 0.0;
     area = recLength * recWidth;
     return area;
}
//Volume of a Cone
double CalcConeVolume (double coneRadius, double coneHeight){
     double volume = 0.0;
     const double pi = 3.14159265;
     volume = (1.0 / 3.0) * pi * coneRadius * coneRadius * coneHeight;
     return volume; 
}
//Surface Area of a Cone
double CalcSurfAre (double coneRadius, double coneHeight){
     double SurfaceArea = 0.0;
     const double pi = 3.14159265;
     double length = 0.0;
     length = sqrt((coneRadius * coneRadius) + (coneHeight * coneHeight));
     SurfaceArea = (pi * (coneRadius * coneRadius)) + (pi * coneRadius * 
     length);
     return SurfaceArea;
}
void WriteResults (char UserChoice, double input1, double input2, double result){
//if user chose to calculate hypotenuse
     if (UserChoice == '1') {
	cout << "\nYou enterd the height of the triangle to be ";
	cout << input1 << " inches, \nand the length to be " << input2 << 
        " inches.\n" << endl;
	cout << "The hypotenuse of the right triangle is: ";
	cout << result << " inches" << endl;
	}
	//if user chose to calculate area
     else if (UserChoice == '2'){
	cout << "\nYou enterd the length of the rectangle to be ";
	cout << input1 << " inches, \nand the width to be " << input2 << 
        " inches.\n" << endl;
	cout << "The area of the rectangle is: ";
	cout << result << " square inches" << endl;
	}
}
//if user chose to calculate volume and surface area
void WriteResults (double height, double radius, double volume, double SurfArea)
{
     cout << "\nYou enterd the height of the cone to be ";
     cout << height << " inches, \nand the radius to be " << radius << 
     " inches.\n" << endl;
     cout << "The volume of the cone is: ";
     cout << volume << " cubic inches \n" << endl;
     cout << "The surface area of the cone is: ";
     cout << SurfArea << " square inches" << endl;
}

//Driver.cpp

#include <iostream>     //needed for cout and cin
#include <string>
#include <math.h>
#include <stdlib.h>
#include "Header.h"

using namespace std; 

int main ()
{
//declarations
char UserChoice; //char or int
double triHeight = 0.0, triLength = 0.0, recLength = 0.0, recWidth = 0.0; 
double coneRadius = 0.0, coneHeight = 0.0;
double hypotenuse = 0.0, area = 0.0, volume = 0.0, SurfArea = 0.0;
std::string answer; 
	
//Call our function
WriteHeader();
//do while loop
do {
   UserChoice = WriteMenu ();
		
   do{
     if (UserChoice == '1'){
	cout << "\nWhat is the height of the triangle (example: 4.5)? ";
	cin >> triHeight;

	cout << "What is the length of the triangle (example: 4.5)? ";
	cin >> triLength;
	cin.ignore();
	hypotenuse = CalcTri (triHeight, triLength);
	WriteResults (UserChoice, triHeight, triLength, 
        hypotenuse);				
	}
     else if (UserChoice == '2'){
	cout << "What is the length of the rectangle (example: 4.5)? ";
	cin >> recLength;
	cout << "What is the width of the rectangle (example: 4.5)? ";
	cin >> recWidth;
	cin.ignore();
	area = CalcRec (recLength, recWidth);
	WriteResults (UserChoice, recLength, recWidth, area);
	}
     else if (UserChoice == '3'){		{
	cout << "What is the radius of the cone (example: 4.5)? ";
	cin >> coneRadius;
	cout << "What is the height of the cone (example: 4.5)? ";
	cin >> coneHeight;
	cin.ignore();
	volume = CalcConeVolume (coneRadius, coneHeight);
	SurfArea = CalcSurfAre (coneRadius, coneHeight);
	WriteResults (coneRadius, coneHeight, volume, SurfArea);
	}
     else{
	cout << "You entered an incorrect choice, please choose again: " <<
        endl;
	UserChoice = WriteMenu();
	}	
    }while (UserChoice != '1' || UserChoice != '2' || UserChoice != '3');
		
    cout << "\nDo you want to play another round (yes/no) ==> ";
    getline(cin,answer);
    system ("CLEAR");
} while (answer == "yes" || answer == "YES" || answer == "Yes"); 
cout << "Thank you for coming to us for your needs! GOOD BYE!" << endl;

return 0;
}
Last edited on
Maybe try cin.get(); before getline or cout. And initialize answer with example "no" , or something.
Last edited on
Thank you, but that didn't work. The problem is that after it writes the result it asks the user for the same input it did before.

For example, if the user chooses to calculate the area of a rectangle (UserChoice 2) it will ask for your parameters then it will solve. It is then SUPPOSED to ask if they wish to try another option, if yes do loop again and if no display a goodbye message. However, what it's doing is once solved it automatically asks the user for the parameters again (the same ones for the option chosen at the beginning).
The statement on line 161 is always true. Use && instead of ||.
Nope, that didn't work either. When I add && it just runs through the rest of the program to the end. So still no looping.
That did work, but it's not the only problem.

Remove line 159 and move line 123 after line 125.

On line 164 you use getline(...) (which seems to be unnecessary). Be aware that the operator>> leaves a new line in the stream and hence getline(...) might fetch an empty line.
So either use ignore(...) after line 37 or use the operator>> on line 164 as well.
Topic archived. No new replies allowed.