program doesnt prompt for input in getline()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
string studentName;
    string studentGender;
    int studentCapacity;
    int n =0;

    cout << "Insert n:" << endl;
    cin >> n;

    Student* student = new Student[n];

    for(int i=0;i<n;++i)
    {
        cout << "Enter your name:" ; //ask for name
        getline(cin, studentName);
        cout << endl;

        cout << "Enter your Gender(Male or Female):"; //ask for gender
        getline(cin, studentGender);
        cout << endl;

        cout << "How many roomate(s) do you wish to have?:" << endl; //ask for capacity
        cout << "1. I want 1 roomate only (2 people in total)" << endl;
        cout << "2. I want 2 roomate (3 people in total)" << endl;
        cin >> studentCapacity;
        cout << endl;

    }

    system("cls");

    for(int i=0;i<n;++i)
    {
        student[i].Print(); //print student's information
    }


My problem is simple yet hard to trace.
I don't know what happen the program didn't prompt me to input student name and it straightly asked me for gender.
It just appears as:

1
2
Enter your name:
Enter your gender:

only the program asked me to input my gender..
I thought the name and gender has same getline syntax?
Last edited on
add
cin.clear();
cin.ignore( numeric_limits <streamsize> ::max(), '\n' )

to the end of each CIN.
dont add after getline though
tried using cin instead of getline for studentName,
and I added up the code as you suggested, seems working but it works like a string cin.
I try to input 'Peter Jackson' but the result displayed will be Peter only...
Try to do it with arrays. Instead of strings, use char st[length]. And then, read it with cin.getline(st, length). Length is an integer.
Last edited on
unable to debug getline with array, if I use cin.get(),
the program does not work, program did not prompt for any input and gave me some junk values when displaying information.

I tried to add cin.ignore() in line 13 in the code displayed, and it seems like working properly but it seems silly to put cin.ignore() inside a loop and it works quite badly if I just add it after cin my n since it only ignore once.
If I input my n is 2, my first student input is okay but for the input of second student the program, I managed to put the student name, but the program did not prompt for the gender etc etc...
well. It worked for me. Getline should get the line. It clearly works for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void populator(student array[] , int length)
{
     cout << "Enter your students  information in the format shown below for each student" << endl;
     cout << " Name" << endl;
     cout << " Grade" << endl;
     cout << " Gender" << endl;
     cout << " Studentnumber" << endl;
     cout << " Medperil" << endl;
     cout << " Address" << endl;
     cout << " Phone" << endl;
     
     
     for ( int m=0 ; m < length ; m++)
     {      
      
         getline (cin, array[m].name) ;  
         getline (cin, array[m].grade);
         getline (cin, array[m].gender);
         getline (cin, array[m].studentnumber);
         getline (cin,array[m].medperil);
         getline (cin, array[m].address);
         getline (cin, array[m].telephone);  
         
         
         cout << "Please Enter for Next student " << endl;
         cout << "" << endl;
                        
     }
         
         
}

It works for me.
1
2
3
cin >> length; c
in.clear();
cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); 

your each cin command should look like this
Topic archived. No new replies allowed.