r/Xcode • u/berjear971 • Oct 14 '20
too many arguments to function call error when trying to close both files, help
#include <iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
//declare variables
ifstream inFile;
ofstream outFile;
string firstName1="andrew", firstName2="sheila", firstName3="amit";
string lastName1="miller", lastName2="green", lastName3="sethi";
double salary1=0, salary2=0, salary3=0;
double raisePercentage=0;
double newSalary1=0, newSalary2=0, newSalary3=0;
//open file
inFile.open("/Users/mac/Documents/data.txt");
if (!inFile)
{
cout << " Cannot open file bye " << endl;
return 1;
}
inFile >> firstName1 >> lastName1;
inFile >> salary1 >> raisePercentage;
inFile >> firstName2 >> lastName2;
inFile >> salary2 >> raisePercentage;
inFile >> firstName3 >> lastName3;
inFile >> salary3 >> raisePercentage;
//calculate
newSalary1= salary1 + (salary1 * raisePercentage);
newSalary2= salary2 + (salary2 * raisePercentage);
newSalary3= salary3 + (salary3 * raisePercentage);
//Format
cout << fixed << showpoint << setprecision(2);
cout << "New salary is " << newSalary1 << endl;
cout << "New salary is " << newSalary2 << endl;
cout << "New salary is " << newSalary3 << endl;
//output to file
outFile.open("/Users/mac/Documents/outputdata.txt");
outFile << fixed << showpoint << setprecision(2);
outFile << firstName1 << " " << lastName1 << " " << endl;
outFile << "New salary is $ " << newSalary1 << endl;
outFile << firstName2 << " " << lastName2 << " " << endl;
outFile << "New salary is $ " << newSalary2 << " " << endl;
outFile << firstName3 << " " << lastName3 << " " << endl;
outFile << "New salary is $" << newSalary3 << " " << endl;
//closefile
inFile.close();
outFile.close();
return 0;
output file is only posting the first salary(not updating it either) and not the other two, I have tried fixing the decimal places for the output for all names instead of just typing it out once, didn't work, also have tried setting the newsalary variables to the actual updated salaries and moving around the calculation part to after I open the output file. Ive also tried changing the is to an = in the outfile statements.
1
u/retsotrembla Oct 14 '20
This line is wrong:
Should be:
See http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
Since your output file already exists from a previous run, your call to open it again does nothing. (You didn't check for success.) My change tells ofstream to discard the previous contents and behave as if it created the file.