How to make cin less susceptable to hitting Enter key accidentally

  • Thread starter Thread starter yungman
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on improving the reliability of user input in C++ programs that utilize the cin and cin.get() functions. The primary issue addressed is the accidental pressing of the Enter key, which disrupts the input process and can lead to data loss. The suggested solution is to replace cin.get() with cin >> for single-character inputs, as it simplifies handling user input without requiring additional cin.ignore() calls. Additionally, implementing a confirmation prompt before exiting the program can enhance user experience and prevent unintended exits.

PREREQUISITES
  • Understanding of C++ input/output streams
  • Familiarity with the cin and cin.get() functions
  • Basic knowledge of control flow structures like switch-case
  • Experience with character data types in C++
NEXT STEPS
  • Research best practices for handling user input in C++
  • Learn about the differences between cin.get() and cin >> in C++
  • Explore techniques for implementing confirmation prompts in C++ applications
  • Investigate common patterns for user input validation in console applications
USEFUL FOR

C++ developers, software engineers working on console applications, and anyone looking to enhance user input handling in their programs.

yungman
Messages
5,741
Reaction score
291
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
    {   count << " Enter either a, r, s, d or q: ";
        cin.get(choice);
        cin.ignore();
        switch (choice)
        { case 'a': count << " you chose a."; break;
          case 'r': count << " you chose r."; break;
          case 's': count << " you chose s."; break;
          case 'd': count << " you chose d."; break;
          case 'q': count << " you chose q."; break;
          default: count << " Not a choice.";
        }
        count << "\n\n Want to do it again? ";
        cin.get(more);
        count << 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
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   Reactions: yungman
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().
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
10
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K