3

I'm learning C++ with codeblocks and I'd like to ask if you can post me some good pointers how I can learn the difference between MinGW and Visual studio C++, for example \n and << don't always behave as I'm expecting. I'm complete newbie, only reached day 2 of an old book "Teach yourself C++ in 21 days" by Jesse Liberty and the program looks like this:

#include <iostream>

using namespace std;

    int main()
    {
        cout << "Hello there."; endln;
        cout << "Here is 5: " << 5 << endl;
        cout << "The manipulator endln writes a new line to the screen";
        cout << "Here is a very big number:\t" << 70000;
        cout << "Here is the sum of 8 and 5:\t" << 8+5;
        cout << "Here is a fraction:\t\t" << (float) 5/8;
        cout << "And here is a very big number:\t" << (double) 7000*7000;
        cout << "Remember to replace Niklas with you name";
        cout << "Hampus is a C++ programmer!";
        return 0;
    }

Is MinGW C++ the same as GNU C++? Is there an official standard? any advice for newbie learning / teaching will be appriciated. The book I'm following is "Teach yourself C++ in 21 days" and it is and old edition of the book but I could modify the programs from the first exercise to run and I believe I can use the book since perhaps not many changes were made to the basic C++ since it was published (the edition of the book I own is maybe 10 years old).

Thank you!

Update After getting the recommendations here, I've bought the book C++ Primer.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 5
    "Teach yourself C++ in 21 days" sounds a very big alarm bell!!! You cannot possibly do what the title of this books says. Get yourself a good C++ book, for starters!!! – Tony The Lion Dec 03 '11 at 15:23
  • 1
    So aside from the fact that the book has reasonable reviews (and one of the specific reviews mentioning that it helped someone who wasn't picking up C++ well from other books unlike their CompSci peers), and that I read it when I was 12 or 13 and remember it being fairly accessible, why don't you *suggest* a book? – Doug Moscrop Dec 03 '11 at 15:41
  • Thank you. I also have a book on Computer Graphics (C version with OpenGL) and the Kernigham Richie "The C Programming Language" but this is for absolute beginning when I teach my siste's son C++. He is 11 years old and can change programs and I want to teach him how to write his own programs and computer graphics. – Niklas Rosencrantz Dec 03 '11 at 15:52
  • 1
    That's great for you to do that - I wasn't quite ready to grapple with OpenGL/DirectX until I was 14 and in retrospect, something like C# and XNA or OpenTK would be much better, especially at 11, to get results faster. I think I spent too much time getting *one thing* to work, and I would have been better off had I first looked at higher level concepts and an overall sense of how programs work. Do you think he's going to like spending hours fiddling with C and C++ or would he rather have a model on the screen? Food for thought. – Doug Moscrop Dec 03 '11 at 16:13
  • 1
    I learned C++ with same book when I was 13 (13 years ago) and I thought it was a great book. If I remember correctly it covered pointers very well. Regardless a lot to learn in there. In modern times maybe a language like Python or a modern C++ book would be a better choice. One that uses STL. – Joe McGrath Dec 03 '11 at 16:21
  • 1
    I recommend [C++ Primer](http://www.amazon.com/Primer-4th-Stanley-B-Lippman/dp/0201721481/) as a good C++ book that helped me to bridge the gap between C and C++. In any case, one should not expect to learn C++ (not even C) in 21 days. 210 days would be more appropriate and even then it would depend a lot on what's done and how and likely stretch that to 2100 days. I'm serious. – Alexey Frunze Dec 03 '11 at 16:22

4 Answers4

3

The only thing that compiler has to enforce (or SHOULD enforce :P) is stuff that's written in standard. Standard defines behavior, not the implementation, therefore compilers can differ.

First of all, code::blocks is not a compiler, it's an environment in which people usually use MinGW compiler. Visual Studio on the other hand is an environment that just happens to come with it's own compiler.

Differences between compilers should be of no concern for you as a beginning c++ programmer and text editor or dev. environment you want to use is up to you, you can program (write the code) in PSpad and then you can compile it with million different compilers.

To sum it up, there's a standard to which compilers have to (or should) comply. Compilers implement that in whatever way they like and they may add some extra things (like variable size arrays static allocation.) Notice that standard knows nothing about those extensions and therefore doesn't define their behavior.

Then there's a text editor of some kind, in which you write your code and compile it with whatever compiler your heart desires.

C++ is not an easy language to learn unless you have prior experience with lower-level (still high level :)) programming language. There's a lot going on especially if you haven't encountered pointers and references yet.

I suggest you get a new book possibly even containing information on c++0x / c++11 standard which was officially released couple of months ago.

Also, don't use (double)x this kind of typecasting in c++ since it's really a c-way of casting types. Use static_cast < double > (x) in this scenario. (There are other ways to cast too.)

ScarletAmaranth
  • 5,065
  • 2
  • 23
  • 34
  • Thank you for answering. My project is to teach the fundamentals and beginner concepts of C++ to my sister's son who is 11 years old, and also to rehearse my self and learn myself since I used to program C. – Niklas Rosencrantz Dec 03 '11 at 15:55
  • 1
    @Nicke INtorducing programming concepts with c++ :)? Interesting :) – ScarletAmaranth Dec 03 '11 at 16:04
  • Yes the student wants to learn OpenGL and I can teach that in C with the GLUT library and he is very interested in computer graphics so we'll also use the book called Computer Graphics http://www.amazon.com/Computer-Graphics-C-Version-2nd/dp/0135309247 – Niklas Rosencrantz Dec 03 '11 at 23:35
  • 1
    @Nicke Well i wish you best of luck then :) I reckon computer graphics is a tad too difficult beginner programmers not to mention he's 11 (NOT TO underestimate!). I mentioned his age because at the end of the day it requires certain knowledge of linear algebra, vector analysis and similar topics. – ScarletAmaranth Dec 04 '11 at 00:07
2

Just a comment to "the difference between MinGW and Visual Studio C++":

Both MinGW and Visual C++ comes with both the standard C/C++ library, and the Windows API libraries. In addition to these, the MinGW package contains lots of POSIX C/C++ libraries, for example dirent.h or pthread.h, which can be useful to write cross-platform console programs on Windows (I did this when I developed simulation programs on Windows, to run on SEE-GRID).

kol
  • 27,881
  • 12
  • 83
  • 120
0

cout << "Hello there."; endln;

you have an error there of (I assume) a ; in the middle of the line.

The << operator should work as defined in the C++ standard. It's not up for discussion between mingw and visual studio, or any other compiler.

The \n however has implications for Windows and Linux environments. In Linux, every line is ended with a \n character. In Windows it ends with a LF (line feed) and CR (carriage return). ie, \r\n. This is why a linux text file will look very different when displayed on Windows, and vice-versa.

In any case, the difference is nothing to do with the compiler, but with the line-end characters on the different operating systems.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • Thank you for teaching me this. – Niklas Rosencrantz Dec 03 '11 at 16:38
  • 1
    Amazing how many people who are otherwise proficient with C++ are confused about endl. checkout http://stackoverflow.com/questions/213907/c-stdendl-vs-n `\n` is end of line in the stream, not directly in the file/output. ostream changes this to the correct version for the platform unless in binary mode. – Joe McGrath Dec 03 '11 at 16:53
  • This was a good lesson 1. Thanks @Joe McGrath for teaching me – Niklas Rosencrantz Dec 03 '11 at 17:51
  • 1
    Not really - Joe obviously does remember, or does not know, that STDOUT is line-buffered. So whenever you write "\n" to it it will flush the output... – DejanLekic Dec 03 '11 at 18:02
0

Is MinGW C++ the same as GNU C++?

Yes, MinGW is basically a distribution of GNU tools for Windows (MinGW = Minimalist GNU for Windows). It contains a GNU C++ compiler among compilers for other programming languages, that are part of the GCC.

Is there an official standard?

There is a standard for C++ as a language. However, there are no "standards" for C++ implementations. Sure, the implementations should follow C++ the standard as close as possible. There parts of the C++ standard that state that something is "implementation specific" - then it is up to the implementation how to deal with that particular case.

The problem you mentioned about "\n" (line-feed character) is purely platform specific. If you only use std::cout, then "\n" will work, but if you use print() or printf(), or some other means to write to the STDOUT, then you need to be careful what you do. I see from your post that you use Windows, where new line is "\r\n" (carriage-return + line-feed). On most POSIX platforms "\n" is the the "end of line" character, on Windows it is the "\r\n". On MacOS X it was "\r", now is "\n".

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
  • 1
    This answer is wrong in regards to `\n` on windows it is changed by cout to `\r\n`. `std::endl` forces a flush of the buffer. It is argued all over the internet and this site if you need more information. cout is aware of the platform, not just `std::endl`. – Joe McGrath Dec 03 '11 at 16:35
  • Thank you so much everyone who elaborated and answered my newbie questions. – Niklas Rosencrantz Dec 03 '11 at 16:37
  • 1
    @Nicke Although my learning path was `gwbasic` -> `C++`. I am still learning C++ 13 years later. This is an example of how C++ may not be best suited for starting programing if you want to learn graphics programming because there is alot to learn. There is even debate / confusion on how to end a line in a console program! If the kid you are teaching enjoys knowing the "hard" way instead of getting results I'd stick with C++. If they are results oriented I'd pick a different route. – Joe McGrath Dec 03 '11 at 16:47
  • 1
    @Nicke Side note, I'd also consider using Qt library to start with OpenGL. I am currently using it for simulating a CNC laser's cutting path and it took away a lot of the busy work. – Joe McGrath Dec 03 '11 at 16:51
  • 1
    @Joe McGrath: Fair enough, but if you really want to argue, "\n" will trigger flush because STDOUT is line-buffered (unless you explicitly force it not to be line-buffered). I made a mistake by adding a sentence about std::endl, as that paragraph was about line-edings in general, not about cout. (Note nowhere in that thread i mention cout, nor i said that cout is not aware of the platform!) – DejanLekic Dec 03 '11 at 17:18
  • 1
    @DejanLekic This is something that is often taught incorrectly. I was not aware of it for a long time. Just trying to avoid new programmers from the confusion too. Line ending history is good to know and understand though. Just incorrectly justifying use of std::endl here. `\n` will work fine to end a line. – Joe McGrath Dec 03 '11 at 17:45
  • 1
    I think what is confusing here is that you think about cout, while I think about STDOUT in general. No matter what you use to send bytes to STDOUT, the moment it encounters "\n" it will flush by default. – DejanLekic Dec 03 '11 at 17:50
  • Joe, read this please. http://stackoverflow.com/questions/156278/does-setbuf-affect-cout . More precisely the second comment in this thread. – DejanLekic Dec 03 '11 at 18:03
  • Erm, isn't `\n` the standard newline on OS X? I thought `\r` was a much older standard that has since been left behind. Wikipedia seems to agree with me: http://en.wikipedia.org/wiki/Newline – jpmc26 Jul 29 '14 at 00:14