C++ STL Structures & List Iterators: I\'m trying to read a file with a format bu
ID: 3854897 • Letter: C
Question
C++ STL Structures & List Iterators:
I'm trying to read a file with a format but it's not reading in the values. Below are the main components of my code but it doesn't look like it is taking in and displaying the values. I cannot tell where the error is. struct Student { string first: string last: double GPA: int id: }: list students: ifstream file: file.open("file.cpp"): while(file > > data.first > > data.last > > data. GPA > > data.id) { students.push_back(data): } file.close(): Student object: coutExplanation / Answer
Below is the compile version of code. I have modified the way you were reading data from file and object declaration:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct Student
{
string first;
string last;
double GPA;
int id;
};
int main()
{
Student object;
int count=10;
ifstream file;
file.open("file.txt");
if(file)
{
while(file.eof()==0)
{
getline(file,object.first);
getline(file,object.last);
file>>object.GPA;
file>>object.id;
}
}
else
{
cout<<"Error in opening file...";
exit(0);
}
file.close();
cout<<"First Name:"<<"Last Name:"<<"GPA:"<<"Student ID:"<<endl;
for(int i=0;i<count;i++)
{
cout<<object.first<<" "<<object.last<<" "<<object.GPA<<" "<<object.id<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.