473,396 Members | 1,907 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,396 software developers and data experts.

trouble with >> overload

Hello,
I've overloaded the istream operator >> to output the private
data members of my class Word, i'm using the operator go get
data from an input file with something like
//--snip--
#include <iostream>
#include <fstream>
#include <cstdlib>
int main(int argc, char* argv[]) {
ifstream in(arcv[1]);
//...
word W;
while(!in.eof()) {
in >> W;
cout << W;
}
//--snip--
below is the class and overload declarations, the problem is that
if I attempt to read in a file with 16 zeroes in it, the above code
will print 17 zeroes to counsole.
I suspected after looking thru with a debugger, that the blank char
at the end of the file is being read into istream and instantiating
a word object with the default constructor(no args). But when I change
this constructor to
this->wr = 0xfff;
the 17th digit does not become 0xfff or its decimal equvalent, it is
still zero.

Any Ideas as to what is happening here?

class word {
private:
unsigned long wr;
public:
word(void) {
this->wr = 0;
}
unsigned long value(void) {
return (this->wr);
}
void set(char* s_wr) {
this->wr = strtoul(s_wr,'\0',16);
}
};
extern istream &operator >> (istream &in, word &wr);
extern ostream &operator << (ostream &out, word &wr);
//////////////////////////////
istream &operator >>(istream &in, word &wr) {
char* line;
line = new char[33];
in >> ws;
in >>line;
if (in.bad()) return(in);
wr.set(line);
delete[] line;
return (in);
}

ostream &operator <<(ostream &out, word &wr) {
out << hex << wr.value()<<endl;
return(out);
}
/////////////////////////////
THanks,
Jaime

--
The opinions expressed in this message are mine and not
those of my employer.
Jul 19 '05 #1
0 1726

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
8
by: maths_fan | last post by:
Can't understand how to overload these operations and for what reason?
2
by: dave | last post by:
I have 2 pages... in the first page i check to see for the existance of the cookie, if found do something else set the value and set the cookie. (cookie gets set ok) I use the following code to...
2
by: Phil C. | last post by:
Hi. I'm having trouble translating a routine to alter validation at the client and the server. A portion of the C# code is: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
11
by: Ashwin | last post by:
hi guys, i have overloaded >operator for my own string class .the function is as given below istream& operator>>(istream &in,string1 &s) { char *a; a=new char; in>>a; s=a;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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...

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.