By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,285 Members | 2,232 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,285 IT Pros & Developers. It's quick & easy.

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

P: n/a
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<MarkInfomarks;
};

typedef map<string, Studentdatabase;

//======================= 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<MarkInfotype.
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<MarkInfois 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 :)

Oct 8 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
br*****@uwindsor.ca 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.
Oct 8 '06 #2

P: n/a
thank you :)

Gianni Mariani wrote:
br*****@uwindsor.ca 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.
Oct 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.