0

Sorry, I dont know how to ask this better, I'm a really novice programmer and I'm not looking for you to do my homework, but I want to understand why this keeps happening.

int inputScores(string names [], double scores [])
{
  int count = 0;  // counter variable for number of student records in array
  char again;     // To check if user has more data
  do
  {
    cout << "Enter student's name: ";
    getline(cin, names[count]);
    cout << "\nEnter student's score: ";
    cin >> scores[count];
    count++;
    cout << "\nDo you have more student records to enter?(Y/N): ";
    cin >> again;

  }while(again == 'y' || again == 'Y');

when I run this code and call the function this keeps happening and I dont know how to fix it:

Enter student's name: Arthur

Enter student's score: 100

Do you have more student records to enter?(Y/N): y
Enter student's name:
Enter student's score:

it skips the "enter student's name question" (doesnt let me type anything) and goes straight to the next question.

Taryn
  • 242,637
  • 56
  • 362
  • 405
aSilveira
  • 79
  • 2
  • 10
  • Why aren't `names` and `scores` local variables? Passing them in as a copy is defeating the purpose. – tadman Jun 20 '14 at 00:48
  • 2
    cin only reads the y, leaving the next getline to read the newline character. this question is very common, so try having a look at the 'related' column for solutions, and remember to have a good search on SO before posting – user3125280 Jun 20 '14 at 00:49
  • @tadman I'm not really sure what you mean, this is just the way the teacher had us do the assignment, passing those arrays to this function which should fill the arrays with input from users. – aSilveira Jun 20 '14 at 01:17
  • 1
    @user3125280 I searched a lot before posting this, I hadn't been on this site yet but now I'm looking at other posts, I guess I just have to read more about keyboard buffers and cin. Thank you! – aSilveira Jun 20 '14 at 01:19

1 Answers1

2

The reason the program does not wait for you to enter the student's name is there is a \n still left in the input stream after you read again in the line:

cin >> again;

When the program reaches:

getline(cin, names[count]);

it just reads the an empty line and moves on to the next line.

You need to use:

int maxCharsToIgnore = 100; // This seems large enough
                            // for your use case.
cin.ignore(maxCharsToIgnore,'\n');

right after

cin >> again;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    +1 `std::numeric_limits::max()` works as well, and leaves little doubt either the skip will happen, or die trying. – WhozCraig Jun 20 '14 at 01:18
  • this works, however I have to hit enter twice now after typing name the first time around. Do you know why? – aSilveira Jun 20 '14 at 01:20
  • @user3758431, you can move the `cin.ignore` line right after reading `again`. – R Sahu Jun 20 '14 at 01:21
  • 1
    @WhozCraig, Thanks for the upvote. I agree with you on the use of `std::numeric_limits::max()`. – R Sahu Jun 20 '14 at 01:24
  • @WhozCraig Thank you so much, I definitely need to read more, like I said Im a novice, this is for my first programming class and the teacher never mentioned anything about cin.ignore(). Do you suggest any reading on this subject? Also I will come back here to upvote you once I have more rep. Thank you again! – aSilveira Jun 20 '14 at 01:44
  • @aSilveira eh. not really. there are plenty of decent texts on the [book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), You can count on anything by Scott Meyers being a decent read. In the end its like a good scotch; you gotta let it age. While doing so, pay a **ton** of dues writing code and seeing how things work. Read the standards, if you get confused, ask about it here. But no matter what stick with it, and when you get stuck, bring solid questions with concrete attempts and suspicions here. If you ask the right question, you'll get solid answers. – WhozCraig Jun 20 '14 at 03:42
  • @aSilveira Oh, and spend an hour a day [on cppreference.com](http://en.cppreference.com/w/) reading about classes you may find interesting, then read about the ones you're quite certain are *not*.( Hint: they *all* are.) Pay particular dues to the algorithms, containers, and IO stream classes and functions (not necessarily in that order) You'll use those time and time again, and they will become your bread-and-butter. – WhozCraig Jun 20 '14 at 03:47