Home > Mobile >  why is my code not accepting letters as user input
why is my code not accepting letters as user input

Time:11-13

my code runs without errors or anything but after I put in a "F" or "P" it skips to "click any button to continue" but if I use any numbers it goes through the code fine.

code

#include #include // needed to use set precision

using namespace std;

void calcCost(double f_benefits, double F, double total_cost, double emp_salary)

{

if (f_benefits == F)

    (total_cost  = emp_salary * 1.5);

else 
    (total_cost  = emp_salary * 1.25);   // calculating operating function

}

int main() {

double num_emp = 0; // employees
double emp_salary = 0; // employees salary
double f_benefits = 0; // are they full time or part time benifits
double total_cost = 0;
int F = 0;
int P = 0;

cout << setw(69) << "Cost of Operation\n\n";


cout << "Please enter the number of employees to process: ";
cin >> num_emp;
cout << endl;


    for (int i = 1; i <= num_emp; i  )  // loop for each employees salary and benifits
    {
        cout << "Please enter the salary for employee " << i << ":";
        cin >> emp_salary;
        

        cout << "Is employee " << i << " receiving(F)ull or (P)artial benefits ? Please enter F or P : "; // Dont forget input validation for this step
        cin >> f_benefits;
        

        
    }
    
    

    



    return 0;

}

CodePudding user response:

Shouldn't f_benefits be a string?

CodePudding user response:

The "cin >> f_benefits" is trying to read a double value. You should read the response into a char or string. Reading into a string expects a newline before returning.

You should also check that you got an "F" or "P" .

  • Related