Help appreciated with identifying a little bug(intro std, map<> and overloading related)

B

brzozo2

Hello, this program might look abit long, but it's pretty simple and
easy to follow. What it does is read from a file, outputs the contents
to screen, and then writes them to a different file. It uses map<> and
heavy overloading.
The problem is, the output file differs from input, and for the love of
me I can't figure out why ;p

#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <list>
#include <string>
using namespace std;

struct MarkInfo {
string courseid;
int mark;
};
struct Student {
string Name;
list<MarkInfo> marks;
};

typedef map<string, Student> database;

//======================= OVERLOADERS =======================//

istream& operator >>(istream& is, MarkInfo& m) {
is >> m.courseid >> m.mark;
return is;
}
istream& operator >>(istream& is, list<MarkInfo>& l) {
MarkInfo m;
while(is >> m) // >> needs overload wrt MarkInfo type
l.push_back(m);
return is;
}
istream& operator >>(istream& is, Student& s) {
is >> s.Name;
string line;
if(getline(is, line)) {
istringstream istring(line);
istring >> s.marks; // >> requires overloading wrt list<MarkInfo>
type
}
return is;
}
istream& operator >>(istream& is, database& db) {
Student s;
while(is>>s) // >> overload required for type Student
db.insert(make_pair(s.Name, s));
return is;
}

ostream& operator <<(ostream& os, MarkInfo& m) {
os << m.courseid << ' ' << m.mark << ' ';
return os;
}
ostream& operator <<(ostream& os, list<MarkInfo>& l) {
for(list<MarkInfo>::iterator i=l.begin(); i!=l.end(); ++i)
os << *i; //overload required for type MarkInfo
return os;
}
ostream& operator <<(ostream& os, Student& s) {
os << s.Name << ' ' << s.marks; // << needs overloading wrt
list<MarkInfo> type.
return os;
}
ostream& operator <<(ostream& os, database& db) {
for(database::iterator i=db.begin(); i!=db.end(); ++i)
os << i->second << endl; // << needs overloading wrt Student type
return os;
}

//==================== main =========================//
int main() {
//read in from the file
database db;
ifstream infile("myinput.txt");
infile >> db; //requires >> overloading
infile.close();

//list the database what it contains
for(database::iterator i=db.begin(); i!=db.end(); ++i)
cout << i->second << endl; //requires << overloading,
/*how come cout << *i << endl; is not permitted? Doesn't *i
corresponds to database type, in which case
overload for it already supplied? */

//output the database to file
ofstream outfile("myoutput.txt");
outfile << db;
outfile.close();
}
//end of code

Now, if the input file holds:
joey 222-22 33
luke 333-33 99
mike 444-44 56
the screen output (and hence file output)will end up with:
joey 222-22 33
luke 222-22 33 333-33 99
mike 222-22 33 333-33 99 444-44 56
By inspection, it would seem that the Student::marks member, which is
of type list<MarkInfo> is shared by all elements of the database map,
and each new entry just appends the exsisting list<MarkInfo>, even
though the code specifically states to create it separate for each
element of the map(ie. For each Student entry).
Been wresting with this for hours and running out of debugging ideas,
any help would be greatly appreciated :)
 
G

Gianni Mariani

(e-mail address removed) wrote:
....
Your error is here:
istream& operator >>(istream& is, database& db) {
Student s;
while(is>>s) // >> overload required for type Student
db.insert(make_pair(s.Name, s));
return is;
}

----------
....

You reuse the same Student object. You need a new one every time you
call is >> s or at least reset the marks after you're finished inserting
it into the db.
 
B

brzozo2

thank you :)

Gianni said:
(e-mail address removed) wrote:
...
Your error is here:


----------
...

You reuse the same Student object. You need a new one every time you
call is >> s or at least reset the marks after you're finished inserting
it into the db.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top