473,795 Members | 3,323 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 2331
"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
2217
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 implementation. Clients of the library deal only with the base class and request objects of that type from a...
4
2721
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 but this time extraction gave me a seemingly random number (-858993460). Once an istringstream is...
6
3007
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
3116
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 difference between mem_fun_t and mem_fun_ref_t is that they have different operator() methods. But since...
6
4713
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
2918
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
2541
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++ compiler in C++, and i want the intermediate representation to be C. Please help me in this....
2
2262
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 <iostream> #include <fstream> #include <sstream>
4
2654
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
9519
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9037
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.