Hey guys.. my program is building fine and running but my information from the file is not showing instead its showing my error message.. any help would be appreciated
What error message are you getting? It worked fine at my end apart apart from a few stylisitc quirks that I've tried to smoothen out. Make sure that your .txt file and compiler are in the same root folder. Over time, also learn to overload the extraction operator >> (instead of getAssignments()) and the insertion operator << (instead of displayAssignments()). And keep the # of courses as a global variable, SIZE in my example, so that if ever the # of courses changes you can just update one variable accordingly:
#include <iostream>
#include <fstream>
usingnamespace std;
const size_t SIZE = 3;
struct Assignments
{
string courseName;
string courseCode;
};
void getAssignments(Assignments []);
void displayAssignments(Assignments []);
int main()
{
Assignments course[SIZE];
getAssignments(course);
displayAssignments(course);
return 0;
}
void getAssignments(Assignments course[])
{
ifstream readFile("F:\\test.txt");
if(!readFile)
{
cout << " Error Cannot Display \n\n";
return;
}//end if
else
{ // going through the file for reading the information
for(int i = 0; i < SIZE && !readFile.eof(); i++) // loop until end of file
{
if(readFile)
{
getline(readFile, course[i].courseName);
getline(readFile, course[i].courseCode);
}
}// end for
}// end else
}
void displayAssignments(Assignments course[])
{
for(int i = 0; i < 3; i++)
{
cout << " Course Name :" << course[i].courseName<<"\n";
cout << " Course Code :" << course[i].courseCode<<"\n\n";
}
}
Sample Text
Science
101
Maths
102
C++
103 Output
1 2 3 4 5 6 7 8 9 10 11 12
Course Name :Science
Course Code :101
Course Name :Maths
Course Code :102
Course Name :C++
Course Code :103
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.