Sign in to follow this  

Question

This topic is 4165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a question about my code...
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name;
    
    cout << "Enter your name: ";
    cin >> name;
    if(name == "Cameron")
    {
            cout << "My name is Cameron also\n";
            cin.get();
    }
    else
    {
        cout << "My name is Cameron, nice to meet you " << name << "\n";
        cin.get();
    }
    system("Pause");
    return 0;
}

Whenever I use the system("Pause") command it displays Press Enter to Continue... I want it to say Press Enter to Exit... How might I do that?

Share this post


Link to post
Share on other sites
PLEASE don't artificially stall your programs at the end. If you're writing console apps, run them like they were ment to be run (from the console). Or, set a breakpoint at your return, it will step into the debugger and you can verify your output. Or, if you don't like either of those options, and are working in visual studio, you can use ctrl+f5 (or "run without debbuging") to run your program and it will not close at the end.

Share this post


Link to post
Share on other sites
0) Don't pause your programs at the end, as indicated.
1) 'system("pause")' is not a command. system() is a function. "pause" is an argument (parameter) for the call to that function.
2) You don't have control over what 'system("pause")' outputs, because what it does is run another program. That program's sole purpose in life it to write that text, wait for input and bail out.
3) You can instead emulate the behaviour of that program in your own code, but please see (0).
4) "Press Enter to Exit" doesn't make any sense anyway, if you look at it :s

Share this post


Link to post
Share on other sites
Quote:
Original post by 14 year old
What everyone sais is true, but if you are really keen on having press return to exit just add this at the end of your code:

*** Source Snippet Removed ***


Actually, that code more than likly won't work as intended. I was experimenting with that exact bit of code last night, and cin apparently wanted to ignore me when I just pressed enter. No idea why. It just kept adding newlines until I typed something and then pressed enter.

EDIT: That may not always be the case, but some strange voodoo has been plagueing me latly. That I have heard several others say that using cin in that fasion doesn't always work as intended.

Share this post


Link to post
Share on other sites
Quote:
Original post by -JetSirus-
Quote:
Original post by 14 year old
What everyone sais is true, but if you are really keen on having press return to exit just add this at the end of your code:

*** Source Snippet Removed ***


Actually, that code more than likly won't work as intended. I was experimenting with that exact bit of code last night, and cin apparently wanted to ignore me when I just pressed enter. No idea why.


Because you're cin-ing an int. It doesn't matter how many times you hit enter, cin will go on ignoring whitespace until you enter a value.

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Quote:
Original post by -JetSirus-
Quote:
Original post by 14 year old
What everyone sais is true, but if you are really keen on having press return to exit just add this at the end of your code:

*** Source Snippet Removed ***


Actually, that code more than likly won't work as intended. I was experimenting with that exact bit of code last night, and cin apparently wanted to ignore me when I just pressed enter. No idea why.


Because you're cin-ing an int. It doesn't matter how many times you hit enter, cin will go on ignoring whitespace until you enter a value.

So, the following will work:
cout << "Press any key to exit...";
char anyKey;
cin >> anyKey;

Correct? I was thinking you might use the conio lib and then have all input other than the enter key be ignored. That would be more to the point and elegent wouldn't it?

Share this post


Link to post
Share on other sites
Quote:
Original post by -JetSirus-
Quote:
Original post by Driv3MeFar
Quote:
Original post by -JetSirus-
Quote:
Original post by 14 year old
What everyone sais is true, but if you are really keen on having press return to exit just add this at the end of your code:

*** Source Snippet Removed ***


Actually, that code more than likly won't work as intended. I was experimenting with that exact bit of code last night, and cin apparently wanted to ignore me when I just pressed enter. No idea why.


Because you're cin-ing an int. It doesn't matter how many times you hit enter, cin will go on ignoring whitespace until you enter a value.

So, the following will work:
cout << "Press any key to exit...";
char anyKey;
cin >> anyKey;

Correct? I was thinking you might use the conio lib and then have all input other than the enter key be ignored. That would be more to the point and elegent wouldn't it?


That will work some of the time. If you dont flush the input buffer before getting input from cin then anything left on the buffer will cause it to go right past the cin successfully (while not waiting for your input since it already got it).

Share this post


Link to post
Share on other sites
Firstly, I agree that you shouldn't be artificially pausing a console program at the end. Run it from an existing console window (Start->Run, cmd, [enter], then "cd [your_program_path]" then "nameofmyprogram.exe".

However, if you really insist on pausing at the end and are using pretty much any Windows or DOS compatible compiler, you will have an include file available called <conio.h> with a function called getch(). This will physically wait for a keypress before returning its ASCII code.

I really don't see the point in trying to be standards-compliant when you are doing something that you wouldn't ever do in a professional project anyway. Realistically, if you are writing a console app that pauses at the end before closing, the odds are pretty slim it is code that will ever have to be ported or be worked on by a team anyway.

Having said that, I've yet to see a Windows compiler that didn't support getch(). I'm pretty sure I used to have it under linux g++ as well.

Share this post


Link to post
Share on other sites

This topic is 4165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this