I'm having issues reading multiple lines from a text file.
Jul 5, 2015 at 5:34am UTC
I'm having issues reading multiple lines from a text file.
The file contents is three lines with the following data:
Doe Jane 65789.87 5
Bob Joe 75892.56 6
Bill Jill 74900.50 6.1
Current code only gets me the first line of results and 1 decimal instead of 2 after the decimal point.
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//declared variables
string firstName; //first name
string lastName; //last name
char ch;
string str;
double percentage=0.0; //percentage of increase in salary
double currentsalary = 0.0; //current salary
double newsalary = 0.0; //new salary
double increase; //amount of new pay
ofstream outFile; //write out to file stream variable
ifstream inFile; //read in from file stream variable
//open file to write to
outFile.open("PayDataOutput.out" );
inFile.open("PayData.txt" );
//read from file
inFile >> lastName >> firstName >> currentsalary >> percentage;
//calculations
increase = currentsalary * percentage/100;
newsalary = increase + currentsalary;
//output to file
outFile << firstName << " " << lastName << " $" << newsalary << endl;
//output to screen
cout << firstName << " " << lastName << " $" << newsalary << endl;
//close file
outFile.close();
inFile.close ();
//end of program
system("pause" );
return 0;
}
Last edited on Jul 5, 2015 at 5:39am UTC
Jul 5, 2015 at 7:54am UTC
try a while loop:
while(some condition)
{
infile >> lastName etc ...
}
Jul 5, 2015 at 7:58am UTC
1 decimal instead of 2 after the decimal point.
Increase precision:
std::cout << std::setprecision(10);
Jul 5, 2015 at 11:21am UTC
Not really sure yet how to format the output using the iomanip functions so I just fiddled with the values until it looked good enough.
error: 'system' was not declared in this scope so I commented it out.
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//declared variables
string firstName; //first name
string lastName; //last name
char ch;
string str;
double percentage=0.0; //percentage of increase in salary
double currentsalary = 0.0; //current salary
double newsalary = 0.0; //new salary
double increase; //amount of new pay
ofstream outFile; //write out to file stream variable
ifstream inFile; //read in from file stream variable
//open file to write to
outFile.open("PayDataOutput.out" );
inFile.open("PayData.txt" );
while (inFile >> lastName >> firstName >> currentsalary >> percentage)
{
//calculations
increase = currentsalary * percentage/100;
newsalary = increase + currentsalary;
//output to file
outFile << firstName << " " << lastName << " $" << newsalary << endl;
//output to screen
cout << setiosflags(ios::right)<<setw(10) << firstName
<< setw(13) << lastName << setw(7) << "$" << setprecision(7)
<< newsalary << endl;
}
//close file
outFile.close();
inFile.close ();
//end of program
//system("pause");
return 0;
}
Last edited on Jul 5, 2015 at 11:24am UTC
Jul 5, 2015 at 2:23pm UTC
setprecision
is sticky, so you don't need to call it repeatedly.
right
is terser than
setiosflags(ios::right)
Also, as you're working with currency, it makes sense to also use
fixed
.
Andy
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
// 1. removed pointless comments (if variable is called firstName
// I do not need a comment saying "first name"; it's already clear!)
// 2. moved variable declarations to tightest poss scope (don't
// declare needlessly early)
// 3. declared variable in order of use
// 4. used constructor to open files
// 5. used right rather than setiosflags(ios::right)
// 6. added fixed
// 7. used sticky manipulators just once (before loop)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//declared variables
string lastName;
string firstName;
double currentsalary = 0.0;
double percentageIncrease = 0.0; //percentage of increase in salary
//open files
ifstream inFile("PayData.txt" );
ofstream outFile("PayDataOutput.txt" ); // was .out
//TODO check these opened successfully
//sticky manipulators
cout << fixed << setprecision(2); // now 2 (with fixed precision is num of DP)
while (inFile >> lastName >> firstName >> currentsalary >> percentageIncrease)
{
//calculations
double increase = currentsalary * percentageIncrease / 100.0;
double newSalary = increase + currentsalary;
//output to file
outFile << firstName << " " << lastName << " $" << newSalary << endl;
//output to screen
cout << left
<< setw(10) << firstName
<< setw(13) << lastName
<< right
<< setw( 7) << "$" << newSalary << endl;
}
// close files
outFile.close();
inFile.close ();
//end of program
//system("pause");
return 0;
}
Last edited on Jul 5, 2015 at 2:31pm UTC
Jul 5, 2015 at 6:57pm UTC
I see where I went wrong with the while statement. I made a simple mistake. I took it out in the posting but I get it now. Thanks for clearing it up guys. Same thing with the use of setprecision.
Last edited on Jul 5, 2015 at 6:57pm UTC
Topic archived. No new replies allowed.