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;
}
user2603709
  • 33
  • 1
  • 3
  • 3
    Those *declared, then initialized* statements are wrong. It's declared, then assigned to. – chris Aug 27 '13 at 13:05

5 Answers5

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.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
2

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

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

    // no code after return gets executed

}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

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

const_ref
  • 4,016
  • 3
  • 23
  • 38
0

Please re-position the "return 0;" statement

Martin
  • 3,396
  • 5
  • 41
  • 67
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.
}
iluvthee07
  • 491
  • 1
  • 5
  • 16