File Handling and structs

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

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
  #include <iostream>
 #include <fstream>
 using namespace std;

struct Assignments
{
    string courseName;
    string courseCode;
};

void getAssignments(Assignments []);
void displayAssignments(Assignments []);

int main()
{
    Assignments course[5];

    getAssignments(course);
    displayAssignments(course);


    return 0;
}

void getAssignments(Assignments course[5])
{
    ifstream readFile;
    readFile.open("courses.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 < 4 && !readFile.eof(); i++) // loop until end of file
        {
            getline(readFile, course[i].courseName);
            getline(readFile, course[i].courseCode);
        }// end for
    }// end else
}

void displayAssignments(Assignments course[5])
{
    for(int i = 0; i < 4; i++)
    {
        cout << " \nCourse Name : " << course[i].courseName;
        cout << " \n\nCourse Code : " << course[i].courseCode;
    }
}
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:
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
#include <iostream>
#include <fstream>
using namespace 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.
Last edited on
Dude thank you so much! it finally displayed
Topic archived. No new replies allowed.