I/o Problems..

So for this program... I'm trying to read from a .txt file in a very specific format. I have a function...that looks like 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
void Read_Roster(Maple Roster[], int &count)
{
   cout << "Reading Function" <<endl;

//-------------------
// Opening input file
//-------------------
ifstream din;
din.open("south.txt");
if (din.fail())
cout << "Could not open File" << endl;

//--------------------------------
// Reading file.
//--------------------------------
string str;
getline(din, str);

//--------------------------------
// Loop reading file
//--------------------------------

for (int i=0; i<count && !din.eof(); i++)
        {
        getline(din, str);
        Roster[i].setName(str);


        getline(din, str);
        Roster[i].setID(str);


        getline(din, str);
        Roster[i].setRoom(str);
    

        getline(din, str);
        Roster[i].setCombo(str);


        getline(din, str);
        Roster[i].setEMail(str);


        getline(din, str);
        Roster[i].setMB(str);


        Roster[i].print();
        }
//--------------------------------------
// Closing input file
//--------------------------------------

din.close();

}


for some reason it is not working... at all...
the .txt file looks like this:

Wayne, Bruce
15813u513
BA-214 FOB # 13513
14-13-12
a@whatever.edu
MB 0313
Wayne, John
15813u513
BA-114 FOB # 16144
14-13-22
ab@whatever.edu
MB 0323
Wayne, Frank
15813u513
BA-314 FOB # 11333
14-33-12
ac@whatever.edu
MB 0413


i have each line reading as a string...and when i try to print..it does not work..

my print function looks like this..

1
2
3
4
5

void Maple::print()
{
cout <<"Name: " << Name << "ID: "<< ID  <<"Room: "<< Room<<"Combo: " << Combo <<"E-Mail: " << EMail << "MB: " << MB <<endl;
}
Last edited on
When you say 'does not work' what do you mean? Give us examples of your output.
when i do ./a.out it simply terminates.. i tried putting cout<<"test"<<endl; in between each setName, setID, setCombo, setMB, setEMail but it does not work..i made sure that it recognizes the .txt file and the contents..but i'm stumped.
I mean..if i'm setting each line of code from the file, shouldn't it print after each iteration?
Last edited on
Your problem isn't in that function, then, because if you're not getting at least "Reading Function" printed to the console, then you're never even entering the function that reads from south.txt
cindyath@tuning:~/test/testl$ ./3
Test program
Reading Function
I know for certain it's the function.. I'm just rather amused as to what in the world could be the error.
Show the declaration of your Maple class.
Sure!

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

Maple::Maple()
{
Name = " ";
ID = " ";
Room = " ";
Combo = " ";
EMail = " ";
MB = " ";
}


void Maple::setName(string name)
{
Name = name;
}

void Maple::setID(string id)
{
ID = id;
}

void Maple::setRoom(string room)
{
Room = room;
}

void Maple::setCombo(string combo)
{
Combo = combo;
}

void Maple::setEMail(string email)
{
EMail = email;
}

void Maple::setMB(string mb)
{
MB = mb;
}

:P That's the definition. I wanted the declaration.
function being called from main works....


Read_Roster(Roster,count);


header file...

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
class Maple
{
public:
        Maple();
        ~Maple();
        void print();

        string getName();
        string getID();
        string getRoom();
        string getCombo();
        string getEMail();
        string getMB();

        void setName(string name);
        void setID(string id);
        void setRoom(string room);
        void setCombo(string combo);
        void setEMail(string email);
        void setMB(string MB);

private:

        string Name;
        string ID;
        string Room;
        string Combo;
        string EMail;
        string MB;
};

Your declaration (.h file) says
void setMB(string MB); // Note the variable name in uppercase

Your definition (.cpp file) says
void Maple::setMB(string mb) // Note the variable name in lowercase

The variable names don't match in the definition and declaration.
okay that's fixed...but it's still doing the same thing... there is a problem with the reading function.


cindyath@tuning:~/test/testl$ ./3
Test program
Reading Function
I'm pretty sure that it's not entering the for loop. Track your data through your function and see what's going on with that (especially the count variable). Also, I would take the logical AND && out of the for loop, and just put a while loop around it like:

1
2
3
4
5
while (din.good()) {
    for (int i = 0; i < count; i++) {
        ...;
    }
}
Topic archived. No new replies allowed.