### 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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` `````` //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
 ``123456789101112131415161718192021222324252627`` `````` #include 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'?

 ``1234567891011121314`` `````` 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

 ``12345678910111213`` ``````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

 ``12345678910111213`` ``````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
 ``12345678910`` ``````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.