Thread: istream + class

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    istream + class

    HI.. quick implementation question, first off i would like to say im new to the functionality of classes. I understand how they work, however I am having a problem.

    What I need to do is read in a string of numbers, seperated by a character...

    IE:

    3 4 1 2 3 X 4 4 5 3 3 Q 5 5 6 7 3

    I have one class:

    Code:
    private:
         int num1,num2,num3,num4,num5;
         char letter;
    What I need to do is read each group of numbers, and the following symbol
    into a sepeart instance of a class...

    Thanks in advance for any help !!


    IE.
    class1 would read in 34123 X
    class2 would read in 44533 Q

    of each line...... Kind of like an associative array in respect to column numbers each structured as a class..

    I just dont know how I can get multiple instances of a class to read in, and continue reading in properly..

    IE:

    Code:
    class c1;
    class c2;
    class c3;
    while(!infile.EOF()) {
       c1.readfile(infile);  // read first set of nums and char
       c2.readfile(infile);  // read second set of nums and char
       c3.readfile(infile); // read third set of nums;
    }
    
    void class::readfile(istream &in) {
       in >> num1 >> num2 >> num3 >> num4 >> num5 >> letter;
    }
    Any help, or explation of how classes work in respect to istreams would be great...

    Im trying to have two instances of a class read different parts of a file at different points in the file;...

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I see no problem (aside from the class name being a keyword ). Have you tried running your code?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    yes.... It hangs.... im just using the class name as an example, it is not the real name of my class..


    the class is defined as { num1,num2,num3,num4,num5,letter }

    However each line it is reading in is formatted as:

    "3 4 1 2 3 X 4 4 5 3 3 Q 5 5 6 7 3"

    So.. after it reads in the first "letter" it hangs...

    If I create more datamembers in the class to read in the rest of the line..
    ie:
    temp1,temp2......

    etc etc etc.. it reads in and prints back fine..

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    So it reads 3,4,1,2,3 fine then hangs on the X?...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    yes, it never finishes reading the end of the line...

    I also... in my header file have an array declared as

    class_name c[100];

    Later, what I want to be able to do is pull out the data.. lines and process them
    based on the type of character seperating the numbers.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, could you post more code? I can't tell anything wrong with your usage of the istream (actually an ifstream I suppose).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    There really isnt more code...

    MyClass.h
    Code:
    class MyClass {
    private:
       int num1;
       int num2;
       int num3;
       int num4;
       int num5;
       int letter;
    
    public:
      void readfile(std::istream &)
    }
    MyClass.cpp
    Code:
    void class::readfile(istream &in) {
       in >> num1 >> num2 >> num3 >> num4 >> num5 >> letter;
    }
    main.cpp
    Code:
    ifstream inFile;
     inFile.open(argv[1]);
    MyClass c1;
    
    while(!inFile.EOF()) {
      c1.readfile(inFile);
    }
    Last edited by guda; 09-20-2004 at 08:23 PM.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, you're missing a semicolon after the MyClass declaration... and MyClass.cpp defines a function belonging to class 'class', unless that's just copied from above. Also, you're missing a semicolon after the declaration of readfile() in the class declaration. Next, EOF() isn't a member of a std::ifstream, it's eof(). Finally, 'letter' is an int, not a char.

    After fixing these bugs, it worked fine as far as I could tell through running the debugger. I'm surprised you didn't get compiler errors on these...

    **EDIT**
    Also, just realized... the reason it was hanging was because your loop condition was .eof(). The problem was that after trying to input to 'letter' a character (while letter is an int), it set an error flag on the file - so no input operations would complete successfully, and you would never reach eof() no matter how many times you read from the file (also occurs when the file isn't opened successfully). By changing it to while(inFile) you can fix that problem.
    Last edited by Hunter2; 09-20-2004 at 08:55 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    ah ha..

    actually the syntactial errors were from just typing in default values and not the *actual code...

    In my real program. letter is declared as char...

    sorry about that..

    But the while(inFile) instead of while(!infile.eof())

    worked..

    however does this only read in one line? or does it read in the whole file?

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>actually the syntactial errors were from just typing in default values and not the *actual code...
    All that work for nothing! lol

    >>however does this only read in one line? or does it read in the whole file?
    Well, (inFile) is the same as (inFile.good()) I think, or at least (!inFile.fail()). So it acts the same as .eof(), except that it also catches other errors. I'll leave it to you to decide whether it reads the whole file or not
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    thanx for the help !!.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No problem Always glad to be of service.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM