logical error with Fstream
Oct 24, 2016 at 2:50am UTC
i have to initializes data from file and use array of struct to output the final result
my text File
1 2 3
Sam Albert 18 20162176110 86.7 Math101 CS101 Bio101
Sara Ali 21 20150091030 65.2 CS112 Physics102 CIS240
John Mathio 20 20160175098 70.8 English112 NES201 EE212
My code
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct PersonInfo {
string FirstName;
string LastName;
int Age;
};
struct StudentInfo {
PersonInfo HumanBeing;
unsigned long long int UniversityID;
double GPA;
string Courses[3];
};
void print (StudentInfo s[] , int size )
{
for ( int i = 0 ; i < size ; i++)
{
cout<<"Full Name :\t" <<s[i].HumanBeing.FirstName<<" " <<s[i].HumanBeing.LastName<<endl;
cout<<"Unversity ID : " <<s[i].UniversityID<<endl;
cout<<"Age : " <<s[i].HumanBeing.Age<<endl;
cout<<"GPA : " <<s[i].GPA<<endl;
for ( int j = 0 ; j <= i ; j++){
cout<<"Course Taken : " <<s[i].Courses<<" " ;
}
}
cout<<endl;
}
void initializes (StudentInfo s[], int x)
{
ifstream i;
ofstream o;
i.open("lol.txt" );
while (!i.eof())
{
for (int j = 0; j < 3; j++)
{
for (int b = 0; b < 3; b++)
{
i >> s[j].HumanBeing.FirstName >> s[j].HumanBeing.LastName >> s[j].HumanBeing.Age >> s[j].UniversityID >> s[j].GPA >> s[j].Courses[b];
}
}
}
}
int main() {
ifstream i;
ofstream o;
int x = 3;
StudentInfo s[3];
initializes(s, x);
print (s , 3);
}
Last edited on Oct 24, 2016 at 2:52am UTC
Oct 24, 2016 at 2:52am UTC
Remove an unnecessary for loop.
1 2 3 4
for (int b = 0; b < 3; b++)
{
i >> s[j].HumanBeing.FirstName >> s[j].HumanBeing.LastName >> s[j].HumanBeing.Age >> s[j].UniversityID >> s[j].GPA >> s[j].Courses[b];
}
Should be :
i >> s[j].HumanBeing.FirstName >> s[j].HumanBeing.LastName >> s[j].HumanBeing.Age >> s[j].UniversityID >> s[j].GPA >> s[j].Courses[b];
You don't have good variable naming in some of your variables it seems.
Oct 24, 2016 at 5:48am UTC
do you mean this
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct PersonInfo {
string FirstName;
string LastName;
int Age;
};
struct StudentInfo {
PersonInfo HumanBeing;
unsigned long long int UniversityID;
double GPA;
string Courses;
};
void print (StudentInfo s[] , int size )
{
for ( int i = 0 ; i < size ; i++)
{
cout<<"Full Name :\t" <<s[i].HumanBeing.FirstName<<" " <<s[i].HumanBeing.LastName<<endl;
cout<<"Unversity ID : " <<s[i].UniversityID<<endl;
cout<<"Age : " <<s[i].HumanBeing.Age<<endl;
cout<<"GPA : " <<s[i].GPA<<endl;
cout<<"Course Taken : " <<s[i].Courses<<" " ;
}
}
void initializes (StudentInfo s[], int x)
{
ifstream i;
ofstream o;
i.open("lol.txt" );
while (!i.eof())
{
for (int j = 0; j < 3; j++)
{
i >> s[j].HumanBeing.FirstName >> s[j].HumanBeing.LastName >> s[j].HumanBeing.Age >> s[j].UniversityID >> s[j].GPA >> s[j].Courses;
}
}
}
int main() {
ifstream i;
ofstream o;
int x = 3;
StudentInfo s[3];
initializes(s, x);
print (s , 3);
}
still not not working
Oct 24, 2016 at 6:34am UTC
1 2 3 4 5 6 7
while (!i.eof())
{
for (int j = 0; j < 3; j++)
{
i >> s[j].HumanBeing.FirstName >> s[j].HumanBeing.LastName >> s[j].HumanBeing.Age >> s[j].UniversityID >> s[j].GPA >> s[j].Courses;
}
}
Should be :
1 2 3 4 5
while (i)
{
i >> s[j].HumanBeing.FirstName >> s[j].HumanBeing.LastName >> s[j].HumanBeing.Age >> s[j].UniversityID >> s[j].GPA >> s[j].Courses;
j++;
}
Oct 24, 2016 at 6:37am UTC
i.open("lol.txt" );
Should be :
1 2 3 4 5 6 7 8
string fileName("lol.txt" );
i.open(fileName.c_str());
while (!i.is_open())
{
cout << "The file " << fileName << " not found. Please enter another file name : " ;
cin >> fileName; i.open(fileName.c_str());
}
Topic archived. No new replies allowed.