Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp What you need is something like this: Code: return (a.x == b.x)? a.y < b.y : ...

  1. #196
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    What you need is something like this:
    Code:
       return (a.x == b.x)? a.y < b.y : a.x < b.x;
    Or, if we use if-statement instead of the ternary operator:
    Code:
       if (a.x == b.x) return a.y < b.y;
       return a.x < b.x
    --
    Mats
    This function is non-static meaning it takes one argument... I think your example is based on static function which actually receives the two objects (a & b)...

  2. #197
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    This function is non-static meaning it takes one argument... I think your example is based on static function which actually receives the two objects (a & b)...
    Yes, I was just demonstrating "how you compare two-level objects", not concerned with any particular implementation you may choose to use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #198
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by csonx_p View Post
    Thanx mats for explaining boost..
    Actually, boost is a collection of libraries for C++. Some of which comes in handy very much.
    What I showed in my example is called integer traits. Basically, it defines various properties of certain types.
    It provides constant numbers for maximum and minimum values the types can hold along with something else and gets the rest of the information by deriving from STL's integer traits.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #199
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Actually, boost is a collection of libraries for C++. Some of which comes in handy very much.
    What I showed in my example is called integer traits. Basically, it defines various properties of certain types.
    It provides constant numbers for maximum and minimum values the types can hold along with something else and gets the rest of the information by deriving from STL's integer traits.
    My question was why use this with stdin.... Is it to allow all types and size of data to be ignored/cleared? Where else are boosts commonly used? a simple example maybe!

  5. #200
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    new line at eof

    When i write records on a text file i use '\n' at the end of each record so that all records can be on separate lines... Now the challenge is there's always going to be an empty line a at the last record... Been thinking of a way to avoid cause when reading from same file, this record reads as junk... any ideas??

  6. #201
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you read using >> x, then it will "fail" if it can't read valid data after the newline, so all you need to do is to check the result of the read operation. Fortunately, that's easy, because the bool operator of the stream class returns false if the stream encountered an error (such as end of file).

    So just do
    Code:
    if (!fin >> car) 
       // Reached end of file, stop reading 
    else
       // process car (e.g. insert into vector).
    Note that it doesn't matter that we tried to read the ENTIRE car record - it will fail when it reaches end of file, and assuming your data file is correctly built, that would happen just after your last end-of-line marker.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #202
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    If you read using >> x, then it will "fail" if it can't read valid data after the newline, so all you need to do is to check the result of the read operation. Fortunately, that's easy, because the bool operator of the stream class returns false if the stream encountered an error (such as end of file).

    So just do
    Code:
    if (!fin >> car) 
       // Reached end of file, stop reading 
    else
       // process car (e.g. insert into vector).
    Note that it doesn't matter that we tried to read the ENTIRE car record - it will fail when it reaches end of file, and assuming your data file is correctly built, that would happen just after your last end-of-line marker.

    --
    Mats
    as this is my record
    Code:
    	in >> make >> model;
    	in >> regNo;
    	in >> ymodel;
    	in >> engCap;
    	in >> ts;
    i'm guessing then it would be more appropriate to check the first field

    i.e
    Code:
    if (!in >> make ) 
       return -1; // Can i return -1 to istream?
    else
    {
            in >> model;
    	in >> regNo;
    	in >> ymodel;
    	in >> engCap;
    	in >> ts; 
    }

  8. #203
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    as this is my record
    Code:
    	in >> make >> model;
    	in >> regNo;
    	in >> ymodel;
    	in >> engCap;
    	in >> ts;
    i'm guessing then it would be more appropriate to check the first field

    i.e
    Code:
    if (!in >> make ) 
       return -1; // Can i return -1 to istream?
    else
    {
            in >> model;
    	in >> regNo;
    	in >> ymodel;
    	in >> engCap;
    	in >> ts; 
    }
    Well, in that case, you should check ALL of them - what if you have make, model, regno <end of file> because of some system error (full disk, power failure/system crash)?

    Edit: Just checking if(!in) after you have read all the fields [any or all of which may have failed] will tell you if it failed, or on the else-side, succeeded.
    Edit2: If it's a operator>> then you will return in itself, and that will be sufficient to check at the calling point. No need to return a bool/integer value from this function in that case.

    --
    Mats
    Last edited by matsp; 07-22-2008 at 02:43 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #204
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Edit2: If it's a operator>> then you will return in itself, and that will be sufficient to check at the calling point. No need to return a bool/integer value from this function in that case.
    calling point ...?
    Code:
    void Car::Read(istream& indata, Car& car, bool isKeyboard)
    {
    	indata >> car;
    }
    If i check here. what do i check for, indata.fail() ?

    OR

    Code:
    ifstream fleetin;
    car.Read(fleetin, car, false);
    If i check here, fleetin.fail()?

    ... just checking ..

  10. #205
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could just do:
    Code:
    istream& Car::Read(istream& indata, Car& car, bool isKeyword)
    {
        return indata >> car;
    }
    and use it like:
    Code:
    if (!car.Read(fleetin, car, false) {
         //input failed
    }
    Then, in case of failed input you could use more specialized functions to find out what went wrong (if that is important to you).

    Also, in the above usage, what is the point to pass car as an argument if this function is already called on the car instance and car is already passed as the this pointer. Simpler:
    Code:
    car.Read(fleetin, false);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #206
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by anon View Post
    Also, in the above usage, what is the point to pass car as an argument if this function is already called on the car instance and car is already passed as the this pointer. Simpler:
    Code:
    car.Read(fleetin, false);
    how do i read with "this" pointer?

    Code:
    void Car::Read(istream& indata, bool isKeyboard)
    {
    	indata >> this;
    }

  12. #207
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You dereference it like other pointers:
    Code:
    indata >> *this;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #208
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want *this, as this is a pointer to car, which won't be available as a "read into this type" overloaded operator>> .

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #209
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    vector vs dequeue vs maps

    For this program, which would you recommend and why (advantages)?

  15. #210
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seeing as you need to have a car to read in the value anyways, you may just as well use the "this" pointer that is already there. Is Car::Read a static function? If not, it is being passed "this" as well as car, so if you do car.Read(... car ...) then you are effectively passing car along twice - once as this, and second as a parameter. Seems unnecessary, and there's no purpose (or advantage) to having a Car reference parameter to a function that requires to have a Car object to be called. Since "simpler is better", this gives the advantage to the model that anon is suggesting.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21