Hi all

I am totally new to C++ and I am using the Dev C++ compiler and getting the tutorial off
http://www.cplusplus.com/doc/tutorial/program_structure.html
I ran the first program (code below) and it compiled fine but when I run it a box appears then dissappears so quickly I dont have time to see what it is but by the black colour I assume its DOS, can anyone tell me why it is doing this please as I don't know if my program is working correctly and I tried the other examples and the same happens, am I doing something wrong is that why it dissappears so quickly?

/ my first program in C++

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

Many thanks

HLA91

Member Avatar for iamthwee

You need to put a cin.get(); before the return 0;

Also the black window is commonly referred to as ze console window

Ok thanks it works now but why didnt the tutorial tell me that, can you recommend a good tutorial for me?

Many thanks for quick reply

HLA91

Member Avatar for iamthwee

> why didnt the tutorial tell me that

Because it ain't got nowt to do with c++, or the semantics of the language anyway. It depends on whether your IDE (integrated development environment) already has a built-in option to pause the program for viewing.

Some don't, some do.

If you ran your program from the command prompt there would be no need for the cin.get(); .

>a good tutorial for me
There are a few useful snippets at daniweb. Look there. But ultimately the best way to learn is to try your hand at a problem and post your queries here.

I have come across another problem again, the code from another tutorial wont be visible

// How IF Works#include <iostream>using namespace std;int main(){    cout << "Enter your age: ";    int age;    cin >> age;        if (age < 20)    cout << "You are still young!\n";    else    cout << "You are not so young anymore.\n";    }

I did try placing cin.get(); before the closing bracket but that didn't work and I also added return 0; incase that helped but nope. Also I cant execute the programs on my laptop running windows Me when I type IF.exe when im in the folder it says syntax error is there something different with the DOS? Many thanks HLA91

commented: horribly formatted code! -4

Unbelievable. 75 posts and you still don't know how to format your code ! I certainly hope you do not plan to turn in that to your instructor.

What do you mean I dont know how to format my code, if you mean placing it in my post I obviously can becasue I did it in the first post of this topic all you did was comress it which to me makes it harder to read. Also I wont be handing it to my instructor becasue this is a hobby not a school subject.

all you did was comress it which to me makes it harder to read. .

Nope -- we didn't do that. In the future please hit the Preview button before posting so that you can see what it looks like.

Because I'm such a sweet guy:

// How IF Works
#include <iostream>

using namespace std;

int main(){    
        cout << "Enter your age: ";    
        int age;    
        cin >> age;        
        if (age < 20)    
                cout << "You are still young!\n";    
        else    
                cout << "You are not so young anymore.\n";    
        
        return 0;  //Yes I did put in 'return 0' because it's bad 
                    //practice to leave it out and yada yada yada...
}

But to help you I guess we will nead a bit more info, the code should execute properly as it is. The problem of 'not visible', is it still in the ide?

>//Yes I did put in 'return 0' because it's bad
>//practice to leave it out and yada yada yada...
You're not getting away with this one. :) Explain why it's a bad practice, please.

>//Yes I did put in 'return 0' because it's bad
>//practice to leave it out and yada yada yada...
You're not getting away with this one. :) Explain why it's a bad practice, please.

He he, well, I thought that at least. Then it's maybe only I who see it as bad practice?
But you asked for an explanation:
A program should exit by telling that it's finished and it all went as planned. Therefor the 'return 0' at the end.

Crap... when I think of it, c++ is the only language where I do this... Well, a old habit then... :)

>Then it's maybe only I who see it as bad practice?
No, you're not the only one, but it is a subjective issue.

>A program should exit by telling that it's finished and it all went as planned.
The language rules say that falling off the end of main will exit as if you said return 0; , so we're not talking about the undefined behavior problem from pre-standard C++ or C89. These two programs are completely identical:

int main()
{
}
int main()
{
  return 0;
}

I can only think of two good reasons not to omit the return statement:

1) Consistency with other functions. main is a special function with special rules. I'm not sure that trying to match main with less special functions is a good idea because it trivializes the differences. This is what causes misunderstandings that produce the void main issue.

2) Symmetry when using an error return. If you're returning failure, you might find it strange not to match that with returning success:

#include <cstdlib>

int main()
{
  //...

  if ( something )
    return EXIT_FAILURE;

  //...

  return EXIT_SUCCESS;
}

I see, thanks for the information :)

Well, If I have to chose one of your reasons I guess number 2 is most equal to my own logics.

Btw... this became quite a nice hijacking of the thread :)

>I guess number 2 is most equal to my own logics.
Then we're of the same opinion.

>Btw... this became quite a nice hijacking of the thread
Threads often go off onto wildly different tangents. It's normal, and often very informative as you'll typically find one or more experts going back and forth.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.