473,554 Members | 5,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new to c++... need help with istringstream and operator overloading

Hi I'm really new to c++ so please forgive me if this is really basic
but im stuck... I am trying to make a data class that uses
istringstram and overloaded << and >> operators to input and output
data. The data comes in string lines like "OREBlegQ 14854 731.818"
which need to be split into a string, int and double when stored in
the class. Can anyone help? This is what i have so far:

/* Begin Code */

#include <sstream>

using namespace std;

class lab2Data
{
public:
string *ID;
int inum;
double fnum;

lab2Data();
lab2Data( string *A, int B, double C );

friend ostream& operator<<(ostr eam& osObj, const lab2Data &data);
friend void operator<<(istr ingstream& isObj, const lab2Data &data);
};

lab2Data::lab2D ata( string *A, int B, double C )
{
ID = A;
inum = B;
fnum = C;
};

lab2Data::lab2D ata()
{
ID = 0;
inum = 0;
fnum = 0;
};

ostream& operator<<(ostr eam& osObj, const lab2Data &data)
{
osObj << data.ID << " " << data.inum << " " << data.fnum << "\n";
return osObj;
}

void operator<<(istr ingstream &isObj, lab2Data &data)
{
}

/* End Code */
Jul 22 '05 #1
4 2316
"dinks" <di*****@yahoo. com> wrote...
Hi I'm really new to c++ so please forgive me if this is really basic
but im stuck... I am trying to make a data class that uses
istringstram and overloaded << and >> operators to input and output
data. The data comes in string lines like "OREBlegQ 14854 731.818"
which need to be split into a string, int and double when stored in
the class. Can anyone help? This is what i have so far:

/* Begin Code */

#include <sstream>

using namespace std;

class lab2Data
{
public:
string *ID;
int inum;
double fnum;

lab2Data();
lab2Data( string *A, int B, double C );

friend ostream& operator<<(ostr eam& osObj, const lab2Data &data);
That seems OK.
friend void operator<<(istr ingstream& isObj, const lab2Data &data);
Why 'void'? Shouldn't it return the stream object for chaining?
Why operator<<? Shouldn't it be operator>>?
Why 'istringstream' ? Shouldn't it simply be 'istream'?
Why 'data' is 'const'? Shouldn't it be changeable?

What book are you reading about operator>> and operator<< overloads?
};

[...]


V
Jul 22 '05 #2
In article <68************ **************@ posting.google. com>,
di*****@yahoo.c om (dinks) wrote:
Hi I'm really new to c++ so please forgive me if this is really basic
but im stuck... I am trying to make a data class that uses
istringstram and overloaded << and >> operators to input and output
data. The data comes in string lines like "OREBlegQ 14854 731.818"
which need to be split into a string, int and double when stored in
the class. Can anyone help?
class lab2Data {
string ID;
int inum;
double fnum;

friend ostream& operator<<( ostream&, const lab2Data& );
friend istream& operator>>( istream&, lab2Data& );
};
This is what i have so far:

/* Begin Code */

#include <sstream>

using namespace std;

class lab2Data
{
public:
string *ID;
Don't use a string*, use a string.
int inum;
double fnum;

lab2Data();
lab2Data( string *A, int B, double C );
Again, don't use a string*.
friend ostream& operator<<(ostr eam& osObj, const lab2Data &data);
friend void operator<<(istr ingstream& isObj, const lab2Data &data);
That should be:
friend istream& operator>>( istream& isObj, lab2Data& data );
};

lab2Data::lab2D ata( string *A, int B, double C )
{
ID = A;
inum = B;
fnum = C;
};

lab2Data::lab2D ata()
{
ID = 0;
inum = 0;
fnum = 0;
};

ostream& operator<<(ostr eam& osObj, const lab2Data &data)
{
osObj << data.ID << " " << data.inum << " " << data.fnum << "\n";
return osObj;
}

void operator<<(istr ingstream &isObj, lab2Data &data)
{
}


That should be:

istream& operator>>( istream& isObj, lab2Data& data ) {
// put something here.
return s;
}

What do you think would happen if you simply used:

isObj >> data.ID >> data.inum >> data.fnum;

in the above? Have you tried it?

I suspect you are having all kinds of problems because of the string
pointer in the class rather than a string object.
Jul 22 '05 #3
Thanks for the help guys... i got it working the way i need it using
istringstream. I do have one question however... i use a repeat loop
to feed this data object strings of info but for some reason this
object is only receiving the data the first time. Each subsequent time
it just retains the old data. here is the code:

//main.cpp
#include <iostream>
#include "linkedList .h"
#include "lab2Data.h "
#include <fstream>
#include <sstream>
#include <string>

using namespace std;
int main()
{
string fileName;
string myString;
ifstream file;
istringstream isObj;
lab2Data tempData;

//get filename
do
{
cout << "Please supply a file --> ";
cin >> fileName;
file.open( fileName.c_str( ) );
}while(!file.is _open());

//output file
while(getline(f ile, myString))
{
isObj.str(myStr ing); //updates every iteration
isObj >> tempData; //PROBLEM: updates only the first iteration
cout << "My string output:"<< myString << endl; //updates every
iteration
cout << "My string output:"<< tempData << endl;

};
}
//lag2Data.h

#include <sstream>

using namespace std;

class lab2Data
{
public:
string ID;
int inum;
double fnum;

lab2Data();
lab2Data( string A, int B, double C );

friend ostream& operator<<( ostream&, const lab2Data& );
friend istringstream& operator>>( istringstream&, lab2Data& );
};

lab2Data::lab2D ata( string A, int B, double C )
{
ID = A;
inum = B;
fnum = C;
};
lab2Data::lab2D ata()
{
ID = "";
inum = 0;
fnum = 0;
};
ostream& operator<<(ostr eam& osObj, const lab2Data &data)
{
osObj << data.ID << " " << data.inum << " " << data.fnum << "\n";
return osObj;
}

istringstream& operator>>( istringstream& isObj, lab2Data& data ) {
isObj >> data.ID >> data.inum >> data.fnum;
return isObj;
}
Jul 22 '05 #4
Ok i thought i had it... i got everything working with a class
containing a string. As soon as i tried to change the class to as
string* it broke and i cant seem to fix it... HELP! my teacher wants
this done by wednesday and he's not very helpful with my questions.

Here is all of the code:

//main.cpp
#include <iostream>
#include "linkedList .h"
#include "lab2Data.h "
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
string fileName;
string myString;
ifstream file;
stringstream line;

lab2Data tempData;

//get filename
do
{
cout << "Please supply a file --> ";
cin >> fileName;
file.open( fileName.c_str( ) );
}while(!file.is _open());

//output file
while(getline(f ile, myString))
{
istringstream isObj;
isObj.str(myStr ing);
isObj >> tempData;
cout << "My string output:"<< myString << endl;
cout << "My string output:"<< tempData << endl;

};
}
//lab2Data.h
#ifndef H_lab2DataType
#define H_lab2DataType

#include <sstream>

using namespace std;

class lab2Data
{
private:
//Data variables
string *ID;
int inum;
double fnum;

public:
//Constructors
lab2Data(); //default constructor
lab2Data( string *A, int B, double C ); //constructor with 3
parameters

//Set methods
void setString( string *A);
void setInt( int A);
void setDouble( double A);

//Get Methods
string* getString();
int getInt();
double getDouble();

//Input & Output friend functions
friend ostream& operator<<( ostream&, const lab2Data& ); //friend
function for output
friend istringstream& operator>>( istringstream&, lab2Data&
); //friend function for input
};

#endif

//lab2Data.cpp
#include <sstream>
#include <cassert>
#include "lab2Data.h "

using namespace std;

lab2Data::lab2D ata( string *A, int B, double C )
{
ID = A;
inum = B;
fnum = C;
};

lab2Data::lab2D ata()
{
};
void lab2Data::setSt ring( string *A)
{
ID = A;
};

void lab2Data::setIn t( int A)
{
inum = A;
};

void lab2Data::setDo uble( double A)
{
fnum = A;
};

string* lab2Data::getSt ring()
{
assert(ID != NULL);
return ID;
};

int lab2Data::getIn t()
{
assert(inum != NULL);
return inum;
};

double lab2Data::getDo uble()
{
assert(fnum != NULL);
return fnum;
};

ostream& operator<<(ostr eam& osObj, const lab2Data &data)
{
osObj << data.ID << " " << data.inum << " " << data.fnum << "\n";
return osObj;
}

istringstream& operator>>( istringstream& isObj, lab2Data& data ) {
isObj >> data.ID >> data.inum >> data.fnum;
return isObj;
}

//sample textfile to read in
QhXwvaqC 1300 3607.27
mEgckAZu 16197 23796.4
wfOIdpXW 27344 5308.18
JxUjBaal 22189 12154.5
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2205
by: Carl Bevil | last post by:
I'm creating a library for internal use that relies on third-party code. I don't want clients of this library to know anything about the third party code, when compiling or running. Generally I've been achieving this by having an abstract base class which defines an interface, and inheriting a concrete class which defines the...
4
2705
by: Brandon | last post by:
I'm using an istringstream to convert some integers stored in string form to integers. However, each integer is stored in a differnt string. So, I used istringstream's str(string) method for the first string and then used the >> operator to extract the integer. It worked just fine. So, on to the second string. I used str(string) again...
6
2984
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
4
3105
by: ShaneG | last post by:
We have ptr_fun to handle functions, mem_fun to handle member functions that will be called through a pointer, and mem_fun_ref to handle member functions that will be called through a reference. First, why do we need to have seperate mem_fun/mem_fun_ref? The first returns a mem_fun_t, and the second a mem_fun_ref_t. But the only...
6
4684
by: PengYu.UT | last post by:
Hi, I have some problem when I use enum with stream. The code segment is listed below. I know if I change the first line to "int op;", there will not be any error. However, what I really want is of enum type. Is there any other way to fix this problem? Best wishes, Peng
11
2886
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
17
2520
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and operator overloading and virtual functions. I mean it should not hamper the C++ meaning. In frank and simple terms i need to implement a small C++...
2
2239
by: brzozo2 | last post by:
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...
4
2643
by: clb | last post by:
I am trying to use stringstreams and my book doesn't cover the included methods. For example, if I init a istringstream on a string and suck all the data out, then put more stuff into the string, how do I reset the ISS to see the new data? Does this make sense?
0
7580
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7503
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7538
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7869
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6121
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5421
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3531
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.