Thread: Please check my C++

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Only if enum's are the same size as int's, which not all compilers will always do
    The C++ Standard makes it clear that "it is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    ofstream as stdout

    How do i pass stdout to ofstream in C++... In C

    Code:
    foo(stdout);
    .....
    .....
    
    foo(FILE *fs)
    {
       fprintf(fs, "sdfdsf"); // go to screen
    }

  3. #33
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you pass a reference of an fstream, like you do in the operator >> and operator << overloads.

    Then you call foo(cout) for example.

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

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    The C++ Standard makes it clear that "it is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int."
    So, clearly
    Code:
    enum e { a, b, c};
    can fit in one byte, whilst
    Code:
    enum e { a, b, c = 256};
    would take up at least two bytes.

    Sure, most compilers make it a 32-bit int. Some have switches to say which size it should use. In other cases, it's entirely up to the compiler.

    If you ever cast pointers or references to enum's to pointer or reference to int, then you should have an assert or similar to detect compilers that doesn't fulfill your expectations.

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

  5. #35
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    So you pass a reference of an fstream, like you do in the operator >> and operator << overloads.

    Then you call foo(cout) for example.

    --
    Mats
    Are you saying i must overload ifstream? Not clear....
    Code:
    void Car::read(ifstream& fin, bool isKeyboard)
    {
    	char make_temp[15], model_temp[15];
    
    	if(isKeyboard)	// Get record from keyboard
    	{	
    		fin >> make_temp >> model_temp;
    		fin >> yearModel;
    		fin >> engineCapacity;
    		fin >> returnDate.day >> returnDate.month >> returnDate.year;
    		fin >> tankStatus;
    		make = make_temp;
    		model = model_temp;
            }
    }
    I may use this function for two purposes, reading from file or keyboard...
    what do i pass to it when calling it if reading from keyboard?

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you saying i must overload ifstream?
    Typically we would overload for std::istream instead since std::ifstream is-a std::istream.

    what do i pass to it when calling it if reading from keyboard?
    std::cin, but you need to overload for std::istream, not std::ifstream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    I may use this function for two purposes, reading from file or keyboard...
    what do i pass to it when calling it if reading from keyboard?
    Code:
    car::read(cin, true);
    --
    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.

  8. #38
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Code:
    car::read(cin, true);
    --
    Mats
    Mats, cin is "istream", but function expects "ifstream", i've change all the ifstreams to istreams, since istream is a base class of ifstream, not other way around.. Compiles happily

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, sorry about that "f" in ifstream, I did mean istream - just got myself a bit carried away, I think...

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

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that your << and >> operators render the read and write members useless.
    Why? Because you can do this:

    std::ofstream out("my file.txt");
    Car car;
    out << car;
    std::ifstream in("my file.txt");
    in >> car;

    It doesn't matter if it's the keyboard of a file, because both derive from std::i/ostream, it works. It's a good example of polymorphism.
    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.

  11. #41
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Note that your << and >> operators render the read and write members useless.
    Why? Because you can do this:

    std::ofstream out("my file.txt");
    Car car;
    out << car;
    std::ifstream in("my file.txt");
    in >> car;

    It doesn't matter if it's the keyboard of a file, because both derive from std::i/ostream, it works. It's a good example of polymorphism.
    Interesting this!!! hmm... will test it, thnx

  12. #42
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Ehm... is fflush(stdout) similar cout.clear() ... ?

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope, it's equalient to
    cout.flush();
    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.

  14. #44
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Nope, it's equalient to
    cout.flush();
    Ok, let me not bit around the bush... In my programming experience i've been flushing the stdin using fflush(), which i've learned is illegal, but work nonetheless... Suggestions are people should use getchar() to clear unwanted characters on stdin... So, just now i tried cin.flush() & doesn't exist... Guess was trying my luck... Is there a C++ way of doing this?

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is there a C++ way of doing this?
    Yes, with cin.ignore() instead. The FAQ has something to say about this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 01: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, 11: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