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?
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace 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;
}
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
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.