Line duplication in project.

closed account (L074GNh0)
I am having a problem with the duplication of a line I'm outputting from a file. Whenever I run the program the last line in the "New accounts" portion is always repeated. Can anyone help with this problem before 11:59 PM Sunday?
Here is my code for reference:



#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>


// Function prototypes
void ProcessAccounts(std::ifstream &file, std::ofstream &file1, double);
void PrintFileContents(std::ifstream &file);
std::ifstream GetInputFile(std::string);
std::ofstream GetOutputFile(std::string);
void SetInputStreamPos(std::ifstream &, int);
void SetOutputStreamPos(std::ofstream &, int);


//[BEGIN MAIN]
int main()
{
std::string inputFileName;
std::string outputFileName;
double interestRate;

std::cout << "Enter an input filename: ";
std::cin >> inputFileName;
std::cout << "Enter an output filename: ";
std::cin >> outputFileName;
std::cout << "Enter an interest rate (%): ";
std::cin >> interestRate;

std::cout << std::endl;
std::ifstream ifile = GetInputFile(inputFileName);
std::ofstream ofile = GetOutputFile(outputFileName);

std::cout << "Current account status:" << std::endl;
PrintFileContents(ifile);

ifile = GetInputFile(inputFileName);

ProcessAccounts(ifile,ofile,interestRate);

ifile.close();
ofile.close();

std::cout << std::endl;
std::cout << "New account status:" << std::endl;
ifile = GetInputFile(outputFileName);
SetInputStreamPos(ifile,0);
PrintFileContents(ifile);

ifile.close();

std::cout << "Press ENTER";
std::cin.ignore();
std::cin.get();
return 0;
}
//[END MAIN]

void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate)
{
// Everyday the bank checks the status of the accounts and processes them
// If the customer has:
// 1) a positive savings balance, pay them interest
// 2) a negative savings balance, transfer it to credit (credit is positive)
// 3) a credit balance, charge them interest
// Note: Unlike normal banks, the interest rate for savings and credit is the same

// Read the info from the input file
// Put the new info into the output file
// The format for the input file and output file should be
// [NAME] [SAVINGS BALANCE] [CREDIT BALANCE]
// There is a single space between each value and a newline at the end

// Put your code here

double save;
std::string name;
double credit;

while (!ifile.eof())
{
ifile >> name >> save >> credit;

if (save >= 0)
{
double diff = save;
diff = diff*(1+(rate/100));
save = diff;
credit = credit *(1+(rate/100));
}

else if ((credit > save)&&(save < 0))
{
double diff2= credit-save;
diff2=diff2 * (1+(rate/100));
save = 0;
credit=diff2;
}

ofile << name <<" " << std::fixed << std::setprecision(2) << save << " "<< std::fixed << std::setprecision(2) <<credit << std::endl;
}
}

void PrintFileContents(std::ifstream &str)
{
// Print the contents of the file
// First, print the file headers
// Then, print each line.
// Make sure the text is properly formatted
// Remember the functions available in iomanip?

// EXAMPLE:
// Name Savings Credit
// Bob $23.56 $0.00
// Joe $43.52 $0.00
// Sally -$1.58 $0.00

// Put your code here
std::string name;
double saving;
double credit;

std::cout << std::left << std::setw(13) << "Name" << std::setw(10) << "Savings" << "Credit" << std::endl << std::fixed << std::showpoint;
while (!str.eof() == true)
{
str >> name >> saving >> credit;
std::cout << std::setw(13) << name << std::setw(10) << std::fixed << std::setprecision(2) << saving << std::fixed << std::setprecision(2) <<credit <<std::endl;
}





}

std::ifstream GetInputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an input file stream

// Put your code here
std::ifstream Name(filename);
if (!Name)
{
std::cerr << "File couldn't be opened" << std::endl;
exit(1);
}
return Name;
}

std::ofstream GetOutputFile(std::string filename)
{
// Open the file named filename
// Make sure it exists
// Return it as an output file stream

// Put your code here
std::ofstream Name(filename);
if (!Name)
{
std::cerr << "File couldn't be opened" << std::endl;
exit(1);
}
return Name;
}

void SetInputStreamPos(std::ifstream &str, int pos)
{
// Set the 'g'et cursor to the desired byte in the stream
// Use the beginning of the file as the reference point

// Put your code here
str.seekg(pos,std::ios::beg);
}

void SetOutputStreamPos(std::ofstream &str, int pos)
{
// Set the 'p'ut cursor to the desired byte in the stream
// Use the beginning of the file as the reference point

// Put your code here
str.seekp(pos, std::ios::beg);
}
closed account (L074GNh0)
Sorry, I forgot to mention that the probem is in the PrintFileContents function
This is the wrong way to write the loop:
1
2
3
4
while (!ifile.eof()) 
{ 
    ifile >> name >> save >> credit; // this may fail
    ...


Instead,
1
2
3
while( ifile >> name >> save >> credit )
{
   // ... 

closed account (L074GNh0)
Thank you . It started to work correctly after I did this.
Topic archived. No new replies allowed.