getline(cin, var) weird error.

So, using the code below I need to collect data entered by a user. The problem I'm having is that getline is being treated like a cin, on the first iteration of the loop only. By that I mean spaces are treated as the end of a line and the beginning of new data input.

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
//PRIMARY LOOP
int loop(char grade[], int hours[], string clnam[], string crsem[], string crnum[], int& clnum, int& loopnum, string filename, int cap, course_taken course_taken[]) {
        for ( ; loopnum < clnum; loopnum++) {
                cout << "What was the name of the class?:\n";
                cin.ignore();
                getline (cin, clnam[loopnum]);

                cout << "What about the semester?\n";
                getline (cin, crsem[loopnum]);

                cout << "Now the course number:\n";
                getline (cin, crnum[loopnum]);

                cout << "And the grade you received:\n";
                cin >> grade[loopnum];
                bool checker = 0;
                while (checker == 0) {
                        if (*LONG ASS LIST OF CONDITIONALS*) {
                                checker = 1;
                        }
                        else {
                                //cin.clear();
                                cout << "That's not a grade. Try again:\n";
                                cin >> grade[loopnum];
                        }
                }

                cout << "Finally, enter the number of hours the class was worth:\n";
                cin >> hours[loopnum];
                checker = 0;
                while (checker == 0) {
                        if (hours[loopnum] < 20 && hours[loopnum] > 0) {
                                checker = 1;
                        }
                        else {
                                //cin.clear();
                                //cin.ignore (100, '\n');
                                cout << "Gee golly gosh, I don't think you should be entering a value like that. Try again.\n";
                                cin >> hours[loopnum];
                        }
                }
                course_taken[loopnum].set(clnam[loopnum], crsem[loopnum], crnum[loopnum], grade[loopnum], hours[loopnum]);
        }
        write(filename, grade, hours, clnam, crsem, crnum, clnum);
        read (filename, grade, hours, clnam, crsem, crnum, clnum, cap, course_taken);

        return loopnum;
}


Here's an example of my input and the input and output I get:

What was the name of the class?:
ABCE 1234
What about the semester?
Fall 2015
Now the course number:
ABCE 1234
And the grade you received:
A
Finally, enter the number of hours the class was worth:
3
What was the name of the class?:
BCAE 1234
What about the semester?
Fall 2015
Now the course number:
BCAE 1234
And the grade you received:
b
Finally, enter the number of hours the class was worth:
3
What was the name of the class?:
CBAE 1234
What about the semester?
Fall 2015
Now the course number:
CBAE 1234
And the grade you received:
c
Finally, enter the number of hours the class was worth:
3
This is the GPA and Course Storage Program. Please enter the letter next to the choice you wish to pick. Your options are:
A.) Calculate the GPA for all courses.
B.) List all courses.
C.) Add another course to the list.
D.) Calculate the GPA for a particular semester.
E.) Calculate the total credit hours of courses with grade 'D'.
F.) Delete a class.
Q.) Quit the program.
b
ABCE
1234
Fall
ABCE
1234
A
CBAE 1234
Fall 2015
CBAE 1234
Last edited on
It's not storing newlines in the string on my side. Sounds like something is wrong in your function that is listing all of the courses. You should post that function instead.
1
2
3
4
5
6
7
8
//List Course Function
void list(string clnam[], string crsem[], string crnum[], int clnum){
        for (int printout = 0; printout < clnum; printout++) {
                cout << clnam[printout] << endl;
                cout << crsem[printout] << endl;
                cout << crnum[printout] << endl;
        }
}

Here's my code to printout the values. Earlier I had a weird error where I'd written some getline statements in a function designed to write to a file and that was causing errors in my main function for some reason, so it might be indirectly related somehow.

EDIT
Woah, hold up. I actually fixed it by adding the getline functions back into my writing function. Checking the text file, I can confirm that everything is getting written in correctly. However, now the course isn't properly listing the final course number.

What was the name of the class?:
ABCDEFG 2584
What about the semester?
Fall 2015
Now the course number:
ABCD 2584
And the grade you received:
A
Finally, enter the number of hours the class was worth:
4
What was the name of the class?:
HIJKLMNOP 3053
What about the semester?
Fall 2015
Now the course number:
HIJK 3053
And the grade you received:
B
Finally, enter the number of hours the class was worth:
3
This is the GPA and Course Storage Program. Please enter the letter next to the choice you wish to pick. Your options are:
A.) Calculate the GPA for all courses.
B.) List all courses.
C.) Add another course to the list.
D.) Calculate the GPA for a particular semester.
E.) Calculate the total credit hours of courses with grade 'D'.
F.) Delete a class.
Q.) Quit the program.
b
ABCDEFG 2584
Fall 2015
ABCD 2584

HIJKLMNOP 3053
Fall 2015


Seems like it's skipping the "clnam" variable forward into the "crsem" variable and putting "crsem" where "crnum" ought to go. Yet it writes properly into the file.
Last edited on
Topic archived. No new replies allowed.