Dustin Trombly
Dustin Trombly

Reputation: 59

A few coding convention/standard practice questions

I have this function that takes a string from main. The string contains all of the valid characters that a user can input from some menu options. Function will put character input into a variable and be compared to each character of the string. Compare input variable to the string characters until valid input is entered.

My question is, what is the best way to implement this loop? I don't like using while (true) with a return in the middle because it looks like an infinite loop with an exception in the middle, which makes it slightly harder to read, but I'm not sure how else I can do what I want it to do. What's the best practice for achieving my goal? Thanks.

char getValidKey(string validKeys)
{
    char letter;

    while (true) {
        cout << "Operation ? ";
        cin >> letter;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        for (int i = 0; i < validKeys.length(); i++) {

            if (letter == validKeys[i])
                return letter;
        }

        cout << "Error. Invalid input.\n";
    }
}

Also, I have a switch statement with multiple returns. Is it more common/preferred to assign calculations to a variable and have one return at the end or is this way generally okay?

string opStr;

switch (myOperation) {
    case 1:
        opStr = "RIGHT";
        break;
    case 2:
        opStr = "LEFT";
        break;
    case 3:
        opStr = "CENTER_ONLY";
        break;
    case 4:
        opStr = "CENTER_MISSING";
        break;
    default:
        opStr = "Error. Invalid input.";
        break;
}

return opStr;

OR

switch (myOperation) {
    case 1:
        return "RIGHT";
        break;
    case 2:
        return "LEFT";
        break;
    case 3:
        return "CENTER_ONLY";
        break;
    case 4:
        return "CENTER_MISSING";
        break;
    default:
        return "Error. Invalid input.";
        break;
}

Upvotes: 0

Views: 61

Answers (1)

Vittorio Romeo
Vittorio Romeo

Reputation: 93274

For the first case, refactor your code in smaller self-contained functions, and it becomes clear to understand the logic of getValidKey even from a while(true):

char isKeyValid(char x, const string& validKeys)
{
    return validKeys.find(x) != string::npos;
}

char readCharFromCin()
{
    char letter;

    cout << "Operation ? ";
    cin >> letter;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return letter;
}

char getValidKey(const string& validKeys)
{
    while (true) 
    {
        const char key = readCharFromCin();
        if(isKeyValid(key, validKeys)) return letter;

        cout << "Error. Invalid input.\n";
    }
}

For the second case, avoid break and simply return from your switch. Make the function containing the switch only do one thing.

string switchOperation(int myOperation)
{
    switch (myOperation) 
    {
        case 1:  return "RIGHT";
        case 2:  return "LEFT";
        case 3:  return "CENTER_ONLY";
        case 4:  return "CENTER_MISSING";
    }

    return "Error. Invalid input.";
}

Also, try to maximize usage of const and pass string instances you're only reading by const& to avoid unnecessary copies.

Upvotes: 4

Related Questions