Noob C++ Question

Started by
13 comments, last by f&g 12 years, 6 months ago

[quote name='f&g' timestamp='1318454346' post='4871987']
Good evening to all at GameDev im very much a noob to C++ and i am looking for some advice to my issue
below is the source to a small program where i get the user to enter there name and then there name is displayed back to them on the screen all this works 100% fine but the main issue is that the console window closes and opens very fast below is the source code


#include <iostream>
#include <string>

int main()
{
std::string FirstName;

std::cout << "Please enter your first name:";
std::cin >> FirstName;
std::cout << FirstName << "\n";
std::cin.get();

return 0;

}


Thank you very much in advance


add the line system("pause"); before return;

[/quote]

omg, why don't take a hammer and smash the keyboard instead?
Advertisement

omg, why don't take a hammer and smash the keyboard instead?

Please try to use more constructive criticism. Such as: "this is highly inefficient because it spawns at least one new process", "this is non-portable" or "this is a bad idea for reasons already stated in the thread".

[quote name='thedodgeruk' timestamp='1318705638' post='4872933']
add the line system("pause"); before return;



omg, why don't take a hammer and smash the keyboard instead?
[/quote]

Because the first idea would help the OP, while the second would not. wink.gif

A self-acknowledged newbie just learning the very bare-bone basics would do well to just use system("pause"). Sure, it's not the best solution at all, but it's by far the easiest for a newbie to understand, instead of trying to explain something like, "Just open your command prompt, change your directory, and execute your application!" (Open my whatnow, change my whajhamjig, and assassinate my job application? Why do people keep throwing random terms at me!), or giving broad sweeping suggestions that aren't really helpful, ("Listen simply: Uninstall your entire IDE, forget everything you ever knew, install -my- favorite IDE, and start from scratch! You have to do this, or you'll forever be doomed!").

He'll have plenty of time to find out why system("pause") is a bad idea, but first he ought to get past page 2 of his programming book. When someone is at that early a stage of learning, you should give the most straightforward and easiest solution, instead of potentially complicating things for them and having them drop programming because it's "too hard" for them.

Seriously... "std::cout << "Please enter your name:" << std::endl;"? He's on page 2 or 3! Make it as easy as possible, before making it as 'correct' as possible. If you are really afraid of the horrible horrendous dangers of the vile system("pause"), then just tell him to use Sleep(3000) or something equally simple, like get(). But before anyone breaks into, "He should structure his programs properly in a loop!". That's correct... and if we let him actually get to the chapter that teaches him about loops, he'll rapidly learn that knowledge in a week or two. tongue.gif
This is the function I use. I just call this at the end of main before returning:

void WaitExit(){
using namespace std;
cin.clear(); // just to ensure there's no junk in the stream
cout << "\nPress ENTER to close...\n";
cin.ignore(1,0);
}
Thank you for all the replys i do have this issue not all sorted i do know indeed that system("pause") is bad practice as this opens as new process witch i dont like the idea of once again thank you for all your help and advice

Regards

f&g

This topic is closed to new replies.

Advertisement