Working with Arrays and Loops

May 31, 2013 at 7:19pm
I am trying to create program that asks a students information. However when I run the program the first questions is skipped? How do I fix that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   for(int x=0; x<i; x++)
    {
        cout<<"What is the students name? "<<endl;
        getline(cin,name[x]);
        cout<<"What Class is this? "<<endl;
        getline(cin,Class[x]);
        cout<<"How old is the student? "<<endl;
        cin>>age[x];
        cout<<"What grade is the student in? "<<endl;
        cin>>grade[x];
        cout<<"What was the student's grade for the class? "<<endl;
        cin>>gradeforclass[x];

    }
May 31, 2013 at 7:32pm
May 31, 2013 at 8:35pm
since you have not posted the code where I put this loop, I think there are two different types of problems:
First of all, you should use the type 'string' and type 'array' for use getline and assuming you still able to use the type 'array', should be of type 'char'.
In fact, if you only change the array type from int to char at least in the first two questions, you no longer have the problem of the jump of the first questions.
May 31, 2013 at 9:12pm
you should use the type 'string'
getline requires second operand be of type std::string, so he already using it.

you no longer have the problem of the jump of the first questions.
The problem here is mix of getline and stream extraction operations. To be precise it is a newline symbol left in input buffer after >> operator.
May 31, 2013 at 9:29pm
right clarification, miiniipaa. apologize for my lack of precision, but since there is the entire code, I thought strange things.
Topic archived. No new replies allowed.