implicit calling of default ctr by >> ? strtoul problem?

implicit calling of default ctr by >> ? strtoul problem?

Post by Jaime Ashande » Sun, 03 Aug 2003 18:02:54

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
#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;

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 de*, 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 {
        unsigned long wr;
        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);
        delete[] line;
        return (in);


ostream &operator <<(ostream &out, word &wr) {
        out << hex << wr.value()<<endl;


The opinions expressed in this message are mine and not
those of my employer.

      [ See]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


implicit calling of default ctr by >> ? strtoul problem?

Post by Ulrich Eckhard » Tue, 05 Aug 2003 07:40:44

>         word W;
>         while(!in.eof()) {
>         in >> W;
>         cout << W;

after 16 loops, you try to read a 17th word which fails. You ignore that
failure and output the former value again. If reading hadn't failed due to
EOF, you would loop infinitely.

Correct code
copy( istream_iterator<word>(ci),


Questions ?
see  C++-FAQ Lite:  first !

      [ See for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


1. Resizing a CListBox>>>>>>>>>>>>>>>>>>>>>>>>>>>

I have created a CListBox on a view...Now how can I make the listbox always
cover the entire client area of the view, even on resizing.

At the moment im overriding OnSize:-

void CMyView::OnSize(UINT nType, int cx, int cy)

It doesnt do the trick. Help!!

Mr Abtar Bangar          | EMAIL ADDRESS:       |

Southbank University     |                      |  something about EVERYTHING
103 Borough Road         |                      |      but definately not    
LONDON SE1 0AA           |                      |  EVERYTHING about something'
United Kingdom.          |                      |

2. WTB: Vidi Amiga 24 RT

3. " INNOVATION INFOS + IMAGING " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

4. Opening objects via REXX

5. HELP >>>>>>>>>>>>> WINAPI <<<<<<<<<<<<<<<<<

6. What the NREN Really Is

7. Windows 3.x printqueue >>>>>>>

8. radius attributes

9. <<<<<<<< CREATIVE DIRECTOR NEEDED - LONDON >>>>>>>>>>>>>

10. <<<<<<Programmers Needed>>>>>>>

11. <<<<<<<Need program to see the call API functions (win3.11 and w95) >>>>>>>>>>

12. <<>><><><><><><><><><><>

13. EASY ONE GUYS/GALS: const pointers----->>>>>>