Alice Community  

Go Back   Alice Community > General Discussion > Questions and Comments

Reply
 
Thread Tools Display Modes
Old
belso
Member
 
Status: Offline
Posts: 42
Join Date: Jan 2010
Default 08-07-2011, 04:37 PM

What does the return 0; do?


/|..__________________/\_
/ `---___________----_____|]-------<BLAM>------>
/_==o;;;;;;;;_______.:/
), ---.(_(__) /
// (..) ), ----"
//___//
//___//
   
Reply With Quote
Old
legolizard
Senior Member
 
legolizard's Avatar
 
Status: Offline
Posts: 242
Join Date: Jan 2011
Location: Aboard the Hyperion escaping the zerg.
Default 08-07-2011, 06:14 PM

You will problably read up on this later in your book, but here is my expanation if what it is.

Since the main() is of integer type meaning that it has the int keyword it must return a value. See there two primary types of functions ,functions that return values and functions that return nothing.Any function with the keyword void at the beggining does not return anything. Every other function must return something. For example:

Code:
int foo()
{
//Some code
}
returns an integer.

while

Code:
string boo()
{
//Some code
}
returns a string.

Now every function that returns something must have a return statment and then a value or variable that you want to return.

So you would get:

Code:
int foo()
{
return 5; 
}
So this means that when foo is finsihed executing it will return the value 5.

Now what this return statment does is basically makes the value of foo eqaul to 5.

So to summarize return 0; basically returns the integer 0 back to the main().

Does that make sense? If it does not I can make a example program for you.


"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."-Albert Einstein
   
Reply With Quote
!?!?!?!?
Old
belso
Member
 
Status: Offline
Posts: 42
Join Date: Jan 2010
Default !?!?!?!? - 08-07-2011, 08:01 PM

Hey i understand that completely but what i dont understand is this code i made adjusted to waht you said from the book. It dosent work at all i cant even run it because it wont compile it for some reason


#include <iostream.h>


int main()
{
cout << "Hello! "
"Congratulations on your first"
"C++ program"





return 0;

}


/|..__________________/\_
/ `---___________----_____|]-------<BLAM>------>
/_==o;;;;;;;;_______.:/
), ---.(_(__) /
// (..) ), ----"
//___//
//___//
   
Reply With Quote
Old
legolizard
Senior Member
 
legolizard's Avatar
 
Status: Offline
Posts: 242
Join Date: Jan 2011
Location: Aboard the Hyperion escaping the zerg.
Default 08-07-2011, 08:28 PM

Now Microsoft and that book are getting on my nerves. First they use void main() and anyone who uses that should be shot. Now they are using a C library and not even using the STANDARD NAMESPACE. They even forgot the@#$&*^!!! semicolon at the end of the cout statement!!!! RAWR!!!!! *thorws something from desk*

Sorry one of the few things that annoys me is when experts have no clue on what they are doing.

Okay well firstly instead of

Code:
#include <iostream.h>
use

Code:
#include <iostream>
iostream.h is for C not C++.

Secondly cout and endl and a few other things are part of the library iostream within the standard namespace. Now there are two ways to address this you can

A)
Code:
#include <iostream>


using namespace std;
int main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

return 0; 

}
or

B)
Code:
#include <iostream>


int main()
{

std::cout << "Hello! "<<std::endl;
std::cout<<"Congrats on making your first C++ program!"<<std::endl;

return 0; 

}
Matter of personal preferance. Also one other thing to note. Both programs above will compile fine but you will notice that your prompt will open and then close immediately. To counter act this you can do a variety of things such as:

Code:
#include <iostream>


using namespace std;
int main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

cin.get();//What I generally use; just press the enter key.

return 0; 

}
or


Code:
#include <iostream>
#include <conio.h>//Technically a C header file but it is not as bad as iostream.h


using namespace std;
int main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

getch();

return 0; 

}
or


Code:
#include <iostream>


using namespace std;
int main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

system("PAUSE");//This method however is very resourse costly do not use for big programs as it is bad programming pracitce.

return 0; 

}
One final thing endl = end line or enter.


"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."-Albert Einstein

Last edited by legolizard; 08-07-2011 at 08:42 PM. Reason: few syntax errors
   
Reply With Quote
Thanks
Old
belso
Member
 
Status: Offline
Posts: 42
Join Date: Jan 2010
Default Thanks - 08-07-2011, 08:50 PM

Hey thanks but i decided to use cin.get(); and i would like to know what that means. Im sorry but i dont like to use something and less im certain on the uses so it can be used in as many ways as i can


/|..__________________/\_
/ `---___________----_____|]-------<BLAM>------>
/_==o;;;;;;;;_______.:/
), ---.(_(__) /
// (..) ), ----"
//___//
//___//
   
Reply With Quote
Old
legolizard
Senior Member
 
legolizard's Avatar
 
Status: Offline
Posts: 242
Join Date: Jan 2011
Location: Aboard the Hyperion escaping the zerg.
Default 08-07-2011, 09:06 PM

Very good state of mind. cin.get(); also comes from the standard namspace within the iostream header file. It is derived from the normal cin>>.

What it does is wait for the user to enter something it then treats it as a char meaining you could type "Blalalalaalalalalala" and then cin.get() would only recieve 'B' where as cin>> whould take the entire string. When you press enter cin.get() takes in whatever you typed in executes and then the main() goes to to the last line return 0; thereby terminating the program. cin.get() can also be used for user input because of this and is not just for pausing. You could use it like this for example:

Code:
#include <iostream>

using namespace std;

int main()
{
    char myChar;
    
    cin.get(myChar);
    
    cout<<myChar<<endl;
    
    system("PAUSE");//I used system("PAUSE"); to make it less confusing.
    
return 0; 

}


"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."-Albert Einstein
   
Reply With Quote
Old
belso
Member
 
Status: Offline
Posts: 42
Join Date: Jan 2010
Default 08-07-2011, 10:20 PM

Ok i sorta get that but i am also confused so you used cin.get() but how did you make it so he had to hit enter?


/|..__________________/\_
/ `---___________----_____|]-------<BLAM>------>
/_==o;;;;;;;;_______.:/
), ---.(_(__) /
// (..) ), ----"
//___//
//___//
   
Reply With Quote
Old
legolizard
Senior Member
 
legolizard's Avatar
 
Status: Offline
Posts: 242
Join Date: Jan 2011
Location: Aboard the Hyperion escaping the zerg.
Default 08-08-2011, 08:55 AM

Okay then lets back track a little. As you know cout tells the computer to output something such as a string or varaiable and we have already seen it used in our "Hello World " program like so:

Code:
#include <iostream>

using namespace std;

int main()
{
     cout<<"Hello World! "<<endl;

cin.get();
return 0;
}
cout stands for console output, but does console input exist? Yes this is called a cin statment and like cout it is contained within the iostream header file. In order to use the cin statment in our program we go like this:

Code:
#include <iostream>

using namespace std;

int main()  
{
      int userAge;//Here we declare a integer variable named userAge.
     cout<<"Hello World!"<<endl;
     cout<<"How old are you?"<<endl;
     cin>>userAge;//Here we use a cin statment for user input. Notice how we use two greter than signs rather than two less than signs. You will learn about this later.

cin.get();
return 0;
}
Now here is the problem with using cin.get() that I forgot to mention. When you press enter for your cin statment you then tell the computer to save whatever you typed into the integer userAge. However when you press "enter" it actually stays in the keyboard buffer rendering cin.get() useless for pausing. So what you do is add another dirivative of cin known as cin.ignore(); which will ignore the last key you typed(enter) thus pausing the program.

In the end we should have this:

Code:
#include <iostream>

using namespace std;

int main()  
{
      int userAge;//Here we declare a integer variable named userAge.
     cout<<"Hello World!"<<endl;
     cout<<"How old are you?"<<endl;
     cin>>userAge;//Here we use a cin statment for user input. Notice how we use two greter than signs rather than two less than signs. You will learn about this later.

cout<<"Wow you are "<< userAge <<" year(s) old."<<endl;

cin.ignore();//Ignores the last key typed being enter.
cin.get();
return 0;
}
Now in order to tell the user to press the enter key again all you have to do is add a cout statment below cin.ignore saying "Press enter to continue."

Now cin is very interesting. If you enter a number it will treat it s one, if you enter a string it will treat it as a string. Yet what if we want only one single character. This is when we use cin.get() such as in the program in my last post. The difference between cin.get() and cin is basically when you enter somthing it will treat what you typed as an appropiate data type while cin.get() will treat everything as a char value and will even cut off what you typed into a single char.

Does that make more sense? Again I can give you an example.

Edit: I just noticed but my posts are really long. Sorry about that.


"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."-Albert Einstein

Last edited by legolizard; 08-08-2011 at 01:08 PM.
   
Reply With Quote
Old
belso
Member
 
Status: Offline
Posts: 42
Join Date: Jan 2010
Default 08-08-2011, 01:45 PM

Yes i think i get it much better now thank you and your post are long but you cant attempt to try and make them short they are full of valuable info that needs to be there thank you


/|..__________________/\_
/ `---___________----_____|]-------<BLAM>------>
/_==o;;;;;;;;_______.:/
), ---.(_(__) /
// (..) ), ----"
//___//
//___//
   
Reply With Quote
?
Old
belso
Member
 
Status: Offline
Posts: 42
Join Date: Jan 2010
Default ? - 08-08-2011, 02:03 PM

Hello i am trying to make a program to explain all the functions of c++ and i have made a starting but something is wrong with it can you help explain what?



#include <iostream>

using namespace std;

int main()
{
int userinp;

cout<< "This program was made by Belso. And the knowledge is from Legolizard."



cout<<"Hello we are about to learn about the functions" end1;
cout<< " of c++ take a seat and enjoy the ride" end1;
cout<< " Next to each word if there is a letter" end1;
cout<< "Then that is the letter you have to hit to learn about it" end1;
cout<< "cout(a)"end1;
cout<< "cin(b)" end1;
cout<< "int(c)" end1;
cout<< "cin.get() (d)" end1;
cin>>userinp;
}
for some reason it wont compile maybe i am missing something thank you for looking at it


/|..__________________/\_
/ `---___________----_____|]-------<BLAM>------>
/_==o;;;;;;;;_______.:/
), ---.(_(__) /
// (..) ), ----"
//___//
//___//

Last edited by belso; 08-08-2011 at 02:24 PM.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.