14
donuts
7y

OMFG... Should I continue reading this book on C++....

Comments
  • 4
  • 1
    Hmm? It looks like the C++ I've had to do in classes, seems pretty textbook-standard
  • 6
    I think this looks better?

    Plus that if (grade >=70) is just WTF?
  • 2
    Looks intentional yet extremely nasty
  • 5
    @billgates I think it's probably just an early example to show how to use nested ifs. Most textbooks have that in the first couple of chapters.
  • 3
    Now they introduced auto....

    auto a = 1; fine...

    but auto sum(int x, int y)

    why would you ever use auto for a function declaration?

    Is C++ turning into JS?
  • 4
    Why are you using "using namespace std"? Not that cats will die or anything but it can be dangerous using it. I always write std::cout or std::string
  • 0
    @b3b3 where am I using namespace std?
  • 1
    @billgates you're not writing 'std::string', I guess that's his point. Indirectly you're using the namespace Std by this.

    Personal flavor I guess, as long as one does not run into problems. 😃
  • 0
    @Emphiliis I used typedef string std::string?

    Isn't that just like an alias? bc I don't want to type std all the time?
  • 1
    @billgates yeah by using "using namespace std;" you wont have to type std all the time. But why the heck did you typdef string? Never seen that before 😂😂
  • 2
    @billgates you missed a semicolon
  • 2
    @billgates if the grade is lower than 70 you don't need to make another 2 ifs, this is a bit faster, probably not significantly but this way it can make you to think before writing slow code
  • 0
    @AlexAC35 Indeed. If this was looping through thousands of grades you could avoid a lot of unnecessary ifs since we can expect that only a few will be in the 80-100 area.
  • 0
    (Also please add an "<< std::endl;" at the end of your cout, tyvm)
  • 0
    @b3b3 because they went over typedef in chapter 1...

    Also I'm a C# Java guy... std::string just feels strange...

    Why did they even call the namespace std.... Every single time I read it I think of STDs....
  • 1
    @endor \n > std::endl

    @billgates instead of writing
    typedef std::string string;
    You could've just written
    using std::string;
  • 0
    @Krokoklemme will I have the type def in a header file and then just include it in all the cpp files.

    Would using work as well?
  • 0
    @Krokoklemme endl flushes the buffer, \n doesn't.
    Personally, unless I have that specific need, I stick with endl, to avoid any unexpected weird behaviour (and because when I tell my program to print stuff, I want it there and then)
  • 0
    @endor that's just partially true. stdout is newline-buffered, which means that it'll get flushed every time it encounters a newline

    @billgates yes, 'using' would work too (and is actually preferred, as it's the "C++ way")
  • 0
    @Krokoklemme but not cin?

    I think I had to use cin.ignore so the outer function wouldn't also get the value
  • 0
    @Krokoklemme but Windows newline != Linux new line?
  • 0
    @billgates a \n is a \n on every OS

    And flushing stdin is undefined behavior anyway
  • 0
    @Krokoklemme but when I have a shell script written in Windows... I need to convert the EOL characters to Linux ones or else it won't run?
  • 0
    @billgates that's really just a matter of displaying stuff. Windows can handle "raw" newlines just as good as Linux does.
  • 0
    @Krokoklemme nope... If the sh file isn't Linux EOL formatted, shell will complain.

    http://bencane.com/2014/02/...
  • 1
    @billgates errrmmm... I know, I was talking about that Windows EOL doesn't necessarily have to be \r\n
  • 0
    @Krokoklemme I thought we were talking about how endl is the same as \n and \n will work for any system.

    But there is a diff though in endl depending on OS?
  • 0
    @billgates \n and endl differ in that way, that endl guarantees the stream it's applied upon to be flushed and yes: \n does work for every system (AFAIK at least)

    But idk if there are any differences in endl depending on the platform
Add Comment