### 2 questions, array and looping

First question

The code was working just fine until I started to do some tuning to my program. I added the array string item[10] {"", "", "", "",.....}

When I run the program, as soon as I enter the 10th item name windows pops up saying "Program" has stopped working. And forces me to close it by entering another key to close it.

 123456789101112131415161718192021 string item[10] {"", "", "", "", "", "", "", "", "", ""}; cout << "Enter the ten items that you bought at " << store << ". The first item is: " << item[1] << endl; getline(cin, item[1]); cout << "The second item is: " << endl; getline(cin, item[2]); cout << "The third item is: " << endl; getline(cin, item[3]); cout << "The fourth item is: " << endl; getline(cin, item[4]); cout << "The fifth item is: " << endl; getline(cin, item[5]); cout << "The sixth item is: " << endl; getline(cin, item[6]); cout << "The seventh item is: " << endl; getline(cin, item[7]); cout << "The eigth item is: " << endl; getline(cin, item[8]); cout << "The ninth item is: " << endl; getline(cin, item[9]); cout << "The tenth item is: " << endl; getline(cin, item[10]);

Second Question

Before I implemented that array, this set of code works but to get from the price of 1 item to the next I have to enter the price twice. Example is if item #1 is \$5 I have to enter 5, it skips to the next line and I enter 5 again then it will move to item #2

 1234567891011121314151617181920212223242526 double itemPrice[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool price = false; do { cout << "What was the price of " << item[1] << "?" << endl; cin >> itemPrice[1]; while (!(cin >> itemPrice[1]) || itemPrice[1] <= 0) { cout << "Invalid price, enter a new amount." << endl; cin.clear(); cin.ignore(1000, '\n'); } } while (itemPrice[1] <= 0); do { cout << "What was the price of " << item[2] << "?" << endl; cin >> itemPrice[2]; while (!(cin >> itemPrice[2]) || itemPrice[2] <= 0) { cout << "Invalid price, enter a new amount." << endl; cin.clear(); cin.ignore(1000, '\n'); } } while (itemPrice[2] <= 0);
arrays and vectors start at place 0. So int myArray[3] would be myArray[0], myArray[1], myArray[2].
 First question The code was working just fine until I started to do some tuning to my program. I added the array string item[10] {"", "", "", "",.....} When I run the program, as soon as I enter the 10th item name windows pops up saying "Program" has stopped working. And forces me to close it by entering another key to close it.

 getline(cin, item[10]);

The coerced termination of your software results from the attempted access of a non-existent array element, which is done during the execution of the preceding statement. Know that the elements within an array are accessed using the indexes [0, N); in other words, it is acceptable only to enclose an integer between zero and the size of the array minus one in square brackets.

http://en.cppreference.com/w/cpp/language/array

I have modified your source code to correct the error.

 1234567891011121314151617181920212223 string item[10] {"", "", "", "", "", "", "", "", "", ""}; cout << "Enter the ten items that you bought at " << store << ". The first item is: " << item[1] << endl; getline(cin, item[0]); cout << "The second item is: " << endl; getline(cin, item[1]); cout << "The third item is: " << endl; getline(cin, item[2]); cout << "The fourth item is: " << endl; getline(cin, item[3]); cout << "The fifth item is: " << endl; getline(cin, item[4]); cout << "The sixth item is: " << endl; getline(cin, item[5]); cout << "The seventh item is: " << endl; getline(cin, item[6]); cout << "The eigth item is: " << endl; getline(cin, item[7]); cout << "The ninth item is: " << endl; getline(cin, item[8]); cout << "The tenth item is: " << endl; getline(cin, item[9]);
Ah thank you, any of kyou know about by 2nd question
 Before I implemented that array, this set of code works but to get from the price of 1 item to the next I have to enter the price twice. Example is if item #1 is \$5 I have to enter 5, it skips to the next line and I enter 5 again then it will move to item #2

That's because you're inputting the price twice.

 67 cin >> itemPrice[1]; while (!(cin >> itemPrice[1]) || itemPrice[1] <= 0)

What you probably meant was to check the status of cin, rather that inputting itemPrice a second time.
 12 cin >> itemPrice[1]; while ((! cin) || itemPrice[1] <= 0)

Last edited on
Thank you it worked
Actually, I tested it to see what happens if I put in an invalid price like "h" or any character, Invalid Price gets printed twice and anything I enter from there on whether it be a number or character outputs Invalid Price

 123456789101112 do { cout << "What was the price of " << item[0] << "?" << endl; cin >> itemPrice[0]; while ((!cin) || itemPrice[0] <= 0) { cout << "Invalid price, enter a new amount." << endl; cin.clear(); cin.ignore(10000, '\n'); } } while (itemPrice[0] <= 0);
Bump
If cin is invalid or itemPrice is <= 0, you will never exit the loop at line 5 since you never do another input operation.
Topic archived. No new replies allowed.