### Using sentinel method to end the input sequence? Help!

SOLVED
Last edited on
use a while loop

while(input != -1)

inside the while loop continue asking for more numbers and creating the structures
SOLVED
Last edited on
I am getting the continuous prompt now in the console and when I enter -1 it breaks out of the loop but it skips to line 55. I cannot figure out how to have it skip over this problem. If I change the 0 in line 55 to a number besides 0 I get an output but the numbers are all wrong.

I feel like I am looking over an obvious problem :(
Last edited on
You need to take out your old for loop, and increment count inside the while loop, since you declare it as 0 and never do anything with it

 ``123456789101112131415161718192021`` ``````while (true) { // create a node Score* aScore = new Score; cout << "Enter score " << (i + 1) << " [-1 to quit]: "; cin >> aScore->value; cin.ignore(1000,10); // check for sentinal value if (aScore->value == -1) break; // add node to list (stack model) aScore->next = head; head = aScore; // sum the scores scoreTotal += aScore->value; i++; count++; }``````

ueeyu
Thanks you have been super helpful! One last question but I'm having difficulty getting rid of my old "for loop".

Lines 24-28 are not needed though when I delete that "if" loop I am receiving a ton of compiling errors. However isn't the "for" loop starting from line 31 necessary for my program to work properly?

 ``1234567891011121314151617181920212223242526272829303132333435363738`` `````` while (true) { // create a node Score* aScore = new Score; cout << "Enter score " << (i + 1) << " [-1 to quit]: "; cin >> aScore->value; cin.ignore(1000,10); // check for sentinal value if (aScore->value == -1) break; // add node to list (stack model) aScore->next = head; head = aScore; // sum the scores scoreTotal += aScore->value; i++; count++; } // check count if (count == 0) { cout << "0 scores to enter?" << endl; } else { cout << endl; // prompt user to enter scores for (int i = 0; i < count; i++) { // create a node Score* aScore = new Score; cout << "Enter score " << (i + 1) << ": "; cin >> aScore->value; cin.ignore(1000,10);``````
@TrentMason
I'm sorry but I don't understand what "ueeyu" means
So I copy and pasted your code, I didn't really add any lines of code I just deleted most of the for loop and moved some needed things from it to the while loop.

I tested it and it works, I'm just going to paste the entire thing here since its hard to explain where everything was moved, but the only changes are in the while loop and for loop. So it should be pretty easy to see the changes
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` `````` while (true) { // create a node Score* aScore = new Score; cout << "Enter score " << (i + 1) << " [-1 to quit]: "; cin >> aScore->value; cin.ignore(1000,10); // check for sentinal value if (aScore->value == -1) break; // add node to list (stack model) aScore->next = head; head = aScore; // sum the scores scoreTotal += aScore->value; // check for min/max if (aScore->value < min) min = aScore->value; if (aScore->value > max) max = aScore->value; // check for A, B, C, D, F grades if (aScore->value >= 90) { grade[0]++; } else if (aScore->value >= 80) { grade[1]++; } else if (aScore->value >= 70) { grade[2]++; } else if (aScore->value >= 60) { grade[3]++; } else { grade[4]++; } i++; count++; } // check count if (count == 0) { cout << "0 scores to enter?" << endl; } else { cout << endl; cout << endl; // display scores cout << "Scores entered:" << endl; for (Score* p = head; p; p = p->next) { cout << p->value << ' '; } cout << endl << endl; // display min, max cout << "Lowest score = " << min << endl; cout << "Highest score = " << max << endl; // calculate and display average for (Score* p = head; p; p = p->next) { sum += p->value; } average = double(sum) / count; cout << fixed << setprecision(1); cout << "Average score = " << average << endl; cout << endl; // display number of A grades cout << "Number of A grades: " << grade[0] << endl; cout << "Number of B grades: " << grade[1] << endl; cout << "Number of C grades: " << grade[2] << endl; cout << "Number of D grades: " << grade[3] << endl; cout << "Number of F grades: " << grade[4] << endl; if(grade[0] > 0) cout << "At least one passing grade entered" << endl; if(grade[1] > 0) cout << "At least one passing grade entered" << endl; if(grade[2] > 0) cout << "At least one passing grade entered" << endl; } // release borrowed memory while (head) { Score* next = head->next; delete head; head = next; } return 0; }``````
THANK YOU!!!
Topic archived. No new replies allowed.