472,791 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

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

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
2 2160
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: john smith | last post by:
Hi, I have a question about map and classes that contain maps. My problem is declaring any class methods const that would do something to the map. Presumably it's because when operator is...
5
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
3
by: mcassiani | last post by:
Hi, I need use map faster as possible (I store in the map data about open network connections). First a question, this code fragment is from "The C++ Programming........
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
4
by: Stuart Moore | last post by:
Hi, I'm quite new to templates and I seem to be getting myself messed up. I want to write a function that takes a map<T, int> and a set<T>, iterates over the set, and increments the corresponding...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
4
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain...
3
by: eiji.anonremail | last post by:
Hi folks, I have a compile problem on linux, and maybe someone has an idea: #include <map> #include <iostream>
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.