Compile error

Sep 25, 2015 at 12:54am
Write your question here.
I need help with an error that is preventing from compiling my code.
the error reads as:
error: 'Jon' was not declared in this scope.

Here is the code as written in vi

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
  
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
  ifstream infile;
  float hourly_rate;
  float regular_wage, overtime_wage, total_wage;
  int hours_worked;
  string name;

  infile.open("work_hours.txt");        //Open the file
  if (!infile) {                        //check if the file exists
    cout << "Cannot open the input file." << endl; // if not, exit the program
    return 1;
}

  if (hours_worked > 40) {
  regular_wage = 40 * hourly_rate;
  overtime_wage = (hours_worked - 40) * hourly_rate * 1.5;
  total_wage = regular_wage + overtime_wage;
}
  else
{
  total_wage = hours_worked * hourly_rate;
}

  getline(infile, Jon Doe);             //Read first employee name
  name = Jon Doe;

  infile >> hourly_rate;                //Read hourly pay rate

  infile >> hours_worked;               //Read hours worked

  cout << name << "'s weekly wage is $ "  << setprecision(2) << total_wage << endl;
}
Sep 25, 2015 at 1:28am
closed account (48T7M4Gy)
you have errors in lines 33 and 34

Jon Doe in line 33, the way you have written it, is a variable not the name of a perso on fil

You should have something like:

1
2
string name;
getline( infile, name );


( name is already declared at line 15 so it doesn't need to be repeated. )
Last edited on Sep 25, 2015 at 1:31am
Sep 25, 2015 at 1:47am
Thank you kemourt. I got the program to compile correctly now but it does not read the inserted file correctly. The program is supposed to read another file with that read as:

1
2
3
Jon Doe
20
30


it is supposed to take these numbers and use them to output Jon Doe's income of that week based off his hours. For whatever reason it comes out as $0 for his earnings. Do I need to declare more strings?
Sep 25, 2015 at 2:02am
closed account (48T7M4Gy)
It wouldn't be, total_wage = hourly_rate * hours_worked;, would it?
Sep 25, 2015 at 2:17am
You think that line isn't correct?
Sep 25, 2015 at 2:26am
closed account (48T7M4Gy)
I'm reasonably sure the line I wrote is correct. It's the same as your line 30 which I missed seeing. However line 30 is in the wrong place because the total_wage is being calculated before, not after which is the correct spot, you have read in the values.

Your calculation is 0 times 0 = $0.
Sep 25, 2015 at 2:31am
so if I switched lines 23-31 with lines 33-41 do you think the program would do the calculations after it reads the information from the other file?
Sep 25, 2015 at 2:36am
closed account (48T7M4Gy)
Shifting lines 23-31 should work. Leave the other lines where they are - you'll see they both don't have to be shifted. Try it. Don't forget to use 'undo/redo' if necessary.

But you are right, the details have to be read in before the calculation is done. You are dealing with a machine not a mind reader. :)
Last edited on Sep 25, 2015 at 2:37am
Sep 25, 2015 at 3:15am
That makes sense, I had gotten certain parts from my professor in a class and didn't even give thought to the order they should have been put in. Thanks for the help!
Topic archived. No new replies allowed.