input validation

Hi,

I need help with my code, I should create a function that gets input from the user for 3 different pizza sizes (in inches), and their price accordingly. I should validate the user input, therefore only valid price and size will be accepted. Can you please check my code and let me know if I'm on a right track?

Thank you,

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

void userInputSize(double&, double&, double&);
void userInputPrice(double&, double&, double&);

int main(){
	
	double sSize,
		mSize,
		lSize,
		sPrice,
		mPrice,
		lPrice;
	userInputSize(sSize, mSize, lSize);
	userInputPrice(sPrice, mPrice, lPrice);
	
	system("pause");
	return 0;
}
void userInputSize(double&small, double& medium, double& large){

	do {
		cout << "Enter the size of the small pizza (in inches):" << endl;
		cin >> small;
		cout << "Enter the size of the medium pizza (in inches):" << endl;
		cin >> medium;
		cout << "Enter the size of the large pizza (in inches):" << endl;
		cin >> large;
	} while ((small >= 5 && small <= 7) || (medium >= 8 && medium <= 10) || (large > 11 && large <= 13));

}

void userInputPrice(double&small, double&medium, double&large){
	do{
		cout << "Enter the price of the pizza" << endl;
		cin >> small;
		cout << "Enter the price of the pizza" << endl;
		cin >> medium;
		cout << "Enter the price of the pizza" << endl;
		cin >> large;
	} while ((small = 12.90) || (medium = 13.40) || (large = 27.80));
}

Last edited on
Hey, could you please edit your code and use code tags? They look like this <> and are under the format section. Mark all of your code, and then press that icon.


1
2
void userInputSize(double&, double&, double&);
void userInputPrice(double&, double&, double&);

These 2 are called function prototypes. They should not be inside main. Move them to above main.
Here you go, thanks,
There could be some improvements. I copied the code over and tried it out. I couldnt finish your program. You never tell the user what to do.

For example. Look at your userInputPrice. How is anyone ever support to know what to put exactly? You just say enter the price of the pizza, but never say if its a small pizza, medium or large. And you also never provide a menu so we can see what each cost. Also. In c++,

this "=" is an assignment operator. You use it to assign things, such as int a = b;
in your while loop

while ((small = 12.90) || (medium = 13.40) || (large = 27.80));
you're trying to check if something is equal to something else, so you want to use the double equal sign, "=="

while ((small == 12.90) || (medium == 13.40) || (large == 27.80));

Lets talk about your other function, userInputSize. The while loop.

((small >= 5 && small <= 7) || (medium >= 8 && medium <= 10) || (large > 11 && large <= 13));

This is saying, that I can enter the small, medium and the larges inches as "1". Becuase, you're saying run this loop as l ong as small is between 5 and 7, medium is between 8 and 10, and large is between 11 and 13, meaning if I just type the number 1 or 2 or 3 etc it will work, thats not what you want is it?

Edit: oh and about the while loop in the userInputPrice function. Currently (if you change the = to ==) it will exit the loop once only one of the prices are correct. So meaning, if they input the small pizza's price correctly, and the other 2 they go balls to the walls, it will still give them a pass, which is not what you want I assume.
Last edited on
Thank you for your help.
Does it look better now?
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
#include <iostream>
using namespace std;

void userInputSize(double&, double&, double&);
void userInputPrice(double&, double&, double&);

int main(){
	
	double sSize,
		mSize,
		lSize,
		sPrice,
		mPrice,
		lPrice;
	userInputSize(sSize, mSize, lSize);
	userInputPrice(sPrice, mPrice, lPrice);
	
	system("pause");
	return 0;
}
void userInputSize(double&small, double& medium, double& large){

	do {
		cout << "Enter the size of the small pizza (in inches):" << endl;
		cin >> small;
		cout << "Enter the size of the medium pizza (in inches):" << endl;
		cin >> medium;
		cout << "Enter the size of the large pizza (in inches):" << endl;
		cin >> large;
	} while ((small >= 5 && small <= 7) && (medium >= 8 && medium <= 10) && (large >= 11 && large <= 13));

}

void userInputPrice(double&small, double&medium, double&large){
	do{
		cout << "Enter the price of the small pizza" << endl;
		cin >> small;
		cout << "Enter the price of the medium pizza" << endl;
		cin >> medium;
		cout << "Enter the price of the large pizza" << endl;
		cin >> large;
	} while ((small == 12.90) && (medium == 13.40) && (large == 27.80));
}
Should I create a menu in my main function?
I think you still got the problem with the while loop in userInputSize.

while ((small >= 5 && small <= 7) && (medium >= 8 && medium <= 10) && (large >= 11 && large <= 13));

You havent really told me how you want it but this is how I assume you want it. Right now, I can still enter both the small pizzas inches as 2, and the large pizzas inches as 2, which makes no sense right? You're telling the loop, to keep running if they answer 5-7, 8-10, and 11-13. But what I assume you want is, that the correct inches are 5-7, 8-10, and 11-13. rather than the wrong ones right? because right now they are the wrong ones, while everything else is right. Yes, even 600 inches for the small pizza is correct in the current state of the loop.

Also, if you want to do just what you said you want to do, then I dont think you need a menu, but if anyone else other than you were to use this program, they would not be able to. Because there is no way in the world the user would be able to know that a small pizza costs 12.90 a medium costs 13.40 and large costs 27.80. So maybe you can write that in the cout statement? Or yes, just create a menu function that just displays each pizzas price, and the acceptable inches for each pizza, so the user can scroll upp and look at it. Or even better, everytime he presses some button, it will show up again, incase they forgot something.
I think I got it this time, I have 2 questions,
1. The project asks that create a function to get the size and price accordingly but I used 2 functions. is that ok?
2. I use bunched of output to help user to pick the correct size and price.

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

void userInputSize(double&, double&, double&);
void userInputPrice(double&, double&, double&);

int main(){
	
	int choice;
	cout << "Enter the size and price the pizza" << endl;
	cout << " Small Pizza varies between 5 to 7" << endl;
	cout << " Medium pizza varies between 8 to 10" << endl;
	cout << " Large pizza varies between 11 to 13" << endl;
	double sSize,
		mSize,
		lSize,
		sPrice,
		mPrice,
		lPrice;
	userInputSize(sSize, mSize, lSize);
	cout << " Small pizza costs $12.90" << endl;
	cout << " Medium pizza costs $13.40" << endl;
	cout << " Large pizza costs $27.90" << endl;
	userInputPrice(sPrice, mPrice, lPrice);
	
	system("pause");
	return 0;
}
void userInputSize(double&small, double& medium, double& large){

	do {
		cout << "Enter the size of the small pizza (in inches):" << endl;
		cin >> small;
		cout << "Enter the size of the medium pizza (in inches):" << endl;
		cin >> medium;
		cout << "Enter the size of the large pizza (in inches):" << endl;
		cin >> large;
	} while (!(small >= 5) || !(small <= 7) && !(medium >=8) || !(medium <=10) && !(large >= 11) || !(large <= 13));

}

void userInputPrice(double&small, double&medium, double&large){

	do{
		cout << "Enter the price of the small pizza" << endl;
		cin >> small;
		cout << "Enter the price of the medium pizza" << endl;
		cin >> medium;
		cout << "Enter the price of the large pizza" << endl;
		cin >> large;
	} while ((small == 12.90) && (medium == 13.40) && (large == 27.80));
}
1. Yes, ofc it is! 2 functions in this case is better than one, since the functions do 2 different things.

2. Great job! It works great now. The only think that can be a bit improved is just the cout statements a bit. Like for example in the very beginning, you say

cout << "Enter the size and price the pizza" << endl;

and then say

1
2
3
cout << " Small Pizza varies between 5 to 7" << endl;
cout << " Medium pizza varies between 8 to 10" << endl;
cout << " Large pizza varies between 11 to 13" << endl;


But... what varies between 5-7? Specify that its inches(size).

Other than that. Looks great.
while (!(small >= 5) || !(small <= 7) && !(medium >=8) || !(medium <=10) && !(large >= 11) || !(large <= 13))

This needs to be fixed. Imagine I input the values:

1
2
3
small = 6;
medium = 1;
large = 1;

Then the while statement evaluates to

while (!true || !true && !false || !true && !false || !true)

simplified even more to

while (false || false && true || false && true || false)

which, since the && operator has precedence over the || operator (which I learned from an embarrassing mistake here last week), further simplifies to

while (false || false || false || false)

which in the end simplifies to

while (false)

You need to use more parentheses, surrounding more expressions so that you can control how they are evaluated. You also need to think about your use of the ! operator in relation to your usage of && and ||.
That was a great point!
Thank you.

while ((!(small >= 5) || !(small <= 7)) && (!(medium >= 8) || !(medium <= 10)) && (!(large >= 11) || !(large <= 13)));
Topic archived. No new replies allowed.