Trying to create an if/else statement for a string

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    String
Click For Summary

Discussion Overview

The discussion revolves around creating an if/else statement in a programming context to validate user input for a string. Participants are focused on ensuring that the input is either 'y' or 'n' (in any case) and generating an appropriate error message if the input is invalid. The scope includes programming logic and error handling.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using an if statement that checks if the input is not equal to 'y', 'Y', 'n', or 'N' to trigger the error message.
  • Another participant proposes using a character variable instead of a string for clarity and suggests using getchar() to read the input.
  • There is a recommendation to allow both uppercase and lowercase letters for valid input, emphasizing that the user should not be restricted to lowercase only.
  • A later reply agrees with the suggestion of checking for four conditions or converting the input to a single case to simplify the logic.

Areas of Agreement / Disagreement

Participants generally agree on the need for an if statement to validate the input, but there are differing opinions on the best approach to implement this validation, including the use of character variables versus strings and whether to convert case.

Contextual Notes

Some assumptions about user input handling and the implications of using different data types (string vs. char) are present but not fully explored. The discussion does not resolve the best method for input validation.

Who May Find This Useful

Readers interested in programming, particularly in input validation and error handling in user interfaces, may find this discussion relevant.

magnifik
Messages
350
Reaction score
0

Homework Statement


I need to make an error message that says "You must enter y or n" for a program. Right now I have this... this is only the part that i need help with, not the whole code.

count << "Student? (y/n): ";
string student;
getline(cin, student);


i need to now make an if else statement like

if (_________){
count << "You must enter y or n" << endl;
return 1;
}

i'm not sure what to put in the blank. i could do student.empty(), but the error message would only show up if a user didn't input anything. i need to put something in the if function that would cause the error message to show if the user didn't put anything, wrote a capital Y or capital N, wrote some other letter, etc.
 
Physics news on Phys.org
hi,
try
if ((not equal to y) or (not equal to x) or (not equal to Y) or (not equal to X))

which will return true if the answer is not any of x, y, X, Y

hope this helps
 
magnifik said:

Homework Statement


I need to make an error message that says "You must enter y or n" for a program. Right now I have this... this is only the part that i need help with, not the whole code.

cout << "Student? (y/n): ";
string student;
getline(cin, student);
I would use a char variable, not named student. A name like student is misleading, since a casual reader of the code would think it represented some attribute of a student, rather than a single character.

I would use getchar() to get the character
magnifik said:
i need to now make an if else statement like

if (_________){
cout << "You must enter y or n" << endl;
return 1;
}
Something like this:
Code:
if (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n')
{
    cout << "You must enter y or n" << endl;
    return 1;
}
magnifik said:
i'm not sure what to put in the blank. i could do student.empty(), but the error message would only show up if a user didn't input anything. i need to put something in the if function that would cause the error message to show if the user didn't put anything, wrote a capital Y or capital N, wrote some other letter, etc.

You shouldn't force the user to enter only lower case letters. Any of 'y', 'n', 'Y', or 'N' should be acceptable.
 
I agree with Mark44,

You can have the 4 conditions in your if statement or you can convert the character the user enters to upper case (or to lower case). But that isn't necessary, it just makes the runtime shorter.
 

Similar threads

  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K