Help with Input Files and Output Files

I have an output file with this information:

Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1

I need to read each line in and make changes to the salary (third number) and then output the information into a new file, the code I have written below works when I use g++ to compile but when I try to use XCode, it leaves me with an empty output file. Any suggestions?

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

using namespace std;

int main() {
        //Creating all variables;
        string lastName;
        string firstName;
        double salary;
        double percentChange;
        double updatedSalary;
        ifstream salaryData;
        ofstream salaryUpdatedData;

        //Opening my input and output files
        salaryData.open("Ch3_Ex6Data.txt");
        salaryUpdatedData.open("Ch3_Ex6Output.dat");

        //Checking to see if a line exists in my input file
        while (salaryData >> lastName >> firstName >> salary >> percentChange) {
                //Updating salary to the new salary based on percent increase or decrease
                percentChange *= .01;
                updatedSalary += salary + (salary * percentChange);

                //Showing two decimal places
                salaryUpdatedData << fixed << showpoint << setprecision(2);

                //Outputting the new salary into the output file
                salaryUpdatedData << firstName << " " << lastName << " " << updatedSalary << endl;

                //resetting the updatedSalary value
                updatedSalary = 0;
        }

        //Closing my input and output files
        salaryUpdatedData.close();
        salaryData.close();

        return 0;
}


Thanks in advance!
Check whether the files could be opened. The problem with using relative path is that the progam is searching for the file in the current path which might not be what you expect.
I'm a wee bit rusty on my C++ but I think I see a potential bug waiting to bite you.
You haven't initialized updatedSalary, but you then use it in the line

updatedSalary += salary + (salary * percentChange);

You initialize it at the end of the loop but on the first iteration the
value is undefined.

Either initialize at the declaration or move your last loop line
up to the top.
Hello subeydoo,

Both previous suggestions are good. I would add that you should initialize all your variables when you define them. Not only a good idea, but a good practice.

A thought I have is that compiling with g++ there may not be a problem with the path to the input and output files whereas XCode does have an issue with the path to the files. When using XCode to compile with you might have to use the full path to the files for it to work right.

Just a thought because I have not used XCode yet.

For what its worth the code compiled and ran fine for me using Visual Studio 2015.

Hope that helps,

Andy
Topic archived. No new replies allowed.