Explore BrainMass

Explore BrainMass

    c++ cin.ignore function

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    I wrote the following c++ code to a source file

    /this program will calculate federal and fica taxes withheld and calculate net pay
    //for an employee

    #include <iostream>
    #include <string>
    #include <stdlib.h>
    using namespace std;

    //function prototypes

    void empInput(string&, float&, float&, float&);
    float calcFwt(float, float);
    float calcFica (float, float);
    float calcNetPay (float, float, float);
    void dispEPay (string, float, float, float);

    int main ()
    {
    string name = "";
    float wkSal = 0.0;
    float fedWld = 0.0;
    float fedWldRate = 0.0;
    float ficaWld = 0.0;
    float ficaWldRate = 0.0;
    float netPay = 0.0;
    int howMany = 0;
    int count;

    cout<<"Please enter the number of employees you want to process: ";
    cin>> howMany;
    cin.ignore(1);

    //begin loop

    for (count = 1; count <= howMany; count++)
    {
    //enter input items

    empInput(name, wkSal, fedWldRate, ficaWldRate);

    //calculate fed tax

    fedWld = calcFwt(wkSal, fedWldRate);

    //calculate fica tax

    ficaWld = calcFica(wkSal, ficaWldRate);

    //calculate net pay

    netPay = calcNetPay(wkSal, fedWld, ficaWld);

    //display employees name pay and taxes withheld

    dispEPay(name, fedWld, ficaWld, netPay);
    }

    return 0;
    }//end of main function

    //*********program defined function**********

    void empInput(string& emp, float& salary1, float& tax1, float& tax2)

    {
    cout<<"Please enter employee's name: ";
    getline(cin, emp);
    cin.ignore(1);
    cout<<"Please enter employee's salay: ";
    cin>> salary1;
    //validate
    while(salary1 < 0)
    {
    cout<<"Salary should be a positive number: ";
    cout<<"Please enter employee's salary: ";
    cin>> salary1;
    }
    cout<<"Please enter federal income tax rate: ";
    cin>> tax1;
    //validate
    while(tax1 < 0)
    {
    cout<<"The tax rate should be a positive number: ";
    cout<<"Please enter federal income tax rate: ";
    cin>> tax1;
    }
    cout<<"Please enter the fica tax rate: ";
    cin>> tax2;
    //validate
    while(tax2 < 0)
    {
    cout<<"The tax rate should be a positive number: ";
    cout<<"Please enter the fica tax rate: ";
    cin>> tax2;
    }
    cin.ignore (1);
    }//end of getinput function

    float calcFwt(float empsal, float fedrate)
    {
    return empsal * fedrate;

    }//end of calcfwt function

    float calcFica(float salary, float ficarate)
    {

    return salary * ficarate;

    }//end of calcFica

    float calcNetPay(float esalary, float fed, float fica)
    {

    return esalary - fed - fica;

    }//end of netpay function

    void dispEPay(string employee, float fed1, float fica1, float pay)
    {
    cout<<"Employee's name: "<< employee << endl;
    cout<<"Federal tax withheld: "<< fed1 << endl;
    cout<<"FICA tax withheld: "<< fica1 << endl;
    cout<<"Employee's net pay is $: "<< pay << endl;

    system ("pause");
    system ("cls");
    }//end of dispepay function

    My problem is when I run it, after I enter the employee's name, I have to push enter twice before it will say enter salary. I think I am doing something wrong w/cin.ignore

    (My program really is not out of line like it shows here it just did that when i cut and pasted it)
    Thanks

    © BrainMass Inc. brainmass.com February 24, 2021, 2:14 pm ad1c9bdddf
    https://brainmass.com/computer-science/cpp/cin-ignore-function-8874

    Solution Summary

    This job features c++ cin.ignore function. The federal and fica taxes withheld are calculated.

    $2.19

    ADVERTISEMENT