Idiotproof y/n option

Sep 1, 2016 at 7:55pm
What I want is a code segment that

So what have you tried? The snippet you've shown isn't doing any input validation and will continue until you enter 'Y'. And don't forget if choice is a char the end of line character will be left in the input buffer.

Sep 1, 2016 at 8:23pm
Okay, a couple of problems with that code.

First a char can only hold a single char not multiple.

Second in the C++ language using a pair of single quotes denote a single character 'Y', a pair of double quotes denote a string "string", also note that you must use matching pairs, an opening and closing quote of the same type.

Third when trying to combine multiple conditions in a control statement you must use logical operators with complete comparisons on both sides of the logical operator. For example:

while(choice = 'Y' || choice == 'y');

Next in C++ there are two types of strings, C++ string, and C-strings (an array of characters terminated by the end of string character ('/0'). In order to be able to enter multiple characters you would need to use one of these two types of strings and use the proper comparison operations depending on which you choose. And remember a string constant uses two double quotes "YES".

Since this is your first C++ program I suggest you keep things simple. Just remove the outer do/while() statement and add an exit menu item to your menu. Then if the user selects that item either break out of the loop or return to the operating system.



Last edited on Sep 1, 2016 at 8:29pm
Sep 1, 2016 at 9:32pm
Alright. Thank you. Everything else in the code look ok? Anything I can do to make it more "standard" eg. anything I did that isn't recommended, is frowned upon, or what have you?
Sep 1, 2016 at 9:52pm
@gentleguy
This is my first program. I used code from here http://www.cplusplus.com/forum/general/119462/
as a base for my project. It expanded really fast, and I didn't even realize it until i stopped. Do you have any other suggestions for my formatting or anything else?
Sep 1, 2016 at 10:24pm
As the title says, I need help with a idiotproof yes/no option. What I want is a code segment that, when prompted, allows users to input Y, y, Yes, yes. Here is the part I have: ]

1
2
3
cout<<"Do you want to convert another temperature? Y/N";
  cin>>choice;
  }while(choice == 'Y')


If you need more of the code, let me know.
Thanks!
-Sam

The code isn't wrong. I've heavily modified it and one of the things I'm trying to do is add a option to convert more temps.
Sep 1, 2016 at 11:10pm
Clearly, the word "idiot-proof" is not itself idiot-proof.
Sep 2, 2016 at 4:04am
I've been missing a lot, but IDK why swolff's OP is missing or why his account is limited.
Unless he's an obvious pseudo for someone else, I don't see that he has done anything wrong.

That, and the question is a very good one that gets asked frequently.

The answer depends on your UI design -- expectations for the user.
The most correct answer, IMHO, is that your program should ask the question. If the question is incorrectly answered, it should either exit gracefully or back up to a reasonable state.

For example, suppose you have a standard homework scenario where you must maintain a list of students. Each student gets a name (first and last), a school ID, a list of grades, and, IDK, a boolean flag to indicate whether or not he/she is matriculated:

1
2
3
4
5
6
7
8
struct Student
{
  std::string last_name;
  std::string first_name;
  std::string id;
  std::vector <double> grades;
  bool is_matriculated;
};

Now, when you write the code to ask the user to input a student's information, you may wish to ask:

    Is <name> matriculated (Y/N)?  

According to my advice above, if the user does not answer with a clear yes or no, that is an input failure. Rather than simply crash the entire program, continue the student entry and return to the main menu without adding the invalid student.

What this does is allows non-human actors to correctly execute your program without problems.

A good way to implement this is to create a method that inputs a student and a separate method that validates a student. A useful helper may use both to decide whether to add the new student to the list of students or simply complain that the student was invalid.

Hope this helps.
Topic archived. No new replies allowed.