-4

I am getting a problem while using

 cin >> PayRate 

and

 cin >> H_worked 

what could be the problem? Even the compiler doesn't show any errors but when program runs the 2nd and 3rd cin values aren't read by compiler.

Program:

#include <iostream.h>
#include<conio.h>
int main()
{
    int employeeName, H_worked;
    float PayRate, GrossPay, NetPay;
    cout << "Please input your employee's name:";
    cin >> employeeName;
    cout << "Input hourly wage rate:";

    cin >> PayRate;
    cout << endl;
    cout << "Input hours worked :";

    cin >> H_worked;
    cout << endl;
    if (H_worked > 40)
    {
        H_worked = H_worked*1.5;
        GrossPay = H_worked*PayRate;
        cout << "Your employees gross pay for this week is" << GrossPay << endl;
        NetPay = GrossPay - (GrossPay* 3.625);
        cout << "Your employees net pay is" << NetPay << endl;
    }
    else (H_worked <= 40);
    {
        GrossPay = H_worked*PayRate;
        cout << "Your employees gross pay for this week is" << GrossPay << endl;
        NetPay = GrossPay - (GrossPay*3.625);
        cout << "And your employees net pay is" << NetPay << endl;
    }
    return 0;
    getch();

}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • First of all, you should write your code in such a way that humans can read it. Also, don't use a [tag:c] for a `std::cin` question, that's specific to c++. And finally the compiler has nothing to do with this. – Iharob Al Asimi Jan 27 '16 at 12:53
  • 5
    Why is `employeeName` an `int`? Are you entering characters there? – NathanOliver Jan 27 '16 at 12:53
  • 1
    Proper indentation matters. Also you tagged your question [tag:compiler-errors] but your description mentions that the compiler does *not* give any errors and the program runs. Please fix that. (P.S: *cin values aren't read by compiler* the compiler does not ready any values, it's your program that reads the values. All the compiler does is turn your source code into an executable) – Borgleader Jan 27 '16 at 12:54
  • once your program runs, the compiler does not read anything. – 463035818_is_not_an_ai Jan 27 '16 at 12:56
  • I went ahead and fixed the code indentation. See how much easier it is to read versus what you had originally? – NathanOliver Jan 27 '16 at 12:59
  • 2
    Many compilers would warn that the statement `(H_worked <= 40);` (which is your entire `else` branch) has no effect. – molbdnilo Jan 27 '16 at 13:02

1 Answers1

1

You declared employeeName as an int but that does not make any sense as names have letters not numbers. If you are actually entering in character data then this will cause cin to fail and that will make any subsequent call fail as well. This fits with your description of what is happening. To fix this first we need to make employeeName a std::string so we can store letters.

int employeeName, H_worked;
//becomes
std::string employeeName;
int H_worked;

Then we need to change the input method. Since a name can have spaces in it we need to use std::getline instead of >> to get the name as >> will stop when it sees a space.

cout << "Please input your employee's name:";
std::getline(std::cin, employeeName);

You also have a semicolon at the end of your else condition

else (H_worked <= 40);

This means that

{
    GrossPay = H_worked*PayRate;
    cout << "Your employees gross pay for this week is" << GrossPay << endl;
    NetPay = GrossPay - (GrossPay*3.625);
    cout << "And your employees net pay is" << NetPay << endl;
}

Will always run as ; ends the else part.

Then we have the issue that you are using non standard includes. In standard c++

#include <iostream.h>

Should be

#include <iostream>

As all standard includes omit the .h. Since all of the standard components live in the std namespace you are also going to have to deal with that. You can either put std:: in front of all the std components or you can put using std::cout;, using std::cin, ect. I would not suggest using using namespace std; for the reasons outlined in Why is “using namespace std;” considered bad practice?

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402