Avatar of 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: ";
                studentId[i] = id;

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



Avatar of undefined
Last Comment

8/22/2022 - Mon

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.


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;

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Was your question left partly unanswered?

Please read the grading guidelines ...

If you wish to change the grade, it can be done by posting a request in community support.

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.