Difficulty with getline()

Hey all,
I can't seem to get the getline() function to work properly for me. I'm trying to use it so that I can store full names in a string array consisting of user-specified size n elements. I've tried a lot of different tweaks (different headers, using a string var instead, etc) all to no avail. Help, please. This was an exercise for my class but I don't think accepting whitespace is a requirement and I've already submitted it using cin instead. I just want to understand why I can't make it work. I doubt that any of the other code is impacting it but I include the entire program just in case. Also, when I tried building this in Visual Studio it wouldn't compile at all! (I did #include "stdafx.h" so that's not why... I just got a bunch of errors. If anyone is interested in what they are, I'll share but it's really besides the point since I'm only concerned with programming for linux/unix consoles for the time being.)

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;
double score(double a, double b, double c) {
    double max, min, final_score;
    if(a > b) {
        max = a;
        min = b;
    }
    else {
        max = b;
        min = a;
    }
    final_score = max * .35 + min * .3 + c * .35;
    return final_score;
}
void letter(double a) {
    if(a > 90)
        cout << "A";
    else if(a > 80)
        cout << "B";
    else if(a > 70)
        cout << "C";
    else if(a > 60)
        cout << "D";
    else
        cout << "F";
    return;
}
int main() {
    int n, x, y;
    cout << "How many students? ";
    cin >> n;
    double grade[n][3];
    string student[n];
    for(x = 0; x < n; x++) {
        cout << "Name: ";
        getline(cin, student[x]);
        for(y = 0; y < 3; y++) {
            cout << "Exam "<< y + 1 << " grade: ";
            cin >> grade[x][y];
        }
    }
    for(x = 0; x < n; x++) {
        cout << student[x] << ": ";
        letter(score(grade[x][0], grade[x][1], grade[x][2]));
        cout << endl;
    }
    return 0;
}
#include <string>
@Chervil, thanks for the suggestion but I tried that as well as
 
#include <cstring> 

to no avail... It compiles fine but the program behaves as if getline() was not even there... or as if only the return key was hit.
After the user enters the input for this line, cin >> n; there will be a newline character remaining in the input buffer.

If the program then does a getline() it will retrieve everything up to the newline character (which is then discarded). The result is an empty string.

The answer is to use cin.ignore() after cin >> n; before the getline() in order to remove the unwanted newline character.
Thanks, Chervil. Worked like a charm :)

I put it in the for loop so that it would discard the newline from each call to cin

1
2
3
4
5
6
7
8
 for(x = 0; x < n; x++) {
        cin.ignore();
        cout << "Name: ";
        getline(cin, student[x]);
        for(y = 0; y < 3; y++) {
            cout << "Exam "<< y + 1 << " grade: ";
            cin >> grade[x][y];
        }


All the best,
David
Topic archived. No new replies allowed.