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.
#include <iostream>
#include<string>
#include<fstream>
usingnamespace std;
void readFromfile(string filename, string title_arr[], int year_arr[], int& numberOf );
int main() {
bool done=false;
int menu;
constint 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;
}
}
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.
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.
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.
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.
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.
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.