Dec 7, 2015 at 9:10pm UTC
Hello! I am having a hard time understanding why my increastPct variable outputs 87654.23 to a file. Any advisement is appreciated. Also any advice on how to get it to output the rest. It only outputs the first line. I tried using count++ inside the loop at the end of the syntax
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int x;
struct employeeType
{
string fname;
string lname;
double current_salary;
double pay_increase;
double updated_salary;
};
int main ()
{
ifstream inFile;
ofstream outFile;
outFile << fixed << showpoint;
outFile << setprecision (2);
string fname = "" ;
string lname = "" ;
double increasePct = 3.5;
double originalSal = 0.0;
double increaseAmt = 0.0;
double updatedSal = 0.0;
inFile.open("Company.txt" );
outFile.open("Company.out" );
if (!inFile)
{
cout << "Cannot open the input file. " << "The program terminates" << endl;
return 1;
}
cout << "Processing Data. Please check Company.out for the results.\n" << endl;
inFile >> fname >> lname >> increasePct >> originalSal >> increaseAmt >> updatedSal;
outFile << " HillTop Chocolate Company \n" << " -------------------------\n" << "Employee Name" << " " << "Increase %" << " " << "Original Salary" << " "
<< "Increase Amount" << " " << "Updated Salary\n" << "-----------------------------------------------------------------------------------------------\n" << endl;
if (!inFile.eof())
{
outFile << fname << " " << lname << " "
<< increasePct << "% " << originalSal << " " << increaseAmt << " " << updatedSal << endl;
//count++;
}
system("Pause" );
return 0;
}
Last edited on Dec 7, 2015 at 9:17pm UTC
Dec 7, 2015 at 9:26pm UTC
the if else
is not a loop, you might want to use while
or for
to actually loop trough the file line by line.
if
outFile <<
will grab first line only, since read is done once only.
Dec 7, 2015 at 9:28pm UTC
increasePct is changed only in two places. It has an initial value of 3.5 which is replaced by whatever is read from the input file. Are you sure the file is being read properly? A sample of the input file "Company.txt" would be helpful.
Dec 7, 2015 at 9:31pm UTC
Okay the only thing I changed was while (!inFile.eof()) instead of if (!inFile.eof((). It prints out the first user's information indefinitely. Here is my code.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int x;
struct employeeType
{
string fname;
string lname;
double current_salary;
double pay_increase;
double updated_salary;
};
int main ()
{
ifstream inFile;
ofstream outFile;
outFile << fixed << showpoint;
outFile << setprecision (2);
string fname = "" ;
string lname = "" ;
double increasePct = 3.5;
double originalSal = 0.0;
double increaseAmt = 0.0;
double updatedSal = 0.0;
inFile.open("Company.txt" );
outFile.open("Company.out" );
if (!inFile)
{
cout << "Cannot open the input file. " << "The program terminates" << endl;
return 1;
}
cout << "Processing Data. Please check Company.out for the results.\n" << endl;
inFile >> fname >> lname >> increasePct >> originalSal >> increaseAmt >> updatedSal;
outFile << " HillTop Chocolate Company \n" << " -------------------------\n" << "Employee Name" << " " << "Increase %" << " " << "Original Salary" << " "
<< "Increase Amount" << " " << "Updated Salary\n" << "-----------------------------------------------------------------------------------------------\n" << endl;
int count;
while (!inFile.eof())
{
outFile << fname << " " << lname << " "
<< increasePct << "% " << originalSal << " " << increaseAmt << " " << updatedSal << endl;
count++;
}
7
system("Pause" );
return 0;
}
Last edited on Dec 7, 2015 at 9:31pm UTC
Dec 7, 2015 at 9:46pm UTC
Oh my apologies. I thought I would need to make a new topic. I will resolve this one and post in the original. Thanks for that clarification jlb
Dec 7, 2015 at 9:47pm UTC
Last edited on Dec 7, 2015 at 9:48pm UTC