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



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;
Quote:}

//--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 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 {
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);
        }

Quote:};

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

Quote:}

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

/////////////////////////////

THanks,
Jaime
--
The opinions expressed in this message are mine and not
those of my employer.

      [ See http://www.veryComputer.com/]
      [ 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),
    istream_iterator<word>(),
    ostream_iterator<word>(cout));

Uli

--
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !

      [ See http://www.gotw.ca/resources/clcm.htm 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)
{
        myListBox.MoveWindow(0,0,cx,xy,TRUE);

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