while loop help

Hey I'm having trouble with figuring out while loops for my homework. I suppose to prompt a user for a number for height between 3 and 10 (inclusive), and a number for width between the height and 20. If the user enters a number outside the criteria I'm suppose to use a while loop to prompt them to input the a correct number. This is the code I came up with but when I run it, it prompts for the height and width but if out of bounds it doesn't prompt to repeat. Not sure what I am doing wrong and any help is much appreciated.



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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void main()
{
	//define variables
	int Width = 0;
	int Height = 0;

	//prompt user for height
	cout << "Enter a box height(between 3 and 10):";
	cin >> Height;

	//prompt user fpr width
	cout << "Enter a box width between (6 and 20): ";
	cin >> Width;

	//prompt user to repeat height if out of bounds
	while (Height < 3 && Height > 10)
	{
		if (Height < 3 && Height > 10)
		{
			cout << "That number is out of bounds: Try again: ";
			cin >> Height;
		}
	}

	//prompt user to repeat width if out of bounds
	while (Width <= Height && 20 >= Width)
		{
			if (Width <= Height && 20 <= Width)
			{
				cout << "That number is out of bounds: Try again: ";
				cin >> Width;
			}
		}
} 

Last edited on
Enter a box width between (6 and 20):

I thought ,
for width between the height and 20.


Since the width depends on height, you should consider nesting the while loops.
Tried changing it to nest them but still getting same output, asks for the number but doesn't prompt to repeat if out of bounds

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void main()
{
	//define variables
	int Width = 0;
	int Height = 0;

	//prompt user for height
	cout << "Enter a box height(between 3 and 10):";
	cin >> Height;

	//prompt user fpr width
	cout << "Enter a box width (between height and 20): ";
	cin >> Width;

	//prompt user to repeat height andd width if out of bounds
	while (Height < 3 && Height > 10)
	{
		if (Height < 3 && Height > 10)
		{
			cout << "That number is out of bounds: Try again: ";
			cin >> Height;
		
			if (Width <= Height && 20 <= Width)
			{
				cout << "That number is out of bounds: Try again: ";
				cin >> Width;
			}
		}
	}
} 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
	int height = 0;
	int width = 0;
	
	while(height  < 3 || height > 10)
	{
		cout<<"Enter heigth (>= 3 and <= 10): ";
		cin>>height;
	}
	cout<<endl;
	while(width < height || width > 20)
	{
		cout<<"Enter width: (>= "<<height<<" and <= 20: ";
		cin>>width;
	}
	
	cout<<"Height  = "<<height<<" and widht is "<<width<<endl;
	cin.ignore();
	return 0;
}
Hey thanks man that works pretty well. Gonna work on it some more match the homework requirements
Topic archived. No new replies allowed.