Thread: Wait for a specific key press (c++)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    24

    Wait for a specific key press (c++)

    Hello, reader. I've been learning c++ like a mother recently and, believe it or not, I've encountered a problem! Or, more of a writers block.

    note:You don't have to read all this crap, my question is at the end of this post.

    I'm making an account management program to better my skill in programming.
    It accepts input from the user for personal information then saves it to a .txt file.

    I'll show you the code so you can get a better idea of what I'm talking about:
    Code:
    /*
    Account Management.
    This is an information database program.
    By Jacob Keller
    March 12, 2008
    */
    
    
    #include <iostream>
    #include <cstring>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        char yes[10];
        char name[50];
        char lastname[50];
        char fullname[100];
        char phonenumber[11];
        char address[100];
        
        ofstream account_a ("accounts.txt", ios::app);
        
        cout<<"To create an account, type 'new'. ";
        if(strcmp(yes,"new")==0)
        {
         cout<<"Enter first name: \n";
         cin.getline(name,50);
         cout<<"Enter last name: \n";
         cin.getline(lastname,50);
         cout<<"Enter home phone number: \n";
         cin.getline(phonenumber,11);
         cout<<"Enter address: \n";
         cin.getline(address,100);
        
        //Group info.
        strcat(fullname,name);
        strcat(fullname," ");
        strcat(fullname,lastname);
        account_a<<"Name: " <<fullname<<". Address: "<<address<<". Phone Number: "<<phonenumber<<".";
        cout<<"New account saved to accounts.txt \n";
        }
    cin.get();
    }
    Right now it's a mess and I haven't got half the features done, but what it's suppose to do is open up, give you the option to "create an account", and then save it.

    But, (because of cin.get(); I believe) it closes once you press enter, not giving the user a chance to create an account. The only way I see of resolving this problem is by hitting a key without hitting the enter key.

    So is there a way to make the program wait for the user to press a specific key instead of using enter? If so, please tell me.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Between here
    Code:
    cout<<"To create an account, type 'new'. ";
    and here
    Code:
    if(strcmp(yes,"new")==0)
    why do you believe the user has a chance to type anything at all?

    Also: if this is supposed to be C++, you should be using std::string which will allow you to do "normal" things, instead of strcmp, strcat, etc.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    I haven't heard of std::string.
    I only know what's in the c++ made easy tutorial (I'm at lesson 17), and they used stuff like strcmp and strcat.

    And to your original question: You can type all you want, its when you press enter that terminates the program.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++ made easy? Hah, good name for a book that should be named "C made easy."
    With std::string, you don't need to use strcmp. Just comparing with == is all you need to do. Easy enough. Now that is made easy. Try it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by RazzTheKid View Post
    I haven't heard of std::string.
    I only know what's in the c++ made easy tutorial (I'm at lesson 17), and they used stuff like strcmp and strcat.

    And to your original question: You can type all you want, its when you press enter that terminates the program.
    You see a cin.getline(yes); between those two statements? Do point it out.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Funny that a book called "C++ Made Easy" would teach you char before std::string...

    If you do want to stick with where your book is going though:

    There's nothing here to take the users input before acting on it,
    Code:
        cout<<"To create an account, type 'new'. ";
        if(strcmp(yes,"new")==0) 
       { 
       ...
       }
    so your program just skips the if statement and goes right to the cin.get(), where it waits for the user to press enter before continuing. Since there's nothing after that (where there should be a return statement, by the way) the program closes.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    Oh, I got it. That was stupid of me..
    Damn I feel stupid!

    Thanks everyone, you all have been very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Press "Enter" key to quit the program
    By Vitamin_C in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2005, 08:25 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Capture Enter key press in EDIT Box
    By richiev in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2005, 12:03 AM
  4. monitor for specific key press
    By Andystudent in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:27 PM
  5. Allegro Specific key input...
    By o0obruceleeo0o in forum Game Programming
    Replies: 6
    Last Post: 07-01-2003, 12:41 PM