I am trying to get user input for a parrallel array and the following loop
runs properly the first time. The second time through is puts the requests on the same line.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
const int NUM_STUDENTS = 4; //four students
string names[NUM_STUDENTS]; //student names
string number[NUM_STUDENTS]; //student numbers
// Input student sfrist and last name
cout << "Enter the students first name and their student number\n";
for (int count = 0; count < NUM_STUDENTS; count++)
{
cout << "Student first and last name : ";
getline(cin, names[count]);
cout << "Student Number:";
cin >> number[count];
}
When you do cin >> number[count];, it leaves behind a newline character in the input buffer (from you pressing the Enter key).
Then, when your code loops back around and you do getline(cin, names[count]);, getline sees that newline character right away (signaling the end of the input, since by default, getline reads until a newline character is found), gobbles it up, and moves on before you have a chance to twitch your fingers.
cin.ignore(); causes cin to discard one character from the input buffer. With any luck, this will be the newline character that's causing problems for you, but if you want to be sure, #include <limits> and use cin.ignore(numeric_limits<streamsize>::max(), '\n'); instead (this will clear out everything in the input buffer).
Another way to clear out the newline character is to change the getline call to getline(cin >> ws, names[count]);.
This will discard any leading whitespace in the input buffer (including newlines) before the call to getline, so you can be sure that getline won't "skip" over your input.