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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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?

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
 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
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    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.