Trying to create an if/else statement for a string

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    String
AI Thread Summary
To create an error message for invalid input in a program, an if/else statement can be structured to check for acceptable characters. The condition should verify that the input is not 'y', 'Y', 'n', or 'N'. Using a character variable instead of a string can simplify the process. An example condition is: if (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N'). Alternatively, converting the input to a single case can streamline the checks. This approach ensures that the program responds correctly to various invalid inputs.
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.

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


i need to now make an if else statement like

if (_________){
cout << "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.
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
2
Views
3K
Replies
2
Views
2K
Replies
7
Views
2K
Replies
6
Views
3K
Replies
12
Views
2K
Replies
2
Views
2K
Back
Top