472,102 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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<<(ostream& osObj, const lab2Data &data);
friend void operator<<(istringstream& isObj, const lab2Data &data);
};

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

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

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

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

/* End Code */
Jul 22 '05 #1
4 2171
"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<<(ostream& osObj, const lab2Data &data);
That seems OK.
friend void operator<<(istringstream& 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.com (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<<(ostream& osObj, const lab2Data &data);
friend void operator<<(istringstream& isObj, const lab2Data &data);
That should be:
friend istream& operator>>( istream& isObj, lab2Data& data );
};

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

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

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

void operator<<(istringstream &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(file, myString))
{
isObj.str(myString); //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::lab2Data( string A, int B, double C )
{
ID = A;
inum = B;
fnum = C;
};
lab2Data::lab2Data()
{
ID = "";
inum = 0;
fnum = 0;
};
ostream& operator<<(ostream& 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(file, myString))
{
istringstream isObj;
isObj.str(myString);
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::lab2Data( string *A, int B, double C )
{
ID = A;
inum = B;
fnum = C;
};

lab2Data::lab2Data()
{
};
void lab2Data::setString( string *A)
{
ID = A;
};

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

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

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

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

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

ostream& operator<<(ostream& 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Carl Bevil | last post: by
4 posts views Thread by Brandon | last post: by
6 posts views Thread by James Aguilar | last post: by
6 posts views Thread by PengYu.UT | last post: by
11 posts views Thread by icanoop | last post: by
4 posts views Thread by clb | last post: by

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.