0

Please let me know why this is going into an endless loop if you enter a decimal point for your monthly salary, hours worked, or sales. Is there an easy fix?

#include <iostream>
#include <iomanip>
using namespace std;
#include <cstdlib>
#include <string>

bool getEmployeeData (int*, char*, int*, int*, int*, int);
float calculateGrossPay (char, int, int, int);
void displayEmployeeInfo(int, char, int);
bool tryAgain(char*);
bool isValidStatus(char*);

int main()
{
        int ID = 0;
        int Salary = 0.00;
        int hourwrk = 0.00;
        int sales = 0.00;
        int grossPay = 0.00;
        char pyrllSt = ' ';
        float totalPayroll = 0.0;
        bool result = true;
        while (result)
        { 
                result = getEmployeeData (&ID, &pyrllSt, &Salary, &hourwrk, &sales, totalPayroll);
                if (result)
                {
                        grossPay = calculateGrossPay(pyrllSt, Salary, hourwrk, sales);
                        totalPayroll += grossPay;
                        displayEmployeeInfo (ID, pyrllSt, grossPay);
                }
                else
                {
                        cout << "Total Payroll for this month: " << endl;
                        cout << totalPayroll << endl;
                }
        }
        return (1);
}

bool getEmployeeData (int *ID, char *pyrllSt, int *Salary, int *hourwrk, int *sales, int totalPayroll)
{
        cout << "Enter employee ID: " << endl;
        cin >> *ID;

        if (*ID > 0)
        {
        cout << "Enter payroll status: " << endl;
        cin >> *pyrllSt;
        if (isValidStatus(pyrllSt))
        {
        if (*pyrllSt == 's' || *pyrllSt == 'S')
        {
                cout << "Enter monthly salary" << endl;
                cin >> *Salary;
        }

        if (*pyrllSt == 'h' || *pyrllSt == 'H')
        {
        cout << "Enter number of hours worked this month: " << endl;
                cin >> *hourwrk;
        }

        if (*pyrllSt == 'c' || *pyrllSt == 'C')
        {
        cout << "Enter total sales for this month: " << endl;
                cin >> *sales;
                        }
        }//if isValid Status
                return true;
        }
        else
        {        
                return false;
        }
}

bool isValidStatus (char *pyrllSt)
{
        bool isGood = true;

        while (isGood)
        {

                isGood = (*pyrllSt == 's' || *pyrllSt == 'S' || *pyrllSt =='h' || *pyrllSt == 'H' || *pyrllSt == 'c' || *pyrllSt =='C');

                if (isGood)
                {
                        return true;
                }
                else
                {
                        isGood = tryAgain(pyrllSt);
                } 
        }
        return false;
}

bool tryAgain(char *pyrllSt)
{
        char quit;

        cout << "Type Q or q to quit, any other key to continue: " << endl;
        cin >> quit;

        if (quit == 'q' || quit == 'Q')
        {
                cout << "*** Program Terminated ***" << endl;
                return false;
        }
        else
        {
                cout << "Re-enter: 's' or 'S' for salaried, 'h' or 'H' for hourly, " << endl;
                cout << "'c' or 'C' for commissioned." << endl;
                cin >> *pyrllSt;
                return true;
        }                
}

float calculateGrossPay (char pyrllSt, int Salary, int hourwrk, int sales)
{
        float grossPay;
        if (pyrllSt == 's' || pyrllSt == 'S')
        {
                grossPay = Salary;
        }

        if (pyrllSt == 'h' || pyrllSt == 'H')
        {
        grossPay = hourwrk * 18.75;
        }

        if (pyrllSt == 'c' || pyrllSt == 'C')
        {
        grossPay = 1000 + (sales * 0.06);
        }
        return grossPay;

}

void displayEmployeeInfo (int ID, char pyrllSt, int grossPay)
{
cout << "Employee ID: " << ID << endl;
cout << "Payroll Status: " << pyrllSt << endl;
cout << "Gross Pay: " << setprecision(5) << grossPay << endl;
}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by Narue
0

>Is there an easy fix?
Yes, make all of those variables double. If you declare them as integers then it would be a bad idea to pretend they are floating-point with an input function that takes you very literally. Since the decimal point is not a valid integer, cin is choking on it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.