C++ noob question. lol  | | |
October 26th, 2004, 11:40 PM
|
#1 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
hi. im a noob to programming  lol. i'm learning c++ and i had a quick question (or 2) at the end of a program, what does the return 0; command do? also, i double click the icon for the program, and it does the stuff, then on the last part where it's supposed to display the final outcome of the stuff they put in, it just closes, instead of staying open. What line do i need to end to stop it from closing right away. either staying in the command prompt or even just add a 10 second display. i tried changing the return command to return 10 to see if that helped but it didn't. lol. thanks guys  . later. |
| |
October 27th, 2004, 12:02 AM
|
#2 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
a)you don't really need to worry about it. Generally the return statement at the end of a program is used to return error codes and things like that to the operating system. They really aren't used anymore in windows.
b) "getch();" will cause it to pause until a key is pressed. Put this at the end of your program to stop at the end. you can also use this anywhere else to pause the program, and also get a single keypress from the keyboard. make sure to include "conio.h" at the top to make this work, |
| |
October 28th, 2004, 04:50 PM
|
#3 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
where do you include conio.h?? i told u...im really a noob with C++ maybe 6 days teaching myself from an online tutorial. lol. is that your preprocessor...and if it is, can you have 2 preprocessors? lol...im such a goof..getting way over my head. lmao. thanks originel  |
| |
October 28th, 2004, 05:01 PM
|
#4 (permalink)
| | Senior Member
Join Date: Jul 2003 Location: NY
Posts: 771
| Quote: |
Originally Posted by originel a)you don't really need to worry about it. Generally the return statement at the end of a program is used to return error codes and things like that to the operating system. They really aren't used anymore in windows. | incorrect, a return statement "returns" information..
example Code:
print(someMethod());
int someMethod()
{
return 56;
} that will output "56" in the console.
__________________ RackByte.com - Web Hosting / Reseller Hosting / VPS / Dedicated Servers / Domain Registration
|
| |
October 28th, 2004, 11:55 PM
|
#5 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
| Quote: |
that will output "56" in the console.
| like i said...it's a DOS convention that isn't really used anymore. We're talking about the return in int main, not in a different function like you have.
yes the include is a pre-processor directive, and you can have as many as you want.
Basically it would look something like this: Code: #include<conio.h>
#include<iostream.h> // or whatever you output with
int main()
{
// Do Stuff
getch();
return 0;
}
Last edited by originel : October 28th, 2004 at 11:57 PM.
|
| |
October 29th, 2004, 12:03 AM
|
#6 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
i tried getch() but it isn't working. it says implicit declaration of function 'int getchar(...) on the bottom after i try to compile it. At the top I have #include <iostream.h> then do i add #include <conio.h> underneath that....i'm confused. i tried getting rid of iostream.h and everything just went crazy. if you want a copy of my program i'll post it up...though it's kinda stupid....just practice....lol |
| |
October 29th, 2004, 12:08 AM
|
#7 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
yeah definitely post your code. hopefully i can see what's wrong with it.
iostream is the library that allows output to the console. |
| |
October 29th, 2004, 12:36 AM
|
#8 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
// my first program in C++
#include <iostream.h>
int main ()
{
//variables
int a,b,c,d,e,f,age;
//instruction to user
cout << "How old will you be on your next birthday. \n";
//declare var.
cin >> age;
//declare var
a=age;
b=a * 7;
c=a*365;
d=c*24;
e=d-60;
f=e*60;
//print results
cout << "You will be " << c << " days old, " << d << " hours old, " << e << " minutes old, " << f << " seconds old and lastly, "<< b << " years old in dog years! \n";
//hit any key to end
cout << "Hit any key to terminate this program";
return 0;
}
so that's the fully working version in C++ now where do i add the stuff you told me to. I just want to make it so that the user has to hit any key to close the program. Like you recommended the getch() thing. sounded like just what i wanted. lol. thanks. whats wierd though...is with include<conio.h> it works fine. I get the error implicit declaration of function 'int getchar(...)' as soon as i add that line. Is it reading getch as a variable?? if not why is int infront of it in the error message. |
| |
October 29th, 2004, 12:38 AM
|
#9 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
hold crap...i think i got it... i needed to define getch as long or short. grrrrrr...if this works now i'm going to spaz. lol. hey...i'll post back to let you guys know how it worked
edit/ well...i have the code in there. and the program works fine. but dos still closes right away. can't get the command prompt to stay open...  oh well...i'm off to bed..i'll check back tomorrow if u guys think of anything. thanks. by the way....my new source code is
// my first program in C++
#include <iostream.h>
#include <conio.h>
int main ()
{
//variables
int a,b,c,d,e,f,age;
//instruction to user
cout << "How old will you be on your next birthday. \n";
//declare var.
cin >> age;
//declare var
a=age;
b=a * 7;
c=a*365;
d=c*24;
e=d-60;
f=e*60;
//print results
cout << "You will be " << c << " days old, " << d << " hours old, " << e << " minutes old, " << f << " seconds old and lastly, "<< b << " years old in dog years! \n";
//hit any key to end
cout << "Hit any key to terminate this program";
long getch();
return 0;
}
thanks again (they need the drinking smiley face for you guys. lol) thanks again.
Last edited by sr71000 : October 29th, 2004 at 12:48 AM.
|
| |
October 29th, 2004, 09:24 AM
|
#10 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
actually adding the "long" in front of getch should not work. it's called overloading the function and gives me a compiler error (because it doesn't have a return). In any case getch() returns a character as it is, not a long. in any case you need to leave the long out of it.
btw what compiler are you using?
Last edited by originel : October 29th, 2004 at 09:28 AM.
|
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |