Problem w/ String and Loop issue

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 911 Views - Last Post: 06 May 2012 - 08:37 PM Rate Topic: -----

#1 williamwil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-April 08

Problem w/ String and Loop issue

Posted 05 May 2012 - 05:18 PM

/*
  Name: Program 5; Payroll Version 1
  Author: william 
  Status: Incomplete
 [b]Issues:Only the first line prior to my timecard loop will execute. I can not execute the timecard loop. I am unable to store a first and last name in my struct array. Any assistance you can provide is greatly appreciated. Thank you.[/b]
  Description: This program will process employee master information and  payroll.
*/



#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Define struct for employee data
struct EmployeeMasterInfo
{
       int employeeId;
       string employeeName;
       double payRate;
       int typeOfEmp;
};

int main(int argc, char *argv[])
{
    // variables to be used
    const int NUM_EMPS = 4;
    int index;
    EmployeeMasterInfo empRecord[NUM_EMPS+1];
    double grossPay;
    double netPay;
    int hoursWorked;
    double overTimePay;
    int overTimeHours;
    double totalGrossPay;
    const double TAXRATE = .15;
    double taxOwed;
    
    
    //Get employee data for master record
    
    cout<<"Enter employee master data for "<< NUM_EMPS<<"employees.";
    for  (index = 0; index < NUM_EMPS; index++)
    {
         cout << "Enter information for emplyee #" << (index + 1)<<":\n";  
         cout << "Employee ID:";
         cin >> empRecord[index].employeeId;                      //get employee id
         if (empRecord[index].employeeId > 0)                     //verify user input
         {
               cout << "Employee Name:";
               cin >> empRecord[index].employeeName;
               cout << "Employee Pay rate:";
               cin >> empRecord[index].payRate;                   //get employee payrate
               if (empRecord[index].payRate > 0)                  //verify user input
               {
                  cout << "Type:";
                  cin >> empRecord[index].typeOfEmp;              //get employee classification type manager =1 or union =0
                      if ((empRecord[index].typeOfEmp = 1) || (empRecord[index].typeOfEmp = 0)) //verify user input
                      {
                         cout << "Employee master record is complete for employee " << (index + 1) <<".\n";        //successful record input message
                      }
                      else
                      {
                          cout << "Improper data entry; type should be 1 or 0./n";    //employee classification type error message
                      }
               }
               else 
               {
                    cout << "All dollar values must be positive integers.\n";        //Error message for negative pay rate entry
               }
         }  
         else
         {
             cout << "The employee ID number should be greater than 0.\n";                   //Error message for negative interger employee id entry
         }  
    }        
   //Timecard Processing
    cout << "\nEnter timecard information for each employee:\n";
    cout << fixed << showpoint << setprecision(2);
    for (index = 0; index < NUM_EMPS; index++)
    {
        //Union timecard processing
        if (empRecord[index].typeOfEmp = 0)
        {
           cout << "Enter hours worked for " << empRecord[index].employeeName << ":";  //get hours worked by each employee
           cin >> hoursWorked;
           if (hoursWorked > 40)                                                           //evaluate if overtime pay needs to be calculated
           {
              overTimeHours = hoursWorked - 40;                                            //calculate overtime hours
              if (overTimeHours > 0)                                                       //Validate overtime requirment for calculation
              {
              //Calculate total gross pay; overtime; taxes owed; net pay
                 grossPay = (hoursWorked - overTimeHours) * empRecord[index].payRate;                 
                 overTimePay = (empRecord[index].payRate + (empRecord[index].payRate / 2)) * overTimeHours;
                 totalGrossPay = grossPay + overTimePay;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" << setw(15) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(15) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay;
              }
              if (overTimeHours < 0)                             // no overtime required calculation for union
              {
                 //Calculate total gross pay; taxes owed; net pay
                 totalGrossPay = hoursWorked * empRecord[index].payRate;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" << setw(15) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(15) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay;
              }
           }
           //Mangement timecard processing
        if (empRecord[index].typeOfEmp = 1)
        {
           cout << "Enter hours worked for " << empRecord[index].employeeName << ":";
           cin >> hoursWorked;
           if (hoursWorked > 40)
           {
           //calculate overtime pay
              overTimeHours = hoursWorked - 40;
              if (overTimeHours > 0)
              {
                 grossPay = (hoursWorked - overTimeHours) * empRecord[index].payRate;
                 overTimePay = empRecord[index].payRate * overTimeHours;
                 totalGrossPay = grossPay + overTimePay;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" << setw(15) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(15) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay;
              }
              //calculation without overtime pay
              if (overTimeHours < 0)
              {
                 
                 totalGrossPay = hoursWorked * empRecord[index].payRate;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" << setw(15) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(15) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay;
              }
           }
        }
    }
    
    
}
    system("PAUSE");
    return EXIT_SUCCESS;
    
}


This post has been edited by no2pencil: 05 May 2012 - 05:23 PM
Reason for edit:: Corrected code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Problem w/ String and Loop issue

#2 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4463
  • View blog
  • Posts: 24,908
  • Joined: 10-May 07

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 05:24 PM

Seeing as how you only provided code, can you please tell us what you are trying to do, & what is wrong with your code?
Was This Post Helpful? 1
  • +
  • -

#3 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 05:28 PM

Line 84. If you're testing for the value to be 0, it should look like this:
if (empRecord[index].typeOfEmp == 0)

Likewise with line 117.
Was This Post Helpful? 1
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 07:07 PM

On line 59 you're assigning 1 to typeOfEmp for all of your employees.

= is assignment.
== is test for equality.
Was This Post Helpful? 1
  • +
  • -

#5 williamwil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-April 08

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 07:20 PM

My apologies, the issues I am having were noted within the comments of my code.

Issues:Only the first line prior to my timecard loop will execute. I can not execute the timecard loop. I am unable to store a first and last name in my struct array. Any assistance you can provide is greatly appreciated. Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 07:29 PM

Yes, answers to that problem have already been posted in Post #3 and Post #4.
Was This Post Helpful? 0
  • +
  • -

#7 williamwil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-April 08

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 07:46 PM

I really appreciate the help. I identified a problem with my if statement at line 88. The remainder of my loop will not execute because of my logic. Any hours less than 40 will cause the program to stop. I have changed the if loop for less than 40 hours to an corresponding else for my if loop at line 88. The changes are documented below.

/*
  Name: Program 5; Payroll Version 1
  Author: william 
  Status: Incomplete
  Issues: Only the first line of my timecard loop will execute. I am unable to store a first and last name in my struct array.
  Description: This program will process employee master information and  payroll.
*/




#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Define struct for employee data
struct EmployeeMasterInfo
{
       int employeeId;
       string employeeName;
       double payRate;
       int typeOfEmp;
};

       

int main(int argc, char *argv[])
{
    // variables to be used
    const int NUM_EMPS = 4;
    int index;
    EmployeeMasterInfo empRecord[NUM_EMPS+1];
    double grossPay;
    double netPay;
    int hoursWorked;
    double overTimePay;
    int overTimeHours;
    double totalGrossPay;
    const double TAXRATE = .15;
    double taxOwed;
    
    
    //Get employee data for master record
    
    cout<<"Enter employee master data for "<< NUM_EMPS<<"employees.";
    for  (index = 0; index < NUM_EMPS; index++)
    {
         cout << "Enter information for emplyee #" << (index + 1)<<":\n";  
         cout << "Employee ID:";
         cin >> empRecord[index].employeeId;                      //get employee id
         if (empRecord[index].employeeId > 0)                     //verify user input
         {
               cout << "Employee Name:";
               cin >> empRecord[index].employeeName;
               cout << "Employee Pay rate:";
               cin >> empRecord[index].payRate;                   //get employee payrate
               if (empRecord[index].payRate > 0)                  //verify user input
               {
                  cout << "Type:";
                  cin >> empRecord[index].typeOfEmp;              //get employee classification type manager =1 or union =0
                      if ((empRecord[index].typeOfEmp == 1) || (empRecord[index].typeOfEmp == 0)) //verify user input
                      {
                         cout << "\nEmployee master record is complete for employee " << (index + 1) <<".\n";        //successful record input message
                      }
                      else
                      {
                          cout << "Improper data entry; type should be 1 or 0./n";    //employee classification type error message
                      }
               }
               else 
               {
                    cout << "All dollar values must be positive integers.\n";        //Error message for negative pay rate entry
               }
         }  
         else
         {
             cout << "The employee ID number should be greater than 0.\n";                   //Error message for negative interger employee id entry
         }  
    }        
    //Timecard Processing
    cout << "\nEnter timecard information for each employee:\n";
    cout << fixed << showpoint << setprecision(2);
    for (index = 0; index < NUM_EMPS; index++)
    {
        //Union timecard processing
        if (empRecord[index].typeOfEmp == 0)
        {
           cout << "Enter hours worked for " << empRecord[index].employeeName << ":";  //get hours worked by each employee
           cin >> hoursWorked;
           if (hoursWorked > 40)                                                           //evaluate if overtime pay needs to be calculated
           {
              overTimeHours = hoursWorked - 40;                                            //calculate overtime hours
              if (overTimeHours > 0)                                                       //Validate overtime requirment for calculation
              {
              //Calculate total gross pay; overtime; taxes owed; net pay
                 grossPay = (hoursWorked - overTimeHours) * empRecord[index].payRate;                 
                 overTimePay = (empRecord[index].payRate + (empRecord[index].payRate / 2)) * overTimeHours;
                 totalGrossPay = grossPay + overTimePay;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" << setw(15) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(15) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay <<"\n";
              }
           }   
           else                             // no overtime required calculation for union
           {
                 //Calculate total gross pay; taxes owed; net pay
                 totalGrossPay = hoursWorked * empRecord[index].payRate;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" << setw(15) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(15) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay <<"\n";
           }
           
           //Mangement timecard processing
        if (empRecord[index].typeOfEmp == 1)
        {
           cout << "Enter hours worked for " << empRecord[index].employeeName << ":";
           cin >> hoursWorked;
           if (hoursWorked > 40)
           {
           //calculate overtime pay
              overTimeHours = hoursWorked - 40;
              if (overTimeHours > 0)
              {
                 grossPay = (hoursWorked - overTimeHours) * empRecord[index].payRate;
                 overTimePay = empRecord[index].payRate * overTimeHours;
                 totalGrossPay = grossPay + overTimePay;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" << setw(15) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(15) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay;
              }
           }   //calculation without overtime pay
           else
           {
                 
                 totalGrossPay = hoursWorked * empRecord[index].payRate;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" << setw(15) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(15) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay;
           }
           
        }
    }
    
    
}
    system("PAUSE");
    return EXIT_SUCCESS;
    
}


This post has been edited by no2pencil: 05 May 2012 - 07:53 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#8 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 07:49 PM

At which line is this? Could you please use the code tags for your code? Thank you.
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4463
  • View blog
  • Posts: 24,908
  • Joined: 10-May 07

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 07:53 PM

Please use code tags when posting...
Was This Post Helpful? 0
  • +
  • -

#10 williamwil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-April 08

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 10:39 PM

I am unable to store a first and last name for my name string. The program will accept the first name but not the last name.

/*
  Name: Program 5; Payroll Version 1
  Author: william 
  Status: Incomplete
  Issues: Only the first line of my timecard loop will execute. I am unable to store a first and last name in my struct array.
  Description: This program will process employee master information and  payroll.
*/




#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Define struct for employee data
struct EmployeeMasterInfo
{
       int employeeId;
       string employeeName;
       double payRate;
       int typeOfEmp;
};

       

int main(int argc, char *argv[])
{
    // variables to be used
    const int NUM_EMPS = 4;
    int index;
    EmployeeMasterInfo empRecord[NUM_EMPS+1];
    double grossPay;
    double netPay;
    int hoursWorked;
    double overTimePay;
    int overTimeHours;
    double totalGrossPay;
    const double TAXRATE = .15;
    double taxOwed;
    
    
    //Get employee data for master record
    
    cout<<"Enter employee master data for "<< NUM_EMPS<<"employees.";
    for  (index = 0; index < NUM_EMPS; index++)
    {
         cout << "Enter information for emplyee #" << (index + 1)<<":\n";  
         cout << "Employee ID:";
         cin >> empRecord[index].employeeId;                      //get employee id
         if (empRecord[index].employeeId > 0)                     //verify user input
         {
               cout << "Employee Name:";
               cin >> empRecord[index].employeeName;
               cout << "Employee Pay rate:";
               cin >> empRecord[index].payRate;                   //get employee payrate
               if (empRecord[index].payRate > 0)                  //verify user input
               {
                  cout << "Type:";
                  cin >> empRecord[index].typeOfEmp;              //get employee classification type manager =1 or union =0
                      if ((empRecord[index].typeOfEmp == 1) || (empRecord[index].typeOfEmp == 0)) //verify user input
                      {
                         cout << "\nEmployee master record is complete for employee " << (index + 1) <<".\n";        //successful record input message
                      }
                      else
                      {
                          cout << "Improper data entry; type should be 1 or 0./n";    //employee classification type error message
                      }
               }
               else 
               {
                    cout << "All dollar values must be positive integers.\n";        //Error message for negative pay rate entry
               }
         }  
         else
         {
             cout << "The employee ID number should be greater than 0.\n";                   //Error message for negative interger employee id entry
         }  
    }        
    //Timecard Processing
    cout << "\nEnter timecard information for each employee:\n";
    cout << fixed << showpoint << setprecision(2);
    for (index = 0; index < NUM_EMPS; index++)
    {   
           cout << "Enter hours worked for " << empRecord[index].employeeName << ":";  //get hours worked by each employee
           cin >> hoursWorked;
        //Union timecard processing
        if (empRecord[index].typeOfEmp == 0)
        {
           if (hoursWorked > 40)                                                           //evaluate if overtime pay needs to be calculated
           {
              overTimeHours = hoursWorked - 40;                                            //calculate overtime hours
              if (overTimeHours > 0)                                                       //Validate overtime requirment for calculation
              {
              //Calculate total gross pay; overtime; taxes owed; net pay
                 grossPay = (hoursWorked - overTimeHours) * empRecord[index].payRate;                 
                 overTimePay = (empRecord[index].payRate + (empRecord[index].payRate / 2)) * overTimeHours;
                 totalGrossPay = grossPay + overTimePay;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
               
              }  
           }
             //Mangement timecard processing
        if (empRecord[index].typeOfEmp == 1)
        {
           if (hoursWorked > 40)
           {
           //calculate overtime pay
              overTimeHours = hoursWorked - 40;
              if (overTimeHours > 0)
              {
                 grossPay = (hoursWorked - overTimeHours) * empRecord[index].payRate;
                 overTimePay = empRecord[index].payRate * overTimeHours;
                 totalGrossPay = grossPay + overTimePay;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                
             }
             
           } 
          
        }      
       
    }
     //Calculate total gross pay; taxes owed; net pay
                 totalGrossPay = hoursWorked * empRecord[index].payRate;
                 taxOwed = totalGrossPay * TAXRATE;
                 netPay = totalGrossPay - taxOwed;
                 cout << " Payroll Report";
                 cout << "\n";
                 cout << "ID" <<setw(10)<< "Name"<< setw(10) << "Gross Pay" << setw(10) << "Tax" << setw(10)<< "Net Pay.\n";
                 cout << empRecord[index].employeeId << setw(10)<< empRecord[index].employeeName << setw(10) << totalGrossPay << setw(10) << taxOwed << setw(10)<< netPay <<"\n";
           
    
}
    system("PAUSE");
    return EXIT_SUCCESS;
    
}


This post has been edited by no2pencil: 05 May 2012 - 10:43 PM
Reason for edit:: Added code tags, AGAIN

Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4463
  • View blog
  • Posts: 24,908
  • Joined: 10-May 07

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 10:41 PM

View Postwilliamwil88, on 06 May 2012 - 01:39 AM, said:

I am unable to store a first and last name for my name string.

You are also unable to follow directions. If you refuse to use code tags, you will eventually stop receiving help.
Was This Post Helpful? 0
  • +
  • -

#12 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 10:43 PM

It doesn't look like its actually asking for a last name.
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem w/ String and Loop issue

Posted 05 May 2012 - 10:55 PM

If the name string that you're entering includes a blank space, you can't use cin >> ... to input that string. The >> operator stops at the first whitespace that it finds.

To extract a string that contains whitespace you'll have to use the getline function:
    getline( cin, name );
getline will extract a full line of input, spaces and all.

But ...
since you will be mixing >> with getline you'll have another problem because your >> calls will be leaving 'newline' characters (left-over from you pressing enter after each input) which getline will take as its input. So, you will have to add this on the line after cin >> empRecord[index].employeeId;
    cin.ignore();


That will get rid of the extraneous newline and then getline should work correctly.

This post has been edited by r.stiltskin: 05 May 2012 - 10:56 PM

Was This Post Helpful? 1
  • +
  • -

#14 williamwil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-April 08

Re: Problem w/ String and Loop issue

Posted 06 May 2012 - 06:30 PM

Thank you for the feedback. I am receiving the following error message after the suggested changes.

52 C:\Dev-Cpp\Projects\main_Project5PayrollVer1-Revision2.cpp 'struct std::istream' has no member named 'ingnore'
Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem w/ String and Loop issue

Posted 06 May 2012 - 06:49 PM

C'mon. You're supposed to actually read the error messages.

52 C:\Dev-Cpp\Projects\main_Project5PayrollVer1-Revision2.cpp 'struct std::istream' has no member named 'ingnore'
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2