Thread: Im getting errors

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    23

    Im getting errors

    I am doing a homework assignment and I am pretty new to switch and if else statements, but I am getting errors regarding the math portion of the assignment. I have never had this trouble before I started using the switch and if statements.

    Code:
    #include <iostream>#include <fstream>
    #include <string>
    #include <iomanip>
    #include <cmath>
    
    
    int main()
    {
    	using namespace std;	
    	string emp_name;
    	int hours_worked;
    	int hourly;
    	int total_hours;
    	const double regular_hours = 40;
    	
    	
    	cout << "Please enter your first and last name:" << endl;
    	getline(cin, emp_name);
    	
    	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 << "You are in classification 1 of employee pay"  << endl;
    		break;
    		case 2: hourly = 6.00;
    		cout << "You are in classification 2 of employee pay"  << endl;
    		break;
    		case 3: hourly = 7.00;
    		cout << "You are in classification 3 of employee pay"  << endl;
    		break;
    		case 4: hourly = 9.00;
    		cout << "You are in classification 4 of employee pay"  << endl;
    		break;
    		case 5: hourly = 12.00;
    		cout << "You are in classification 5 of employee pay"  << endl;
    		break;
    		default: 
    		cout << "This number is invalid please start over and enter the correct number." << endl;
    		break;			
    	}
    	if (hours_worked < 40)
    	cout << "You need to work more hours:" << endl;
    	else 
    	cout << "Good job you worked 40 hours!" << endl;
    	
    	
    	hours_worked * hourly = total_hours;
    	regular_hours * hourly = total_hours;
    	(hours_worked - regular hours) * 1.5 * hourly = total_hours;
    		
    }
    Error 1-3:lvalue required as left operand of assignment Lines 57-59

    Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At a glance, there appears to be a few problems:
    • hourly is an int, which is right for you to use it in the switch's condition. However, you also assign double values to hourly. Perhaps you should rename hourly to hourly_classification, and then declare a double variable named hourly_pay to assign to in the switch.
    • total_hours is an int, but you apparently want to assign a double value to it. Perhaps you should declare total_hours to be a double instead. Furthermore, it looks like you cannot decide if total_hours actually means "total number of hours" or if it should mean "total amount of pay".
    • You swapped the left hand side and right hand side subexpressions of your assignment expressions, e.g., to assign to total_hours, this:
      Code:
      (hours_worked - regular hours) * 1.5 * hourly = total_hours;
      should have been:
      Code:
      total_hours = (hours_worked - regular hours) * 1.5 * hourly;
      This mistake is the one responsible for the compile error that you observed. Also, as mentioned previously, because hourly is really hourly_pay, this computation computes total amount of pay, not total number of hours.
    • You seem to uselessly assign to total_hours two more times (bearing in mind the incorrect swap of subexpressions).
    • You don't seem to have printed the final output. I also suspect that the output that you did print should be based on the actual total_hours (as in not the total amount of pay) rather than hours_worked.
    Last edited by laserlight; 11-06-2016 at 12:07 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    What's with the endl fetish? Why not just "stuff\n"? : )

    Code:
    hours_worked * hourly = total_hours;
    regular_hours * hourly = total_hours;
    (hours_worked - regular hours) * 1.5 * hourly = total_hours;
    Okay, let's take the first line - can you explain in your own words what you want to do here? Can you then look at it carefully, and explain to us what it actually does? [edit: Oh well, laserlight already explained that one. : ) ]

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Heh, in retrospect I think the formula I stated as being for total amount of pay might not be so, but since we were not shown the requirements... that is left as an exercise for Pastryking
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    I don't want the answer I want to understand why and how to fix mistakes I only give the information needed for what I don't understand lol.
    Last edited by Pastryking; 11-06-2016 at 12:38 PM.

  6. #6
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by laserlight View Post
    Heh, in retrospect I think the formula I stated as being for total amount of pay might not be so, but since we were not shown the requirements... that is left as an exercise for Pastryking
    Code:
    1. The job classification of each employee determines the employee's hourly rate. The classifications are as follows: Classification Hourly_Rate 1 5.50 2 6.00 3 7.00 4 9.00 5 12.000(use a switch statement) When an invalid Classification Type is entered, the hourly rate from classification type 1 is to be used to calculate the employee's wages and an appropriate message is to be printed after the calculated output.
    
    2. Calculate regular pay: (use a two-way if statement)♦ When an employee works the regular number of hours: regular_hours * hourly_rate♦ When an employee works more than the regular number of hours: regular_hours * hourly_rate♦ When an employee works less than the regular number of hours: hours_worked * hourly_rate
    
    3. Calculate overtime pay: (use a two-way if statement)An employee is paid 1.5 times their regular hourly rate for all hours worked over the regular hours, otherwise, the overtime rate is 0.♦ When an employee works more than the regular number of hours:(hours_worked - regular_hours) * 1.5 * hourly_rate
    
    4. An appropriate message is to be generated for any employee who works less than 40 hours or more than 60 hours. Examples: Inadequate number of hours worked! or Excessive number of hours Worked! This message is to be printed after the calculated output. (Use one-way if statements)Input to this Program: 1. The Employee's Name (First & Last).2. The Employee's Id. Number.3. The Employee's Job Classification Number.4. The Number of Hours Worked.Output From This Program:1. All User Input.2. Number of Overtime Hours.3. Hourly Rate.4. Total Amount to be paid to the Employee.5. Any message that was generated.
    Is the requirements which I am a little off from still.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Pastryking
    I don't want the answer I want to understand why and how to fix mistakes I only give the information needed for what I don't understand lol.
    Then you should try to come up with the smallest and simplest program that you expect should compile, but which demonstrates the error. For example:
    Code:
    int main()
    {
        int x = 1;
        int y = 2;
        int z;
        x + y = z;
    }
    You will get a similiar error message. The reason is that the x + y subexpression results is a temporary value that is not an lvalue, i.e., it cannot go on the left hand side of an assignment expression.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by laserlight View Post
    Then you should try to come up with the smallest and simplest program that you expect should compile, but which demonstrates the error. For example:
    Code:
    int main()
    {
        int x = 1;
        int y = 2;
        int z;
        x + y = z;
    }
    You will get a similiar error message. The reason is that the x + y subexpression results is a temporary value that is not an lvalue, i.e., it cannot go on the left hand side of an assignment expression.

    Thank you I understand why that makes total sense now. I have another question as well I made some changes with declaring everything to the int which is probably all I will need, but for some reason my if else statements are not working correctly maybe its just the way I am coding them. Less than 40 hours doesnt give me any numbers and more than 40 hours only gives me the math for 40 hours any ideas why?

    Code:
        if (hours_worked < 40)
        {
            total_pay = hours_worked * hourly;
            cout << "You need to work more hours! Your total pay is: $" << endl;
        }
        else if (hours_worked = 40)
        {
            total_pay = regular_pay * hourly;
            cout << "Good job you worked 40 hours! Your pay is: $" << total_pay << endl;
        }
        else if (hours_worked > 40)
        {
            total_pay = hours_worked * hourly;
            total_pay = regular_pay * hourly;        
                    total_pay = (hours_worked - regular_pay) * 1.5 * hourly;
            cout << "You worked an excessive amount of hours! Your total pay is: $" << total_pay << endl;
        }
    Output of less than 40 hours:
    Code:
    Please enter your first and last name:Joe
    Please enter your hourly classifcation number 1 -5:
    3
    Please enter the amount of hours:
    20
    You are in classification 3 of employee pay
    You need to work more hours! Your total pay is: $
    Output of more than 40 hours:
    Code:
    Please enter your first and last name:Joe
    Please enter your hourly classifcation number 1 -5:
    5
    Please enter the amount of hours:
    50
    You are in classification 5 of employee pay
    Good job you worked 40 hours! Your pay is: $480

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For less than 40 hours, look closely at how you wrote the display output statements (i.e. cout) for the other cases. Can you spot something missing?

    For equal to 40 hours, you confused assignment (=) and comparison (==). Because of that, the case > 40 hours doesn't work.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by Elysia View Post
    For less than 40 hours, look closely at how you wrote the display output statements (i.e. cout) for the other cases. Can you spot something missing?

    For equal to 40 hours, you confused assignment (=) and comparison (==). Because of that, the case > 40 hours doesn't work.
    Thank you very much I really need to work on my debugging skills :/ this is only a 101 class too.

  11. #11
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Just wanted to say I appreciate everyone's help on here today its been great as always. One last question its an issue I am having with the math problems with the math portion of the output.I am almost certain I did the math part correctly.

    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 
        {
            double hourly;
            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 - regular_pay) * 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;
    }
    Output:
    Code:
     Please enter your first and last name:James Printer
    Please enter employee ID number:
    77321
    Please enter your hourly classifcation number 1-5:
    2
    Please enter the amount of hours:
    45
    Hourly Wage: $6
    Name: James Printer
    Employee Id: 77321
    Job Classification: 2
    Hours Worked: 45
    Regular Pay: $80
    Overtime: $15
    You worked an excessive amount of hours! Your total pay is: $95
    Any ideas why?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Line 35: You can't declare variables inside a switch statement outside a case statement.
    Line 35: You need to separate the two hourly variables. They obviously mean different things, but they're also different types!
    Line 88: I assume it's supposed to be overtime = (hours_worked - 40) * 1.5 * hourly? As it is, it doesn't seem to make sense to me. You want to calculate 1.5 * hourly_rate for all the overtime hours, right?

    But you need to fix line 35 before this code starts to make any sense. This shouldn't compile.
    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.

  13. #13
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by Elysia View Post
    Line 35: You can't declare variables inside a switch statement outside a case statement.
    Line 35: You need to separate the two hourly variables. They obviously mean different things, but they're also different types!
    Line 88: I assume it's supposed to be overtime = (hours_worked - 40) * 1.5 * hourly? As it is, it doesn't seem to make sense to me. You want to calculate 1.5 * hourly_rate for all the overtime hours, right?

    But you need to fix line 35 before this code starts to make any sense. This shouldn't compile.
    As is it compiles fine I needed to make hourly a double(could be float as well) inside of the switch in order for it to calculate case 1 and I thought I was allowed to declare inside of the switch statement but also thought I had to declare it differently in order to get the switch statement to work. As is the math of the current statement without making it a double will make it only do the math of 5 instead of 5.50. for case 1.
    The reason 40 is declared as regular_pay my professor asked for that beginning of the semester.

    As for the overtime issue yes that is what I would like to do.

    What program would you recommend because you are not the first person to say this shouldn't compile. I currently use dev c++ is program i use.
    Last edited by Pastryking; 11-06-2016 at 04:50 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Pastryking View Post
    As is it compiles fine I needed to make hourly a double(could be float as well) inside of the switch in order for it to calculate case 1 and I thought I was allowed to declare inside of the switch statement but also thought I had to declare it differently in order to get the switch statement to work
    If you redeclare hourly, you will get a new variable. If you declare it in a case statement, it will be local to the switch, and hence will lost its value when the switch goes away (because you assign to the local hourly).

    What program would you recommend because you are not the first person to say this shouldn't compile. I currently use dev c++ is program i use.
    Visual Studio and Code::Blocks are two popular (and very good) options. Make sure you grab gcc with Code::Blocks package in case you intend to use that (called MinGW on Windows).
    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.

  15. #15
    Registered User
    Join Date
    Oct 2016
    Posts
    23
    Quote Originally Posted by Elysia View Post
    Visual Studio and Code::Blocks are two popular (and very good) options. Make sure you grab gcc with Code::Blocks package in case you intend to use that (called MinGW on Windows).
    Ill have to try that. Though it seems when I fix one problem there is another.
    Now my output no matter what I put for my hourly( job classification) comes out as.
    Code:
            cout << "Job Classification not found please try again" << ".            Default Pay of 5.50 being calculated." << endl;
    Regardless of the number after I switched from double inside the switch to nothing completely removing it. These errors are fascinating me tbh lol.
    Last edited by Pastryking; 11-06-2016 at 05:20 PM.

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