How to make cin less susceptable to hitting Enter key accidentally

  • Thread starter yungman
  • Start date
In summary, cin, cin.get() are used to input char. It is all good if you don't make mistake hitting say the Enter key of Tap key etc.
  • #1
5,706
239
For the lack of better description. cin, cin.get() are used to input char. It is all good if you don't make mistake hitting say the Enter key of Tap key etc.
C++:
//switch-case
#include <iostream>
using namespace std;
int main()
{
    char choice, more;
    do
    {   cout << " Enter either a, r, s, d or q: ";
        cin.get(choice);
        cin.ignore();
        switch (choice)
        { case 'a': cout << " you chose a."; break;
          case 'r': cout << " you chose r."; break;
          case 's': cout << " you chose s."; break;
          case 'd': cout << " you chose d."; break;
          case 'q': cout << " you chose q."; break;
          default: cout << " Not a choice.";
        }
        cout << "\n\n Want to do it again? ";
        cin.get(more);
        cout << endl;
    } while (more == tolower('y'));
}

For example in this program, if I hit Enter instead of an alphabet for choice, it will mess up. more importantly at the end of the while loop, if I accidentally hit the Enter key for more in line 20, it will exit the program instead of cycling back to the start of the program. I lost all the data I keyed in. Using cin >> choice made it much better, BUT then the stream operator create another set of problem for the next cin.getline(). It will not work for my program.

Yes, I know, type more carefully! But $hit do happens. I just want to know whether there is an easy to fix this.

Thanks
 
Technology news on Phys.org
  • #2
Its a common pattern for programs that read from manual input from the terminal to only exit on a specific exit command or condition (or end-of-file when reading from a pipe). Typical its called "exit", "quit" or similar. If you use menu-selection input ("Choose a for this, b for that") you can for instance use "Choose x to exit".

Alternatively, you can ask for confirmation on exit, or in fact on any "dangerous" operation.

For inspiration on an almost legendary program that people even today swear to due to its concise key commands, take a search for "vi cheat sheet".
 
  • Like
Likes yungman
  • #3
I improved a lot after I got rid of all the cin.get() and cin.getline(). All my questions that require cin are one character or one word like last name and first name that doesn't have space in between. It works just as good using streaming operator cin >> name; I don't have to worry about putting cin.ignore() after the cin.get() or cin.getline().
 

1. How can I prevent accidentally hitting the Enter key when using cin?

The best way to prevent accidentally hitting the Enter key when using cin is to use the cin.get() function instead. This function will only read a single character from the input, so there is no risk of accidentally pressing Enter.

2. Can I change the behavior of the Enter key when using cin?

Yes, you can change the behavior of the Enter key by using the cin.ignore() function. This function will ignore the next character in the input, which in this case would be the Enter key.

3. Why is accidentally hitting the Enter key a problem when using cin?

Accidentally hitting the Enter key can be a problem because it will cause the program to read an empty line, which can lead to unexpected behavior or errors. It can also cause the program to move on to the next input without allowing the user to enter the desired input.

4. Is there a way to only allow a certain number of characters when using cin?

Yes, you can use the cin.getline() function to specify the maximum number of characters to read from the input. This can help prevent accidentally hitting the Enter key and also ensure that the input does not exceed a certain length.

5. Can I disable the Enter key entirely when using cin?

No, it is not possible to disable the Enter key entirely when using cin. However, you can use the methods mentioned above to prevent it from causing issues in your program. Alternatively, you can use a different input method that does not rely on the Enter key, such as cin.get() or cin.getline().

Suggested for: How to make cin less susceptable to hitting Enter key accidentally

Replies
14
Views
1K
Replies
1
Views
924
Replies
0
Views
494
Replies
8
Views
1K
Replies
5
Views
1K
Replies
1
Views
2K
Replies
19
Views
1K
Back
Top