Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I better check whether two char variables are in some set of values?

Tags:

c++

Recently, our professor has requested that we use two char variables (day) to receive the input from the user.

The code below works fine as a check to ensure that either Mo, Tu, We, Th, Fr, Sa, Su are the only two characters which are entered together as a pair. If anything else is received as input, it'll loop and ask the user for valid input.

The input should be case-insensitive, meaning that, for example, "mO" and "tu" are acceptable. It seems like there is a lot of repetition that is happening. Is there a way to clean this up?

cout << "Please enter the day of the week did you made the long distance call (Mo Tu We Th Fr Sa Su): ";
cin >> dayOne >> dayTwo;

while ((dayOne != 'M' && dayOne != 'm' || dayTwo != 'O' && dayTwo != 'o') &&
       (dayOne != 'T' && dayOne != 't' || dayTwo != 'U' && dayTwo != 'u') &&
       (dayOne != 'W' && dayOne != 'w' || dayTwo != 'e' && dayTwo != 'E') &&
       (dayOne != 'T' && dayOne != 't' || dayOne != 'H' && dayTwo != 'h') &&
       (dayOne != 'F' && dayOne != 'f' || dayTwo != 'R' && dayTwo != 'r') &&
       (dayOne != 'S' && dayOne != 's' || dayTwo != 'A' && dayTwo != 'a') &&
       (dayOne != 'S' && dayOne != 's' || dayTwo != 'U' && dayTwo != 'u'))
{
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << endl << "You have entered an invalid day. Please re-enter a day in the correct format (Mo Tu We Th Fr Sa Su): ";
    cin >> dayOne >> dayTwo;
}
like image 473
mynameisdlo Avatar asked Jun 16 '20 15:06

mynameisdlo


People also ask

How do you compare char variables?

Compare Char in C Using the strcmp() Function in C The strcmp() function is defined in the string header file and used to compare two strings character by character. If both strings' first characters are equal, the next character of the two strings will be compared.

How do you check if two characters are the same?

Check Equal Char Using the == Equal Operator in Java We can use this operator to check two characters are equal or not. In this example, we created three chars and compared them using the == equals operator. This operator returns true if both the chars are equal, false otherwise.


2 Answers

You could write a fold-expression that compares 2 characters to a string:

template<typename ...Days>
bool any_of(char a, char b, Days ...days)
{
    return (... || (a == days[0] && b == days[1]));
}

and then use it like this:

while (! any_of(std::tolower(dayOne), std::tolower(dayTwo), "mo", "tu", "we", "th", "fr", "sa", "su"))
    // keep asking for input

Here's a demo.

This should satisfy the requirement of using 2 char inputs.

like image 73
cigien Avatar answered Oct 18 '22 00:10

cigien


You typically use tolower or toupper to convert your char variable to the correct case first. I like using tolower - it looks marginally better.

dayOne = tolower(dayOne);
dayTwo = tolower(dayTwo);

while (
    (dayOne != 'm' || dayTwo != 'o') &&
    (dayOne != 't' || dayTwo != 'u') &&
    (dayOne != 'w' || dayTwo != 'e') &&
    (dayOne != 't' || dayTwo != 'h') &&
    (dayOne != 'f' || dayTwo != 'r') &&
    (dayOne != 's' || dayTwo != 'a') &&
    (dayOne != 's' || dayTwo != 'u'))
{
    ...
}

You can further change it by using memcmp to compare both characters at once, but I am not sure it would simplify the code.

like image 21
anatolyg Avatar answered Oct 18 '22 02:10

anatolyg