2 days into programming, need some help

Pages: 12
Hi 2 days ago I decided I wanted to get into programming, or at least make a good attempt to learning one. I have done a bit of research on what language to start with first. I settled with C++ and some comments caused some fear in me to tackle this as my first language but here I am. I bought a book called "Sams Teach Yourself C++" and was adviced from a friend to download DevC++ for when I write code and compile.

In Lesson 1 I created hello.cpp, and this was the example the book asked me to input into the compiler

1
2
3
4
5
6
7
#include <iostream>

int main()
{
  std::cout << "Hello World!\n";
  return 0;
}


When this is done, the book ask me to compile and run the code. I should be able to see the words "Hello World!".

My problem is when I go to execute > compile and run option, I get no errors but the program window flashes nothing else. AT first I thought it was int main(); but the book mentions that most compilers dont need the ";" symbol. Either way I am a bit confused right now, sorry if this is way noobish but I only been doing this 2 days and any help would be appreciated.

Thanks in advance
Just dont get into using System("pause") if your read that thread!!! Also, in my own opinion, Codeblocks and Visual C++ are your best options for compilers.
Thank you so far for the replies. I did read the thread linked and I was a bit confused by it all but I think I understand a little (this so far is a lot to take in)

I did however noticed further in my book, it mentioned to check my compiler documentation to see a way to cause the program to pause after execution. (How does one check this?)

The book recommended I use the following

1
2
char response;
std::cin >> response;


I used this and the program stopped flashing. I see the words "hello world!" and then I have to type anything and press enter.

I have a question though, Is Codeblocks free? and If I switch to a different compiler do I still need to input this code in all of my future programs?

THanks again
Last edited on
That's not exactly the best way to pause a program after execution...(though it does work in most cases, I'll admit)

Your best option would probably be
1
2
3
std::cout << "Press Enter to quit . . . ";
std::cin.sync();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

You'll need to have #include <limits> for that to work.

Yes, Code::Blocks is free (see http://www.codeblocks.org/).

Some IDEs (like Code::Blocks and Microsoft Visual C++) have a way to pause the program after execution. (the two I just mentioned do it automatically as far as I can remember, without you having to dig through the settings)
But that's only if you run it through the IDE.

If you want to be able to just double-click on the file and have it pause when it finishes, you'll still need to put that code at the end.
I suggest you try out Bloodshed Dev-C++ 5.0.0.1 compiler,it really is a good compiler,works fine for me
I am also a beginner, what is wrong with the System("pause") command?
This is great advice all around. I feel I learned so much in just posting here.

@Matri X - I will check Bloodshed out.

@ Moschops - That article is amazing I printed it, and will now make sure to break that habit early.

I plan to use long double main's example posted above from now on, or anything that is not system pause :)

I actually decided to download various compilers just to see how the code works for each one. This is a blast thus far, look forward to posting more stuff in the future.

Thanks again
You can also try this. It will wait for you to press a key before exit.

 
std::cin.getch();
I suggest you try out Bloodshed Dev-C++ 5.0.0.1


The new versions, 5 onwards, are just Dev-cpp; no "Bloodshed" (I think - I cannot check as the new web blocker is ridiculously sensitive). Looking for the Bloodshed version will result in finding the bad, old versions.
You could also execute your console programs from a console.
Or adjust the console configuration so it does not close in termination

If I switch to a different compiler do I still need to input this code in all of my future programs?
The issue is with your IDE. What they do is to generate a wrapper that executes your program.
I recommend you to not dirty your code.
@long double main

You don't need to use the cin.ignore() call... just do this...

1
2
3
std::cin.sync();
std::cout << "Press any key to continue...";
std::cin.get();


@apox01

Two mistakes you've made already...

1. Don't use Dev-C++... it comes with an outdated version of the GCC compiler, and is no longer supported. Not only that, but it lacks features, such as auto-complete that make IDEs like Eclipse, Code::Blocks, and Visual Studio great.

2. Don't use "Sam's Teach Yourself C++"... any book that claims it can teach you a programming language in "10 hours a day" or "in 21 days" is terrible and will ruin you. Get a real book like "The C++ Programming Language" by Bjarne Stroustrup (the creator of the language) or a book like "C++, How to Program" by Deitel & Deitel. It has example exercises and projects at the end of each chapter that reinforce what you've learned, which sadly, most books lack.
get rid of return 0; :) it turns off the application
@ packetpirate

I have a friend that said he can give me visual studio 6.0 and that I can use that compiler. Till then I guess I will just use Code::Blocks and ditch DevC++. Is this a good option?

Regarding the book. Ouch it cost me 50 bucks... I am so dissapointed to hear this book is no good. I noted down the other 2 you mentioned and next week will try to get my hands on them. I guess for now after gathering everyones replies this is what my hello world looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//preprocessor signal to fetch files when compiler runs
#include <iostream>
#include <limits>

int main()
{
    using std::cout; //namespace standard library cout
    cout << "Hello World!\n";
    
    
    
    //automatic pause at the end
    cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );        
}


Hopefully this is much better and I took out the return 0; like Noobscrather mentioned.

Thanks again
to do hello world is easy


1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream> 

using namespace std; 

int main()
{
     cout << "Hello World"; << endl;
}






I personally seriously think Code::Blocks is the best C++ IDE out there :)

When you get good with Code::Blocks and your looking for a change you can use gvim and cmd with mingw32-g++ compiler .

1.) you just download gvim

2.) then download Mingw

3.) then add mingw/bin to path variable


Then you can make a makefile using this as a template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
all: app

app: main.cpp 

#-l is  your linker flags

# -I is your include paths

# -L is your lib paths

# you must also put your main.cpp file right after the compiler your using 

mingw32-g++ main.cpp -o appexe -lmingw32 -lwinmm -lSDLmain -lSDL -lSDL_image -lSDL_ttf -lboost_system-mgw46-s-1_49 -lboost_filesystem-mgw46-mt-sd-1_49 -lopengl32 -I"C:\\SDL-1.2.14\\include" -I"C:\\boost_1_49_0"  -L"C:\SDL-1.2.14\\lib" -L"C:\boost_1_49_0\stage\\lib" 


# this is optional, but it's nice
.PHONY: all


Save out as Makefile

Exactly like that ^^

Then you can use mingw32-g++ in cmd.

By cding.

cd means change directory

cd c:/


to where ever your make file is and use the command mingw32-make -f Makefile

to build the Makefile


then you can run the exe by using appname.exe in cmd

and wala :D
Last edited on
By the way, omitting the return 0; at the end of main() won't stop your program from insta-exiting after it finishes.

Just putting that out there...
use a different compiler( i use xcode, eclipse is also VERY nice) if you do not do install a different compiler doing
cout << "Press ENTER to continue...";
cin.ignore();
return 0;

is a good idea, remember that using namespace std is good for beginners before you learn anything about namespaces, so take away the std:: for example

//program by a new guy <-- this is a comment, does not change the program
#include<iostream>

using namespace std;

int main(){
cout << "Hello world" << endl; // endl is like hitting enter so you get a new line
cout << "Press ENTER to continue...";
cin.ignore();
return 0;
}
its not good for beginners, its good for lazy people, the sooner you stop using namespace std the better.
i consider a beginner's guide to c++ by john smiley to be a great starting point. has anyone else read it? i think its good.
Pages: 12