Search code examples
c++stringgetline

C++ Converting Different Types to String Temporarily


I apologise if this question is a bit long winded. I will try to keep it short but concise.

I am building a major program for an entry level C++ course, where I am creating a mock version of employee management for a warehouse. The general form is that the user is asked for input in order to select the type of employee, and then, based on the input, is then asked to input the employee details relevant to that employee type.

Due to this, there are functions that accept input as int, float, string and char. I have a separate smaller function that checks for 'exit' as input, and exits the program if this is true. I currently have this designed so that all inputs are classed as strings, but this obviously causes problems later when I try to equate values numerically.

Here is my current version of the check function and one of the user input functions:

//Current add_employee function
void add_employee()
{
        string emp_type = "";
        bool exit_check;
        cout << "Please select employee type. Enter 1 for Manager or 2 for Dockhand." << endl;
        getline(cin, emp_type);
        if(name.empty())
        {
                cout << "Cannot accept empty field as input. Please answer with 1 for Manager, or 2 for Dockhand." << endl;
                add_employee();
        }
        exit_check = check_input(emp_type);
        if(exit_check == true)
        {
                return;
        }
        if(emp_type == "1")
        {
                add_manager;
        }
        else if(emp_type == "2")
        {
                add_dockhand;
        }
        else
        {
                cout << "Sorry. Your input is invalid. Please answer with 1 for Manager, or 2 for Dockhand." << endl;
                add_employee();
        }
}

//Current check function
bool check_input(string input_var)
{
        if(input_var == "exit")
        {
                return true;
        }
        else
        {
                return false;
        }
}

As for my question. Is there a relatively simple way to keep the structure of my code, taking the input as an int, then converting it to a string to check for 'exit', but also having the input still being considered an int if the exit_check is false? I was thinking about having the input assigned to two variables, but that felt like a waste of code.

Please let me know if something does make sense and I will edit it into the OP.


Solution

  • getline(cin, emp_type); is going to grab a string from the user. You're not getting an int as an input anywhere in that code.

    You could simplify this by changing:

     exit_check = check_input(emp_type);
            if(exit_check == true)
            {
                    return;
            }
    

    to

    if(emp_type.compare("exit") == 0)
                {
                        return;
                }
    

    which checks if the strings are equal. (See compare() for the details on that operator.) and removing the check_input function.

    If you need an int from the input then zenith pretty much covered how to do that.