Thread: Press a key to proceed>(noob question)

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Press a key to proceed>(noob question)

    There's this problem I have when writing small 'dos applications'.
    It may sound dumb, but I want to know the best way to let someone( using your program) enter a key to proceed.

    Here's an example of a simple code I wrote..
    -------------------------------------------
    #include <iostream.h>

    #include <string.h>

    int main()
    {
    char name[50];

    char confirm[1];

    cout<<"Please enter your name: ";

    cin.getline(name, 50, '\n');

    strlwr(name);

    cout<<"Hello "<<name<<" Do you wish to proceed? y/n?";

    cin.getline(confirm, 1, '\n');

    if(!strcmpi("n", confirm))

    {

    cout<<"Press Return to quit";

    return 0;

    }

    else

    {

    cout<<"It works!!";

    return 0;

    }

    }
    -------------------------------------------
    All works fine, untill I have to press enter to confirm my procedure. The dos screen just disappears becouse the code ends immediately. Why does it skip the cout function each time I run a dos application? (Yes I tried using cin.get(); but it doesn't work) Is there any other function but cin.get(); to let the user press a key to proceed? And when someone replies to this thread, is there any other way to write the same code?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try looking in the FAQ.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Reply

    Oh, thank you! I should have seen that myself. Didn't think about looking in the FAQ..

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char confirm[1];
    This should be at least two char (leave room for string terminator):
    char confirm[2];
    .
    .
    .
    .
    >cin.getline(confirm, 1, '\n');
    Then this becomes:
    cin.getline(confirm, 2, '\n');

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Reply

    Now it works all fine, but is there any possability to retain capitol letters/characters?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    strlwr(name);

    Here you convert name to lowercase. If you wish to retain the original, then you need to make a copy.
    Code:
    char name_lwr[50];
    
    strcpy(name_lwr,name);
    strlwr(name_lwr);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Basic question - press any key to continue
    By PedroTuga in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2005, 10:38 PM
  3. key press in c plz help thanks
    By fizz_uk83 in forum C Programming
    Replies: 1
    Last Post: 12-24-2002, 07:40 PM
  4. How to put a press any key function?
    By Manish Shrestha in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2002, 05:26 PM
  5. Replies: 1
    Last Post: 10-07-2001, 12:27 AM