I have a program that reads from a file, but for some reason it never reaches the end of the file or something, because the loop never ends. I put only 2 lines of text in the file, and a loop like this still ran infinite:
while (! my_file.eof())
Here is the full code:
Implementation:
//-include the DebitCard implementation
#include "Card.h"
//-include the header for I/O
#include <iostream>
//-include header for files
#include <fstream>
#include <string>
//-using definitions
//-used as a shortcut
using std::cout;
using std::cin;
using std::endl;
/**main()
*-definition of this file
*-goes in the main method
*-main method allows the
*-program to be run
**/
int main()
{
DebitCard dc;
//-char array for filename
char filename[50];
//-double for read amount
double my_double = 0;
//-char array to hold the information read
char info[50];
//-declaration of file object
ifstream my_file;
//-prompt the user
cout << "Enter The File --> ";
//-get the filename from the user
cin.getline(filename, sizeof(filename));
//-open the file based on the input
my_file.open(filename);
//-keep count of lines
int count = 1;
//-loop while it's no the end of the file
while (! my_file.eof())
{
//-get the line from file
my_file.getline(info,sizeof(info));
//-store the value in a double
//-this must be converted
my_double = atof(info);
//-if count is one, then we are on
//-the first line and it is the balance
//-to set the debit card to.
if (count == 1)
{
//-print out the balance
cout << "The Starting Balance Was --> " << my_double << endl;
//-set the balance
dc.set_balance(my_double);
}
else
{
//-variable for what should be withdrew
double actual_amount = my_double;
//-call the withdraw method
dc.withdraw_from(my_double, actual_amount);
//-if the actual amount withdrew is zero,
//-then there was a problem
if (actual_amount == 0)
{
//-show the user there was a problme
cout << "Debit of $" << my_double << " -- Insufficent Funds" << endl;
}
else
{
//-show the user it was successfull.
cout << "Debit of $" << my_double << " Completed" << endl;
}
}
}
//-close the file since we are done
my_file.close();
//-print the final balance of the card
//-use the get_balance() method
cout << "The Final Balance Is $" << dc.get_balance() << endl;
}
You probably don't need to see the header file I created, but if you need to, then let me know.