home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++ noob question. lol

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2068
Discussions: 200,949, Posts: 2,379,422, Members: 246,311
Old October 26th, 2004, 11:40 PM   Digg it!   #1 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
C++ noob question. lol

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.
sr71000 is offline   Reply With Quote
Old October 27th, 2004, 12:02 AM     #2 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to 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.

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,
originel is offline   Reply With Quote
Old October 28th, 2004, 04:50 PM     #3 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
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
sr71000 is offline   Reply With Quote
Old October 28th, 2004, 05:01 PM     #4 (permalink)
Senior Member
 
kantlivelong's Avatar
 
Join Date: Jul 2003
Location: NY
Posts: 771
Send a message via AIM to kantlivelong Send a message via Yahoo to kantlivelong
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
kantlivelong is offline   Reply With Quote
Old October 28th, 2004, 11:55 PM     #5 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
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.
originel is offline   Reply With Quote
Old October 29th, 2004, 12:03 AM     #6 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
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
sr71000 is offline   Reply With Quote
Old October 29th, 2004, 12:08 AM     #7 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
yeah definitely post your code. hopefully i can see what's wrong with it.

iostream is the library that allows output to the console.
originel is offline   Reply With Quote
Old October 29th, 2004, 12:36 AM     #8 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
// 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.
sr71000 is offline   Reply With Quote
Old October 29th, 2004, 12:38 AM     #9 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
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.
sr71000 is offline   Reply With Quote
Old October 29th, 2004, 09:24 AM     #10 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
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.
originel is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
best linux version sr71000 Applications and Operating Systems 18 October 15th, 2004 07:12 PM
starting a web server sr71000 Webmastering and Programming 28 August 12th, 2004 12:45 AM
Getting a photo on the home page Detritus IMO Community 2 June 17th, 2004 05:22 PM
New PC glich sevenal Technical Support 3 February 22nd, 2003 03:35 PM
[UD] Looks like im next for termination phenious Distributed Computing 26 June 25th, 2002 08:39 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Making Health Care Worse (176)
Is It Just Me? (2940)
The disrespect of Obama by Russian .. (27)
Wireless Televisions. (12)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Regular Build (6)
radeon x850xt platinum & shader.. (6)
Is the PSU I received dead? (12)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
windows vista security holes (9)
Dept. of HS: NSA 'Helped' Develop V.. (15)
Recent Discussions
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
Point and Shoot Camera Suggestions. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Laptop with wireless problem. (2)
Internet Lost (1)
Hp Artist Edition + Matching Bag (0)
My monitor won't turn on after instal.. (0)
Asus P4G8X Mobo (6)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
windows 7 internet problem (5)
BSOD On Startup (ntoskrnl.exe) (2)
Print spooler problem (15)
Have you switched yet? (86)
screen resolution vs monitor size (2)
sms storage to PC (0)
Regular Build (6)
Open With ..... Win7 (0)
java code for fibonacci (1)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)


All times are GMT -4. The time now is 11:04 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28