Thread: Im getting errors

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Post the newly revised code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by Elysia View Post
    Post the newly revised code.
    Code:
    #include <iostream>#include <fstream>
    #include <string>
    #include <iomanip>
    #include <cmath>
    
    
    int main()
    {
    	using namespace std;	
    	string emp_name;
    	double hours_worked;
    	int hourly;
    	double total_pay;
    	double regular_pay = 40;
    	double overtime;
    	int id;
    	double regular;
    	
    	
    	cout << "Please enter your first and last name:" << endl;
    	getline(cin, emp_name);
    	
    	cout << "Please enter employee ID number: " << endl;
    	cin >> id;
    	
    	cout << "Please enter your hourly classifcation number 1-5:" << endl; // enter for the switch statement to clairify which one to pick
    	cin >> hourly;	
    	
    	cout << "Please enter the amount of hours:" << endl; // enter for the if else statement to pick
    	cin >> hours_worked;
    	
    	switch (hourly) // At this point the person will choose 1 of the following employee hourly pay wages 
    	{
    		case 1: hourly = 5.50;
    		cout << "Hourly Wage: $" << hourly << endl;
    		break;
    		case 2: hourly = 6.00;
    		cout << "Hourly Wage: $" << hourly << endl;
    		break;
    		case 3: hourly = 7.00;
    		cout << "Hourly Wage: $" << hourly << endl;
    		break;
    		case 4: hourly = 9.00;
    		cout << "Hourly Wage: $" << hourly << endl;
    		break;
    		case 5: hourly = 12.00;
    		cout << "Hourly Wage: $" << hourly << endl;
    		break;
    		default: hourly = 5.50;
    		break;			
    	}
    	if (hours_worked < 40) // if else if statments to decide how much needs to be calculated.
    	{
    		total_pay = hours_worked * hourly;
    		cout << "Name: " << emp_name << endl;
    		cout << "Employee Id: " << id << endl;
    		if (hourly == 1 || hourly == 2 || hourly == 3 || hourly == 4 || hourly == 5)
    		{
    		cout << "Job Classification: " << hourly << endl;
    		}
    		else
    		{		
    		cout << "Job Classification not found please try again" << endl;
    		}
    		cout << "Regular Pay: $" << total_pay << endl;
    		cout << "Hours Worked: " << hours_worked << endl;
    		cout << "You need to work more hours! Your total pay is: $" << total_pay << endl;
    	}
    	else if (hours_worked == 40)
    	{
    		total_pay = regular_pay * hourly;
    		cout << "Name: " << emp_name << endl;
    		cout << "Employee Id: " << id << endl;
    		if (hourly == 1 || hourly == 2 || hourly == 3 || hourly == 4 || hourly == 5)
    		{
    		cout << "Job Classification: " << hourly << endl;
    		}
    		else
    		{
    		cout << "Job Classification not found please try again" << endl;
    		}
    		cout << "Regular Pay:" << total_pay;
    		cout << "Hours Worked: " << hours_worked << endl;
    		cout << "Good job you worked 40 hours! Your pay is: $" << total_pay << endl;
    	}
    	else if (hours_worked > 40)
    	{
    		regular = regular_pay * hourly;
    		overtime = (hours_worked - 40) * 1.5 * hourly;
    		total_pay = overtime + regular_pay * hourly;
    		cout << "Name: " << emp_name << endl;
    		cout << "Employee Id: " << id << endl;
    		if (hourly == 1 || hourly == 2 || hourly == 3 || hourly == 4 || hourly == 5)
    		{
    		cout << "Job Classification: " << hourly << endl;
    		}
    		else
    		{
    		cout << "Job Classification not found please try again" << ".			Default Pay of 5.50 being calculated." << endl;
    		}
    		cout << "Hours Worked: " << hours_worked << endl;
    		cout << "Regular Pay: $" << regular << endl;
    		cout << "Overtime: $" << overtime << endl;
    		cout << "You worked an excessive amount of hours! Your total pay is: $" << total_pay << endl;
    	}
    	
    return 0;
    }

  3. #18
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    You are using the same variable name, hourly, to refer to 2 things - (a) the hourly classification number (lines 26-27) and (b) the hourly pay inside the switch statements (lines 35, 38, etc). Since the latter are not scoped the value of the variable hourly, post switch, is now one of the hourly wages that is != any of the hourly classification numbers and so one of the messages "Job Classification not found ... " is printed later on.

  4. #19
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by sean_cantab View Post
    You are using the same variable name, hourly, to refer to 2 things - (a) the hourly classification number (lines 26-27) and (b) the hourly pay inside the switch statements (lines 35, 38, etc). Since the latter are not scoped the value of the variable hourly, post switch, is now one of the hourly wages that is != any of the hourly classification numbers and so one of the messages "Job Classification not found ... " is printed later on.
    So in order to fix this the code I would need to input would be:
    Code:
        cout << "Please enter your hourly classifcation number 1-5:" << endl; // enter for the switch statement to clairify which one to pick
        cin >> hourly && x;
    Than change:

    Code:
         if(hourly == 1 || hourly == 2 || hourly == 3 || hourly == 4 || hourly == 5)        {
            cout << "Job Classification: " << x << endl;
            }
            else
            {
            cout << "Job Classification not found please try again" << ".           Default Pay of 5.50 being calculated." << endl;
    Because that as well gives me an error or did I mess that up as well?

  5. #20
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Just scope the switch statments:
    Code:
    switch (hourly) // At this point the person will choose 1 of the following employee hourly pay wages     {
            case 1: 
    		{
            	double hourly = 5.50; 
    			cout << "Hourly Wage: $" << hourly << endl;
            }	break;
            case 2: 
    		{	double hourly = 6.00; 
    			cout << "Hourly Wage: $" << hourly << endl;
    		}	break;
            case 3: 
    		{	double hourly = 7.00;
    			cout << "Hourly Wage: $" << hourly << endl;
    		}	break;
           	case 4: 
    		{	double hourly = 7.00;
    			cout << "Hourly Wage: $" << hourly << endl;
    		}   break;
            case 5:
    		{	double hourly = 9.00;
    			cout << "Hourly Wage: $" << hourly << endl;
    		}   break;
            default:
    		{
    			double hourly = 9.00;
    			cout << "Hourly Wage: $" << hourly << endl;
    		} 
            break;          
        }
    Sample Output
    Code:
    Please enter your first and last name:
    John Smith
    Please enter employee ID number:
    42345
    Please enter your hourly classifcation number 1-5:
    4
    Please enter the amount of hours:
    45
    Hourly Wage: $7
    Name: John Smith
    Employee Id: 42345
    Job Classification: 4
    Hours Worked: 45
    Regular Pay: $160
    Overtime: $30
    You worked an excessive amount of hours! Your total pay is: $190
    PS: please double check your payment calculations as well, @ an hourly wage of $7 I'd not be happy to receive $190 after working 45 hours!

  6. #21
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by sean_cantab View Post
    Just scope the switch statments:
    Code:
    switch (hourly) // At this point the person will choose 1 of the following employee hourly pay wages     {
            case 1: 
            {
                double hourly = 5.50; 
                cout << "Hourly Wage: $" << hourly << endl;
            }    break;
            case 2: 
            {    double hourly = 6.00; 
                cout << "Hourly Wage: $" << hourly << endl;
            }    break;
            case 3: 
            {    double hourly = 7.00;
                cout << "Hourly Wage: $" << hourly << endl;
            }    break;
               case 4: 
            {    double hourly = 7.00;
                cout << "Hourly Wage: $" << hourly << endl;
            }   break;
            case 5:
            {    double hourly = 9.00;
                cout << "Hourly Wage: $" << hourly << endl;
            }   break;
            default:
            {
                double hourly = 9.00;
                cout << "Hourly Wage: $" << hourly << endl;
            } 
            break;          
        }
    Sample Output
    Code:
    Please enter your first and last name:
    John Smith
    Please enter employee ID number:
    42345
    Please enter your hourly classifcation number 1-5:
    4
    Please enter the amount of hours:
    45
    Hourly Wage: $7
    Name: John Smith
    Employee Id: 42345
    Job Classification: 4
    Hours Worked: 45
    Regular Pay: $160
    Overtime: $30
    You worked an excessive amount of hours! Your total pay is: $190
    PS: please double check your payment calculations as well, @ an hourly wage of $7 I'd not be happy to receive $190 after working 45 hours!
    Btw my calculations were working before you fixed my switch statements funny enough lol.
    Not sure where I went wrong the math on paper in the code seems right.

  7. #22
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Yes, that's because hourly is now back to being the classification system and not the wage and so multiplying hours worked with hourly to get total pay will give you a lower number.
    You declare the hourly pay variables inside the switch statements and want to use the values of these variable outside of them to get total pay and therein lies our problem. Try and declare the hourly pay variables at a scope level where they can be used both by the switch statements and by the total pay calculations

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a few errors
    By alberto_1991 in forum C Programming
    Replies: 2
    Last Post: 08-05-2013, 12:44 AM
  2. why am I getting these errors?
    By geekrockergal in forum C Programming
    Replies: 13
    Last Post: 02-07-2009, 09:33 AM
  3. Errors!
    By Moony in forum C Programming
    Replies: 16
    Last Post: 06-26-2006, 04:55 PM
  4. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM
  5. Dev and errors
    By Moffesto in forum C++ Programming
    Replies: 0
    Last Post: 06-22-2002, 12:17 AM

Tags for this Thread