473,418 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 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 2222
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>
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.