Trouble With Loops

Hello, I am currently working on a c++ project that will input students and process their grades. I encountered a problem and my code stops executing after the getScores function. Im not sure what the problem is, but im guessing its something within the function and the loops. Any help will be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Name: getScores
//Description: Will get scores for student
//Parameters: None
//Return: testScore

double getScores()
{
  double testScore;
  double scoreCount=0;

  cout<<"Enter your test score: ";
  cin>>testScore;
  scoreCount++;

  if(testScore<0||testScore>100)
    {
      cout<<"Enter a test score between 0 and 100: ";
      cin>>testScore;
    }
  cin.ignore(80,'\n');
  return testScore;
}
Last edited on
Line 83, 'n' is not the same as '\n'
Whenever I switch it to '\n' my getScore function loops forever, even when the limit is 10. When its just 'n' it loops correctly for 10 times, but does not execute the next function. I'm still slightly confused on what is happening here.
If you don't know what line 83 does, why did you write it?
Adding cin.ignore() to the next line clears/ignores the newline from the stream. I'm aware of what it does but I am confused as to why it keeps looping and does not execute the next function.
Have you considered rewriting the function? It doesn't look like it does anything remotely close to what you want it to do. The loop runs and overwrites the previous input, and then you check the last input to prompt the user to enter just one more number if it was out of range, then your function ends, returning that last input - all the other inputs are lost, and the returned value may not even be in range.
Last edited on
What I ended up doing was getting rid of the first while loop in the getScore function. Then in the getGrade function the return was supposed to return the letterGrade. I figured it out, thank you for your time LB
Topic archived. No new replies allowed.