Hello kcattgirl,
jonnin has made some good points, but I think I should start with this first:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
// Section 1.
// Header files.
#include <iostream>
#include <limits>
// Section 2.
// Prototypes.
//Section 3.
// Global variables. Not advisable to use global variables.
// Section 4.
int main()
{ // <--- Start of main block.
// Section 5.
// Declare Variables.
// Section 6.
// Main Code.
std::cout << "\n Hello World" << std::endl;
// Section 7.
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
} // <--- End of main block.
// Section 8.
// Functions.
/*
Section 1. Your header files that the program will need.
Section 2. For future reference.
Section 3. This is where you would put global variables. Although it is best to avoid a regular variable here as the whole program will have access to it and it canbe changed anywhere in the program. A variable defined as a constant is OK because yo may want the whole file to see this variable.
Section 4. Is the main function. Every program needs only oneand this is where the program will start execution.
Section 5. Some will say to define your variables just before they are needed, but I find this makes it hard to have to search through the whole program to find them. When I started in C we learned to put all the variables at the beginning of a function and this something I am just use to. Whichever way you choose just be consistent in what you do.
Section 6. This is where the code of "main" starts.
section 7. This is something I use to keep the console window open so I can have time to read anything that is last displayed before the window closes because the program ends. You may want to use this or not it is up to you.
Section 8. Not something you need to worry about right now. Some choose to put this section above "main". I like to make "main" the first function and put anything else after "main" or in its own file.
*/
|
Yes I purposely left out the line
using namespace std;
as it
WILL get you in trouble some day. Now is the time to learn how to qualify what is in the standard name space and what is in the standard name space while it is easier and slower.
Line 14 and 34 define the block of main. This is where all your work goes. Not above, below or in-between:
as yo did with the constant variables.
Looking at your last code you have:
1 2 3 4 5 6 7 8 9
|
int main()
constexpr double PAY_RATE1{ 5.50 };
constexpr double PAY_RATE2{ 6.00 };
constexpr double PAY_RATE3{ 7.00 };
constexpr double PAY_RATE4{ 9.00 };
constexpr double PAY_RATE5{ 12.00 };
constexpr double MAX_REGULAR_HOURS{ 40.0 };
constexpr double OVERTIME_RATE{ 1.5 };
{ // <--- Opening brace of main.
|
Lines 2 - 8 need to come after line 9.
As jonnin is saying the switch condition is based on the variable "job_class" which has not yet been defined as the compiler sees it. When I tried to compile your last code I received 83 errors and a good portion of those are because a variable is not defined before it is used or you did not define some of your variables properly. The second part of this switch is that it is coming to soon in the program. I needs to come after you input a value for "job_class".
This bit of code is still not right:
1 2 3 4 5 6 7 8 9 10 11 12
|
int hours_worked; //
job_class,
total_hours, // hours_worked - regular_hours
regular_pay, // regular_hours * hourly_rate
more_pay, // regular_hours * hourly_rate
less_pay, // hours_worked * hourly_rate
overtime_pay, // (hours_worked - regular_hours) * 1.5 * hourly_rate
total_pay // regular_pay + overtime_pay
char name[30]; // employee name (first and last)
id_number[6]; // I.D Number
|
Line 1 is OK and properly defined.
Lines 3 - 9 do not have a type and line 9 is missing the semi-colon at the end. Line 12 should end with a comma or line 13 should start with a type.
Based on what I see this is what I would start with:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int job_class{}; // <--- Because the switch need an "int"
// <--- As a start these variables would work better as a double.
double hours_worked{},
total_hours{}, // hours_worked - regular_hours
regular_pay{}, // regular_hours * hourly_rate
more_pay{}, // regular_hours * hourly_rate
less_pay{}, // hours_worked * hourly_rate
overtime_pay{}, // (hours_worked - regular_hours) * 1.5 * hourly_rate
total_pay{}; // regular_pay + overtime_pay
char name[30]{}, // employee name (first and last)
id_number[6]{}; // I.D Number
|
Notice in lines 1 and 4 I switched the variable names.
For lines 12 and 13 I would suggest using a "std::string" unless you have to use the character array. Since you have included the header file "string" you should use it.
You are working with a program that used time and money. It would make thing easier if you make most of your variables "double"s as doing math with "int"s and "double"s can lead to a wrong answer. until you learn how to deal with "int"s and "double"s in a calculation it is best to stick with "double"s since you are dealing with money.
Almost forgot you have no need right now for the header file "cmath". This is just adding more code to the program that you do not need.
I am curious why you think you need the lines
cin >> ws;
in your input section?
Hope that helps,
Andy