-1

So I perused some of the other articles, but I can't seem to find a reason to why this won't work. I am new to C++ so be kind please.

// User Pay.cpp : Defines the entry point for the console  application.
// This program calculates the user's pay.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    double hours, rate, pay;

    // Get the number of hours worked.
    cout << "how many hours did you work? ";
    cin >> hours;

    // Get the hourly pay rate.
    cout << "How much do you get paid per hour? ";
    cin >> rate;

    //C alculates the Pay.
    pay = hours * rate;

    // Display the pay.
    cout << "You have earned $" << pay << endl;

    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

1

You dont need to include #include "stdafx.h".

Also a better practice for the future is not to include the whole std library ("using namespace std"). Instead of this you can call directly std::cout, std::cin etc...

Also a system("PAUSE") call at the end of the code before "return 0" would be helpful (in your example). So the console doesn't close when the program execute and you can see your result.

Code example:

#include <iostream>
//using namespace std;

int main()
{
    double hours, rate, pay;

    // Get the number of hours worked.
    std::cout << "how many hours did you work? ";
    std::cin >> hours;

    // Get the hourly pay rate.
    std::cout << "How much do you get paid per hour? ";
    std::cin >> rate;

    //C alculates the Pay.
    pay = hours * rate;

    // Display the pay.
    std::cout << "You have earned $" << pay << std::endl;

    system("PAUSE");
    return 0;
}
StormRider
  • 42
  • 7
  • Thats really cool. I am going to save your comment in my code. The code i wrote out is exactly how the book has it written, but i value your input. – Colin Powers Jan 23 '17 at 08:19
  • 1
    also system("pause") should be avoided, because it's system dependent [(link to other SO post)](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong), better of using std::cin.get(); –  Jan 23 '17 at 09:11
0

Try creating an empty project (uncheck precompiled headers). then copy your code but delete #include "stdafx.h".

Laleh
  • 465
  • 3
  • 12
  • You nailed it. Thanks again, I'm still getting the hang of all this. Studying hard! – Colin Powers Jan 23 '17 at 07:41
  • Do you know of a way to keep the original project and fix the error? Is there an option to re-build the precompiled headers, or does one have to add `#include ` to the file stdafx.h? – Peter - Reinstate Monica Jan 23 '17 at 07:53
  • There's no reason enabling precompiled headers should cause an error here. There is something else wrong. – Benjamin Lindley Jan 23 '17 at 07:53
  • @BenjaminLindley well, seems weird but it worked for him. Precompiled headers have made lots of unexpected problems for me! – Laleh Jan 23 '17 at 08:03
  • @Laleh: That may be so. But nobody has learned anything from this answer. If it fixes the problem, it only does so incidentally. It's equivalent to "turn it off and turn it on again". People use precompiled headers every day and have no problems with them. – Benjamin Lindley Jan 23 '17 at 08:06
  • now that we know its precompiled header, i found another article (http://stackoverflow.com/questions/25016806/behaviour-of-precompiled-header-file-causes-error) Basically, all other code MUST come after #include "stdafx.h" and not before. I just tested this theory and its sound. I put all other code AFTER stdafx. – Colin Powers Jan 23 '17 at 08:22
-2

It looks as if you had an error, and then added:

`using namespace std;`

There should be no error now.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
  • The book im working from has `using namespace std;` – Colin Powers Jan 23 '17 at 07:38
  • OK, sorry. This is very weird, the code is correct and it runs just fine on my PC. – Israel Unterman Jan 23 '17 at 07:42
  • No apologies needed. I appreciate the input, this is the first lesson and its already a huge learning experience with all these veteran programmers. Turns out, just putting the code after #include "stdafx.h" is all that was required. Not turning off precompiled headers (even though that also does the trick.) – Colin Powers Jan 23 '17 at 08:27