0

I am learning C++, and I am trying to make a simple program which prints 5 variables, as my book said to do this, but it is not executing my code.

#include<iostream>
using namespace std;
int main()
{
    //Program Code below
    return 0;
    char letter;    letter = 'A'; //Declared, then initialized
    int number;    number = 100; //Declared, then initialized
    float decimal = 7.5;   //Declared AND initialized
    double pi = 3.14159;   //Declared AND initialized
    bool isTrue = false; //Declared AND initialized
    cout<<"Char letter: "<<letter<<endl;
    cout<<"Int number: "<<number<<endl;
    cout<<"Float decimal: "<<decimal<<endl;
    cout<<"Double pi: "<<pi<<endl;
    cout<<"Bool isTrue: "<<isTrue<<endl;
}
1
  • 3
    Those declared, then initialized statements are wrong. It's declared, then assigned to. Commented Aug 27, 2013 at 13:05

5 Answers 5

6

As soon as your code executes this line

return 0;

no other lines of your code will be executed - from a practical point of view, your program will have ended. Move this line down so that it is the last line of code executed by your main() function.

Sign up to request clarification or add additional context in comments.

3 Comments

Not quite. When return 0 executes, the program returns from the main function. There's still a bit more that happens before actual termination.
Haha to be more pedantic, your code may still run after returning from main. Destructors of global objects.
@NeilKirk Rather than edit my answer, I will let your comment do the talking.
2

Your problem is that you are returning from main before doing anything:

int main()
{
    return 0; // HERE!!

    // no code after return gets executed

}

Comments

1

Your return 0; should be at the end of main, not the start

Comments

0

Please re-position the "return 0;" statement

Comments

0

Since main is a function that returns an integer, the execution of the main function is primarily to return some integral value. As soon as the value is returned, the function assumes its job is complete and hence does no longer hold the control of the program.

Your code:

#include<iostream>
using namespace std;
int main()
{
    return 0; // Function thinks its job is done hence it ignores everything after it. 
    ...some other code
}

Actually what you wish to do:

#include<iostream>
using namespace std;
int main()
{
    ... useful code
    return 0;  // Okay. Returning is alright, as the useful job is done.
}

5 Comments

"execution of the main function is primarily to return some integral value" that's funny!
@Neil : I generally use this technique to remember to put return statements at the end of my functions. Funny I know, but I was just trying to help.
Did you know you don't have to put a return statement in main? It's allowed!
Yeah I do know, just a habit of putting it there.
I'm such an idiot :P. Thank you so much! I was never really informed as to what the return 0; did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.