gEEEk Posted December 26, 2008 Share Posted December 26, 2008 Hey guys. I've started learning some C++ and I've got a few questions about a small program I'm writing. It's a Win32 console application. - The first line, "#include <iostream>" why is that important? What is its purpose? And why iostream? - Aswell as the the "using namespace std;" and the "int main()" What is their purpose? And, when I try to run the program, just by simply double clickin it on the desktop, it opens and closes after the user inputs. How can I correct this? I can however run it completly fine if I'm using cmd. #include <iostream> using namespace std; int main() { int UsersAge = 0; cout << "What is your age? "; cin >> UsersAge; if ((UsersAge >=0) && (UsersAge <=4)) { cout << "You are a baby! HAHAH!"; } else if ((UsersAge >=4) && (UsersAge <=10)) { cout << "You are child! HAHAH!"; } else if ((UsersAge >=10) && (UsersAge <=20)) { cout << "You are youngster!; } else if ((UsersAge >=20) && (UsersAge <=60)) { cout << "You are an adult!"; } else if ((UsersAge >=60) && (UsersAge <=140)) { cout << "You're old!"; } else if ((UsersAge >=140)) { cout << "You're lying!"; } cout << " Because you are " << UsersAge << " years old!"; } Quote Link to comment Share on other sites More sharing options...
Luca662 Posted December 26, 2008 Share Posted December 26, 2008 Hey man, I am also learning C++ myself. I'm farther than you so I can help you out =D. the line "#include iostream" allows you to input and output things in the program. It imports the library for you to do so. Try deleting the line and see what errors you get in your compiler. "int main()" is a function. You need "int main()" to begin your program with, but you can divert to other functions in your code too. Don't worry about it for now. Its way ahead of where you are at. "using namespace std" deals with classes, I think. I haven't gotten to that part yet so I can't really help explain what it is. I'm doing classes next chapter =D. If you want to find out more about them heres a link: http://forums.devshed.com/c-programming-42...mean-45679.html The reason your program closes when you double click the desktop is because the program has ended and the command prompt has no reason to stay open anymore. To fix this you simply ask for a variable at the end of your program. "cin" does not see spaces or enter as a character so you must use "cin.get(variable)". I added that to the end of your program as an example. #include <iostream> using namespace std; int main() { int UsersAge = 0; cout << "What is your age? "; cin >> UsersAge; if ((UsersAge >=0) && (UsersAge <=4)) { cout << "You are a baby! HAHAH!"; } else if ((UsersAge >=4) && (UsersAge <=10)) { cout << "You are child! HAHAH!"; } else if ((UsersAge >=10) && (UsersAge <=20)) { cout << "You are youngster!"; //you had a synatax error here } else if ((UsersAge >=20) && (UsersAge <=60)) { cout << "You are an adult!"; } else if ((UsersAge >=60) && (UsersAge <=140)) { cout << "You're old!"; } else if ((UsersAge >=140)) { cout << "You're lying!"; } cout << " Because you are " << UsersAge << " years old!"; char end;// declares the character cin.ignore();//ignores last keyboard input, not needed if nothing was typed. cin.get(end); //asks user for the chacter "End" } Quote Link to comment Share on other sites More sharing options...
gEEEk Posted December 27, 2008 Author Share Posted December 27, 2008 Thanks man. Also, if you don't want your program to end after the user inputs you can add. system("pause") Which gives the classic "Press a key to continue" I think it initializes a pause.exe which is a part of the C++.. or something like that.. Quote Link to comment Share on other sites More sharing options...
H@L0_F00 Posted December 27, 2008 Share Posted December 27, 2008 i use "getchar()" in C, which just pauses everything and waits for the "Enter" key to be pushed, but I'm not sure how it would translate (if at all) in C++ Quote Link to comment Share on other sites More sharing options...
C-S-B Posted December 30, 2008 Share Posted December 30, 2008 Best way to stop the application from just closing would to encompass the entire program in a loop which checks for a certain condition such as the user pressing 'x'. In psuedo: While (user input) != 'x' { your code; } //end while Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.