Hi,
I'm writing a project for my C++ class that uses an input file. The first line of the file has a city and a state then there are about 100 lines after that. The first item in each line is a string and the second item is an integer.
I need to bring in the city and state and store it in a variable, then put the rest of the data in an array of structs.
To bring in the city and state I used 'getline'. For some reason 'getline' is reading the last line of the file and not the first. So when i start to fill in my array of structs it thinks there is no more data in the file.
This is what the code looks like (variable 'place' is predefined):
getline(infile, place);
cout << place << endl;
The output of this gives me the last line of the input file.
Just to see if I could bring data in one piece at a time correctly, I wrote this.
string name;
infile >> name;
cout << name << endl;
getline(infile, place);
cout << place << endl;
The output of this gives the first word of the file then on the next line it prints the last line of the file. So I don't think the problem is with 'infile' but rather with 'getline'.
If anyone could help me with this I'd appreciate it.
I'm writing a project for my C++ class that uses an input file. The first line of the file has a city and a state then there are about 100 lines after that. The first item in each line is a string and the second item is an integer.
I need to bring in the city and state and store it in a variable, then put the rest of the data in an array of structs.
To bring in the city and state I used 'getline'. For some reason 'getline' is reading the last line of the file and not the first. So when i start to fill in my array of structs it thinks there is no more data in the file.
This is what the code looks like (variable 'place' is predefined):
getline(infile, place);
cout << place << endl;
The output of this gives me the last line of the input file.
Just to see if I could bring data in one piece at a time correctly, I wrote this.
string name;
infile >> name;
cout << name << endl;
getline(infile, place);
cout << place << endl;
The output of this gives the first word of the file then on the next line it prints the last line of the file. So I don't think the problem is with 'infile' but rather with 'getline'.
If anyone could help me with this I'd appreciate it.