int main is your programs entry point... it's literally your "main" function. void body is a function itself it's being declared at the top then defined at the bottom. If you look a the bottom...
1 2 3 4
|
void body()
{
//.....
}
|
Everything inside the opening { and closing } bracket will happen when you call the function inside int main().
1 2 3 4 5 6
|
int main()
{
body(); //This will do everything inside the body function.
return0;
}
|
int main is similar but since it's your programs entry point it will be called at the startup of the program and do whatever code you put inside it.
for(int x=0;x<3;x++)
This is a for loop and all it does is repeat the code within it's open and close brackets {...} while x is smaller then 3. more or less...
1 2 3 4 5
|
int x = 0;
while(x < 3) //if x is smaller then 3
{ //open bracket
x++; // add 1 to x each pass thru this code.
}//close brasket
|
If you are unfamiliar with while loops you should really go thru a few basic c++ tutorials because loops are rather important.
The reason the top and bottom of the ship draw like they should is because you call the body() function between the top and bottom "cout" calls.
Every function with a type like "int main" must return a value of the type it was created with, so "int main" must return a int which is why you can return 0 and avoid any compiler errors or complaints.
void functions like "void body()" have a void return type which means they return nothing at all.
Edit: I just noticed he didn't put the open and close brackets on his for loop... c++ allows this for the next line down. It's actually like this..
1 2 3 4 5 6 7 8 9
|
void body()
{
string a="+------+";
string b="| |";
for(int x=0; x<3; x++)
cout<<a<<endl<<b<<endl<<b<<endl; //only this one line is repeated
cout<<a<<endl;
}
|
look closely and it's just printing a b b 3 times then at the bottom a again.
You could combine what he has into one function really easy. instead of the body() function you could do something like..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void drawSpaceship()
{
string a="+------+";
string b="| |";
cout<<" /\\ "<<endl;
cout<<" / \\ "<<endl;
cout<<" / \\ "<<endl;
for(int x=0; x<3; x++)
cout<<a<<endl<<b<<endl<<b<<endl;
cout<<a<<endl;
cout<<" / \\ "<<endl;
cout<<" / \\ "<<endl;
cout<<" / \\"<<endl;
}
|
If you were to call this drawSpaceship function inside your main it would do the same thing.
1 2 3 4 5
|
int main()
{
drawSpaceship();
return 0;
}
|