Reputation: 33
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;
}
Upvotes: 0
Views: 233
Reputation: 501
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.
}
Upvotes: 0
Reputation: 21351
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.
Upvotes: 6
Reputation: 227370
Your problem is that you are returning from main
before doing anything:
int main()
{
return 0; // HERE!!
// no code after return gets executed
}
Upvotes: 2