Getline won't work

Apr 10, 2020 at 8:55pm
Did my own code. Didn't work. Copied instructors code and won't work.
They don't know why. Case 1 of this is to read a .txt file:
The Wizard of Oz
1939
Deadpool
2016

And print it out:
Wizard of Oz 1939
Deadpool 2016.

My output skips the string and only outputs the years. exact code works correctly on instructors machine(mac). i have a surface book.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include <iostream>
#include<string>
#include<fstream>

using namespace std;

void readFromfile(string filename, string title_arr[], int year_arr[], int& numberOf );


int main() {
    bool done=false;
    int menu;
    const int MAXSIZE = 100;
    string title[MAXSIZE];
    int size=0;
    int year[MAXSIZE];
    while(!done){
        cout<< "Select one of the options below\n"
               "1: Read in Movies\n"
               "2: List titles, years to the screen\n"
               "3: List titles, years to the screen\n"
               "4: Exit";
        cin>>menu;
        switch (menu){
            case 1:
                cout<<"Read from file\n";
                readFromfile("movies.txt", title, year, size );
                break;
            case 2:
                cout<<"yo\n";
                break;
            case 3:
                cout<<"yo\n";
                break;
            case 4:
                cout<<"You will now exit the menu.\n";
                done=true;
                break;
            default:
                cout<<"Invalid selection\n";
        }

    }

    return 0;
}

void readFromfile(string filename, string title_arr[], int year_arr[], int& numberOf){
    string name,y;
    int year;
    ifstream infile(filename);
    while(getline(infile,name)){
        getline(infile, y);
        year=stoi(y);

        title_arr[numberOf]=name;
        year_arr[numberOf]=year;


        cout<<title_arr[numberOf]<<" "<<year_arr[numberOf]<<endl;



    }

}
Apr 10, 2020 at 9:04pm
What's that variable numberOf for? It's zero, and it's always zero. It's useless.

Why convert the string of the year into a number? You're just outputting it anyway.

If you simplify the function to this, what happens?

1
2
3
4
5
6
7
8
9
10
11
12
void readFromfile(string filename)
{
    string name,y;
    ifstream infile(filename);
    while(getline(infile,name))
    {
        getline(infile, y);
     
        cout<<name << " " << y << endl;
    }

}
Apr 10, 2020 at 9:09pm
Is it possible for you to dump the byte output of your file? Something funny might be going on with line endings.

There's a unix utility called hexdump, e.g. hexdump -C "movies.txt"
Here's an equivalent Windows version: https://www.di-mgt.com.au/hexdump-for-windows.html
Last edited on Apr 10, 2020 at 9:16pm
Apr 10, 2020 at 9:12pm
Oh, good guess. A Mac will be using Unix line endings, OP's surface book will be using windows line endings.

If you can't dump the file and examine the line endings, just recreating it yourself by hand using notepad or some other plain text editor would do.
Last edited on Apr 10, 2020 at 9:12pm
Apr 10, 2020 at 9:25pm
Repeater numberOf will be used later to verify there is still space in the array. For now it's 0 to just read in the info. At least that's how it was explained to me. Tried name and year with same result.
Apr 10, 2020 at 9:27pm
I understand the hex dump but i found the same problem with my own code and text file. I've used infile.ignore and .back or pop_back commands before but I'm not sure how I'd do that here.
Apr 10, 2020 at 10:01pm
That's why I'm asking for the exact bytes of the file you're using. Perhaps it would be easier of you uploaded the file verbatim to some file sharing site.
Apr 10, 2020 at 10:06pm
38 bytes, literally just the make file within c++ itself using clion
Apr 10, 2020 at 11:54pm
figured it out. you need to use
1
2
if (name.back() == '\r') {
            name.pop_back();

for windows to do any getline ops
Apr 10, 2020 at 11:55pm
other wise the cursor jumps back to the beginning and you lose the first word. goodness that killed an afternoon.

thanks for the help everyone!
Apr 11, 2020 at 12:25am
Sounds like a screw up in your text file. Those '\r' characters myst have come from somewhere. They were used as line end characters in some systems, but haven't seen just that for a long time.
Apr 11, 2020 at 2:12am
Hello bigskit13,

Instead of the if statement to check for the "\r" at the end of the line you could write the "getline" as getline(infile,name, '\r').

Or delete the original "movies.txt" file and create another.

I use VS 2017 with Windows and had no problem running your code. I do suspect that the lines I copied from here to use in the "movies.txt" file may have changed the end of line character and when I check the file it was correct.

With this new information I will have to see if I can create a file with "\r" at the end of the line and see what happens.

When I was working with the code I did manage to get this output:
                                     // Intentional blank line.
Select one of the options below
1: Read in Movies
2: List titles, years to the screen
3: List titles, years to the screen
4: Exit
 Enter Choice: 1

Read from file

The Wizard of Oz 1939
Deadpool 2016
Harvey 1950

Select one of the options below
1: Read in Movies
2: List titles, years to the screen
3: List titles, years to the screen
4: Exit
 Enter Choice: 4

You will now exit the menu.


Just so you know it does work.

Andy
Apr 11, 2020 at 3:20am
Some helpful links that also explain this problem:
http://www.cplusplus.com/forum/general/51349/
https://stackoverflow.com/questions/45956271/stdgetline-reads-carriage-return-r-into-the-string-how-to-avoid-that

https://gist.github.com/josephwb/df09e3a71679461fc104
https://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf/

I'm surprised it doesn't happen more often. I guess you have to be careful when reading in files originating from different systems, and either sanitize the file before-hand or have more checks.
Topic archived. No new replies allowed.