Looping and shortening

When I run this, if I enter a value that is less than 0, like lets say a character, it just spams the question over and over again.

Also is there anyway to shorten this to optimize space and time?

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
 //ask what the price of each item was

	double item1Price = 0, item2Price = 0, item3Price = 0, item4Price = 0, item5Price = 0, item6Price = 0,
		item7Price = 0, item8Price = 0, item9Price = 0, item10Price = 0;
	do
	{
		cout << "What was the price of " << item1 << "?" << endl;
		cin >> item1Price;
	} while (item1Price <= 0);
	do
	{
		cout << "What was the price of " << item2 << "?" << endl;
		cin >> item2Price;
	} while (item2Price <= 0);
	do
	{
		cout << "What was the price of " << item3 << "?" << endl;
		cin >> item3Price;
	} while (item3Price <= 0);
	do
	{
		cout << "What was the price of " << item4 << "?" << endl;
		cin >> item4Price;
	} while (item4Price <= 0);
	do
	{
		cout << "What was the price of " << item5 << "?" << endl;
		cin >> item5Price;
	} while (item5Price <= 0);
	do
	{
		cout << "What was the price of " << item6 << "?" << endl;
		cin >> item6Price;
	} while (item6Price <= 0);
	do
	{
		cout << "What was the price of " << item7 << "?" << endl;
		cin >> item7Price;
	} while (item7Price <= 0);
	do
	{
		cout << "What was the price of " << item8 << "?" << endl;
		cin >> item8Price;
	} while (item8Price <= 0);
	do
	{
		cout << "What was the price of " << item9 << "?" << endl;
		cin >> item9Price;
	} while (item9Price <= 0);
	do
	{
		cout << "What was the price of " << item10 << "?" << endl;
		cin >> item10Price;
	} while (item10Price <= 0);
Turn itemprice into an array of prices or a vector of prices. The problem with the spaming problem is you need a conditional statement within the do while that validates the input, and when the input is not an integer the cin will be cleared
example
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

#include <iostream>
using namespace std;

int main()
{
int opt = 0;
bool inputGood = false;

do
{
    std::cout << "Pick lang again:" << '\n';
    std::cout << "1.[es-ES]:" << '\n';
    std::cout << "2.[en-US]:" << '\n';
    inputGood = std::cin >> opt;
    if(!inputGood)
    {  
      std::cout << "\nERROR! Invalid choice." << '\n';
      cin.clear();
      while( cin.get() != '\n' );
    }
}while(!inputGood || opt < 1 || opt > 2);


system ("PAUSE");
return 0;
}
so I'm getting it to recognize that an invalid price was entered, but I'm not allowed to input anything in.

Also, couldyou explain in your example line 20? why is it set to not equal '\n'?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

cdouble item1Price = 0, item2Price = 0, item3Price = 0, item4Price = 0, item5Price = 0, item6Price = 0,
		item7Price = 0, item8Price = 0, item9Price = 0, item10Price = 0;
	bool price = false;
	do
	{
		cout << "What was the price of " << item1 << "?" << endl;
		cin >> item1Price;
		if (!price)
		{
			cout << "Invalid price, enter a new amount." << endl;
			cin >> item1Price;
		}while (cin.get() != '\n');
	} while (item1Price <= 0);
The '\n' is the newline character. Try this code below, but i would still use an array of itemprice intead of multiple variables

1
2
3
4
5
6
7
8
9
10
11
12
13
double item1Price = 0, item2Price = 0, item3Price = 0, item4Price = 0, item5Price = 0, item6Price = 0,
		item7Price = 0, item8Price = 0, item9Price = 0, item10Price = 0;
	bool price = false;
	do
	{
		cout << "What was the price of " << item1 << "?" << endl;
		price = cin >> item1Price;
		if (!price)
		{
			cout << "Invalid price, enter a new amount." << endl;
			cin.clear();
		}while (cin.get() != '\n');
	} while (item1Price <= 0);
I haven't covered arrays or vectors yet that's why I use variable after variable so far.

I get an error where "price = cin >> item1Price;"

The error is

2 IntelliSense: no suitable conversion function from "std::basic_istream<char, std::char_traits<char>>" to "bool" exists


1
2
3
4
5
6
7
8
9
10
11
12
13
double item1Price = 0, item2Price = 0, item3Price = 0, item4Price = 0, item5Price = 0, item6Price = 0,
		item7Price = 0, item8Price = 0, item9Price = 0, item10Price = 0;
	bool price = false;
	do
	{
		cout << "What was the price of " << item1 << "?" << endl;
		price = cin >> item1Price;
		if (!price)
		{
			cout << "Invalid price, enter a new amount." << endl;
			cin.clear();
		}while (cin.get() != '\n');
	} while (item1Price <= 0);
Bump
1
2
3
4
5
6
7
8
9
10
while( ! ( std::cin >> item1Price ) // user did not entered a number
        || item1Price <= 0 ) // or the price enteredis not positive
{
    std::cout << "Invalid price, enter a new amount.\n" ; // inform the user
    
    std::cin.clear() ; // clear the error state of the stream
    // http://www.cplusplus.com/reference/istream/istream/ignore/
    
    std::cin.ignore( 1000, '\n' ) ; // throw away the junk that was entered
}
works perfect, thank you both
Topic archived. No new replies allowed.