Detecting return key

This is a discussion on Detecting return key within the C++ Programming forums, part of the General Programming Boards category; I want to simplify user input of parameters to a programme by offering default values - ie if the user ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    10

    Detecting return key

    I want to simplify user input of parameters to a programme by offering default values - ie if the user just presses Return the default value is assigned. The input section should be something like this:

    Code:
    float sym_size;
    
    cout << "Input symbol size in cm (default = 0.1cm): ";
    cin >> sym_size;
    
    if (xxxxxxxxxxxxxxxx)
    {
      sym_size = 0.1;
    }
    The problems are

    1. the programme seems to hang if you just press return

    2. I don't know what to put in for the if condition to test for pressing return with no other input

    Is there a smart way of doing this?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    You use cin.getline() to read the input into a string.

    If that line begins with a newline, then assign the default value, otherwise convert the value in the string to a float.

    string streams are a good idea here
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    I don't think you can do that with cin. I tried it and when you just hit return it still waits for user input. Now if there is a way to make cin accept just hte return key the string it returns should be either "\n\0" or "\0". So your if statement would be one of these:

    Code:
    if (sym_size[0] == '\0')
    {//blank input
    Code:
    if ((sym_size[0] == '\n')&&(sym_size[1] == '\0')
    {//blank input
    Alternatively you can use the getch() function which returns after a single keypress to get the input. You'll need to place it in a loop to actually get the input:

    Code:
    char input[512];
    int ctr;
    
    for (ctr = 0; ctr < 512; ctr+)
    {
        input[ctr] = getch();
        //getch() won't echo the character to screen
        printf("%c", input[ctr]);
    
        if (input[ctr] == '\n')
        {//the user hit return, stop getting characters
            break;
        }
    
        if (input[ctr ] == '\b')
        {//user hit backspace, remove a character
            ctr -= 2;
        }
    }
    
    if (input[0] == '\0')
    {
        //user didn't give a value insert the default
    }
    You may need to clean that up a bit, but that should give you an idea how it would look.

    FYI: getch() only exists on windows (include conio.h) there is a good tutorial on the board somewhere (I seem to have lost the link) to get it to work on Linux if need be.
    Last edited by Exile; 01-19-2005 at 04:03 AM.

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Use cin.peek() in a while loop. Here's an example..
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int userInput = 0;
    	
    	cout << "Input data: ";
    
    	while (cin.peek() != '\n')
    		cin >> userInput;
    	
    	if ( userInput )
    	{
    		cout << userInput;
    		while (cin.get() != '\n'); // flush stream after input //
    	}
    	else
    	{
    		cout << "Enter pressed.";
    		cin.ignore();  // flush the \n that was just pressed //
    	}
    	
    	cin.get(); // keep console window open until enter pressed //
    	return 0;
    }
    cin.peek() returns the next character from input without extracting it from the input stream.
    Last edited by Scribbler; 01-19-2005 at 09:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21