Link to home
Start Free TrialLog in
Avatar of koneill99
koneill99

asked on

Getline problem - Borland C++

I have a strange problem.  The following code snippet demonstrates the problem.
I have 4 pieces of data I need to capture which I then store into the index of some arrays.  The index corresponds to the "loop number".

The problem is that on the FIRST LOOP, the program falls through the first GETLINE without waiting for input.  It displays "EnterStudent Name" then immediately displays "Enter student ID" (the second element).

After the ForNEXT loop goes throught the first iteration, it works properly.

It's almost like the default delimiter is being reached on the first pass without typing anything.

Any help would be appreciated.



void readData (string studentName [], string studentId [], float score [],
               int limit, const int MAX_SIZE)
{
        string id;
        string name;
        const int highest = 100;
        const int lowest = 0;
        int temp = 0, validGrade = 0;

        for(int i = 0; i < limit; i++)
        {
                cout << "Enter the students name: ";
                getline(cin,name);   <========================Problem Here on 1st loop.  Program doesnt stop here on first pass
                studentName[i] = name;

                cout << "Enter " << studentName[i] << "'s i.d: ";
                getline(cin,id);
                studentId[i] = id;

                cout << "Enter " << studentName[i] << "'s grade: ";
                cin >> temp;
                validGrade = validateData(temp, highest, lowest);
                score[i] = validGrade;
                system("cls");
        }

}

Avatar of sunnycoder
sunnycoder
Flag of India image

Hi koneill99,

> getline(cin,name);   <========================Problem Here on 1st loop.  
> Program doesnt stop here on first pass
Chances are that you had read some other input before calling this function and there is some unused input (probably just a newline) which is lying unconsumed in the input buffer ... your first getline call is satisfie by this unconsumed input ... For this reason, you dont get a prompt here in first iteration ..

Try adding a getline before the loop or remove unconsumed input from buffer at the same place where you received that input.

Cheers!
sunnycoder
Avatar of koneill99
koneill99

ASKER

Thank you for your fast reply.  The function I posted is the first time I use GETLINE in the program.
There is another part of the program where I get the number of students (code below).  I use this value as the LIMIT var that I pass to the previously posted function.
I think you're right that there is a delimeter in the buffer.  Should I just call a GETLINE to clear it?  Is there a better way to initialize it?

void getHowMany(int& num)
{
        int newNumber = 0;
        const int max_Students = 30;
        const int min_Students = 0;
        cin >> num;
        newNumber = validateData(num, max_Students, min_Students);
        num = newNumber;
}
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Was your question left partly unanswered?

Please read the grading guidelines ...
https://www.experts-exchange.com/help.jsp#hi73

If you wish to change the grade, it can be done by posting a request in community support.
https://www.experts-exchange.com/Community_Support/

sunnycoder