While loop question

I'm using some functions to pass around the value string shape is holding, I had my if statement checking to see if the valid input was entered in to the loop to break out of it. Before I moved the if statement up, my program was still going in to the while loop if I were to enter in circle, and then if I entered in circle again it would then break out.

But what I don't understand is why it was falling through like that?


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
#include <iostream>
#include <conio.h>
#include <cmath>
#include <string>

using namespace std;

void welcome_message() {

	cout << "Hello user, this program is designed to allow you to enter in the name of a shape" << endl;
	cout << "Once you enter in the name, you will be prompted to enter in the dimensions. " << endl;
	cout << "After this we will calculate and display area and circumfrance for a cricle, " << endl;
	cout << "volume and surface area for a cylinder, and for a rectangle area and perimeter." << endl;
}
void user_prompt(string &shape);
void calculations(string shape);

int main() {
	string shape;

	welcome_message(); //Calls welcome message
	user_prompt(shape); //Calls userprompt
	calculations(shape); // Calls calculations
	_getch();

	return 0;
}

void user_prompt(string &shape) { //Prompts user to enter in a shape, and then error checks the input for valid responses
	cout << "Please enter in a shape: (rectange, circle, cylinder) " << endl;
	getline(cin, shape);

	while (shape != "rectangle" || shape != "Rectangle" || shape != "circle" || shape != "Circle" || shape != "cylinder" || shape != "Cylinder") { //Checks for valid input
		
		if (shape == "rectangle" || shape == "Rectangle" || shape == "circle" || shape == "Circle" || shape == "cylinder" || shape == "Cylinder") {

			break;
		}
		
		cout << "You entered in: " << shape << " Please enter in valid input" << endl;
		getline(cin, shape);
		}

}

void calculations(string shape) {
	

	cout << shape << endl; //Here I'm checking to see if shape is passed to this function


}



Here is what I had before in user_prompt()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void user_prompt(string &shape) { //Prompts user to enter in a shape, and then error checks the input for valid responses
	cout << "Please enter in a shape: (rectange, circle, cylinder) " << endl;
	getline(cin, shape);

	while (shape != "rectangle" || shape != "Rectangle" || shape != "circle" || shape != "Circle" || shape != "cylinder" || shape != "Cylinder") { //Checks for valid inp
		
		cout << "You entered in: " << shape << " Please enter in valid input" << endl;
		getline(cin, shape);
		
		if (shape == "rectangle" || shape == "Rectangle" || shape == "circle" || shape == "Circle" || shape == "cylinder" || shape == "Cylinder") {

			break;
		}
	
	}


Any input would be greatly appreciated , even though it "works" as of now, I'm not sure if this is correct? Thank you.
Last edited on
The || operator returns true if at least one of the operands are true. For shape != "rectangle" || shape != "Rectangle" to be false shape would have to be equal to "rectangle" and "Rectangle" at the same time. shape can't have more than one value at the same time so the condition will always evaluate to true. What you probably want is && instead of ||.

I don't think you'll need an if statement to break out of the loop. If you write the loop condition correctly that should be enough.
Last edited on
If "break" is reached this immediately ends the while loop.
Knowing that, consider what happens if shape is "rectangle".

1
2
3
4
5
6
7
while ("rectangle" != "rectangle" || "rectangle" != "Rectangle") // while (false OR true) = while (true) so we proceed into the loop.
{
	if ("rectangle" == "rectangle" || "rectangle" == "Rectangle") // if (true OR false) = if (true) so we enter the if-statement and break out of the loop.
	{
		break;
	}
}


I think maybe you want to consider to negate all your comparisons.

Now consider the shape "banana"
1
2
3
4
5
6
7
8
while ("banana" != "rectangle" || "banana" != "Rectangle") // while (true OR true) = while (true) so we proceed into the loop.
{
	if ("banana" == "rectangle" || "banana" == "Rectangle") // if (false OR false) = if (false) so we skip the if-statement and try to cout for "banana".
	{
		break;
	}
	cout << "You entered in: " << shape << " Please enter in valid input" << endl;
}
Last edited on
Here is what my while statement looks like now:

1
2
3
4
while (shape != "rectangle" && shape != "Rectangle" && shape != "circle" && shape != "Circle" && shape != "cylinder" && shape != "Cylinder") { //Checks for valid input
		cout << "You entered in: " << shape << " Please enter in valid input" << endl;
		getline(cin, shape);
		}


How would I have it break out though of the statement if the conditions are proven to be true without that if statement?
Maybe it would be easier to set up a flag that would trip you out of the loop if it was triggered

do
{
getline(cin, shape);

if(shape == (conditions));
flag = true;
}while(flag == false)





muffsez wrote:
How would I have it break out though of the statement if the conditions are proven to be true without that if statement?

The loop will stop when the loop condition is false (i.e. when shape is equal to "rectangle", "Rectangle", "circle", "Circle", "cylinder" or "Cylinder"). Isn't that what you want?
Peter97: Yes, thank you. Sorry I've been up all night working on a few different things.

1
2
3
4
while (shape != "rectangle" && shape != "Rectangle" && shape != "circle" && shape != "Circle" && shape != "cylinder" && shape != "Cylinder") { //Checks for valid input
		cout << "You entered in: " << shape << " Please enter in valid input" << endl;
		getline(cin, shape);
	}



This is what I have it at now, after checking it a few different times it seems to be working as intended. Thank you for your input.

Nico: I see what you're saying.
Ken: Possibly, however I haven't used a flag before.
You could make a simple function to check the input.
1
2
3
4
5
6
7
bool inputCheck(string input)
{
	if (input =="rectangle") return true;
	if (input =="Rectangle") return true;
	// all your other valid options return true
	return false;
}


1
2
3
4
5
while (inputCheck(shape)) 
{
	cout << "You entered in: " << shape << " Please enter in valid input" << endl;
	getline(cin, shape);
}
Here is my finished product, everything appears to be working perfectly. I just have to check the math at some point.

Nico: I've never used a bool before, so for that input check: I would have to do other if statements to check for the input of "circle" and "Circle" as well too correct?

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
#include <iostream>
#include <conio.h>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;

const double PI = 3.14;

void welcome_message();
void user_prompt(string &shape);
void calculations(string shape);

int main() {
	string shape;

	welcome_message(); //Calls welcome message
	user_prompt(shape); //Calls userprompt
	calculations(shape); // Calls calculations
	_getch();

	return 0;
}

void welcome_message() {

	cout << "Hello user, this program is designed to allow you to enter in the name of a shape" << endl;
	cout << "Once you enter in the name, you will be prompted to enter in the dimensions. " << endl;
	cout << "After this we will calculate and display area and circumfrance for a cricle, " << endl;
	cout << "volume and surface area for a cylinder, and for a rectangle area and perimeter." << endl;
}

void user_prompt(string &shape) { //Prompts user to enter in a shape, and then error checks the input for valid responses
	cout << "Please enter in a shape: (rectange, circle, cylinder) " << endl;
	getline(cin, shape);

	while (shape != "rectangle" && shape != "Rectangle" && shape != "circle" && shape != "Circle" && shape != "cylinder" && shape != "Cylinder") { //Checks for valid input
		cout << "You entered in: " << shape << " Please enter in valid input" << endl;
		getline(cin, shape);
	}
}

void calculations(string shape) {
	
	if (shape == "Rectangle" || shape == "rectangle") { //If Rectangle
		cout << "What is the length of your " << shape << "?" << endl;
		double length;
		cin >> length;
		cout << "What is the width of your " << shape << "?" << endl;
		double width;
		cin >> width;

		double area, perimeter;

		area = (length * width); //Area calculation
		perimeter = 2 * (length + width); //perimteter calculation

		cout << "The area of your " << shape << " is: " << showpoint << setprecision(2) << area << endl; //Area display
		cout << "The perimeter of your " << shape << " is: " << showpoint << setprecision(2) << perimeter << endl; //Perimeter display
	}

	if (shape == "circle" || shape == "Circle") { //If circle

		cout << "What is the radius of your " << shape << "?" << endl;
		double radius;
		cin >> radius;

		double circumference;
		circumference = 2 * PI * radius; //Circumference calculation
		double area;
		area = PI * pow(radius, 2); //Area calculation

		cout << "The circumference of your " << shape << " is: " << showpoint << setprecision(2) << circumference << endl; //Circumference display
		cout << "The area of your " << shape << " is: " << showpoint << setprecision(2) << area << endl; //Area display

	}

	if (shape == "cylinder" || shape == "Cylinder") { //If cylinder

		cout << "What is the radius of your " << shape << "?" << endl;
		double radiusCyl;
		cin >> radiusCyl;
		cout << "What is the height of your " << shape << "?" << endl;
		double height;
		cin >> height;
		
		double volume;
		volume = PI * pow(radiusCyl, 2) * height; //Volume calculation
		double surfaceArea;
		surfaceArea = 2 * PI * radiusCyl * height + 2 * PI * pow(radiusCyl, 2); //Surface Area calculation

		cout << "The volume of your " << shape << " is " << showpoint << setprecision(2) << volume << endl; //Volume display
		cout << "The surface area of your" << shape << " is " << showpoint << setprecision(2) << surfaceArea << endl; //Surface Area display

	}
}
You are using a lot of booleans, but you don't see them.
A bool is variable that is either TRUE or FALSE, no other options.

1
2
bool varibleName = (shape == "cylinder" || shape == "Cylinder");
if (varibleName) 

is equal to
 
if (shape == "cylinder" || shape == "Cylinder")


except for the fact that in the first option you can use the variable name in other places, while with the second one you would keep performing the same comparison over and over again.

That being said, yes if you wanted to add more criteria you would just copy the previous line and change the text that it was compared to.

Also, maybe it is easier to google for the "tolower" function. It might save you a lot of work and clean up your code quite a lot if you modify every capital from the input to a lower case letter.
In addition, then you would also be able to properly respond to input like:
RECTANGLE, ReCtAnGlE, rECTANGLE and so on.
That makes sense, I was thinking about that while I was writing it seeing as that it would only check for to different variations of what the user would enter. I'll definitely have to look into that and implement it into this, and if not this then for sure in the future.

Thank you for all of your help.
Topic archived. No new replies allowed.