1. please use code tags when posting a lot of code.
2. you forgot to ask a question. Or did you just want us to see your gorgeous code?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
first, this is wrong
while(inputOne.eof() == 0)
{
inputOne>>total;
}
It should be coded like this because eof() doesn't catch other errors that might occur which will cause an infinite loop in the program.
while(inputOne>>total)
{
// do something
}
Not sure about the other question(s) -- don't have time right now to look at it.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I stripped all those fstream objects out of the program so that I could see easier what was going on. The below version works -- for menu items 1 (depesit) and 2 (withdrawal) only.
If you want to do both input and output on the same file you should use only one fstream object, not two.
After reading the whole file, the stream has the eof error bit set, and that needs to be cleared before the stream can be used again. See example that I posted.
when ios::app is used it is not necessary to specifically seek to end-of-file before appending new data -- fstream will do that for you.
You don't need conio.h or stdio.h -- so I deleted them.
str[] and str1[] buffers were too small, strftime() was causing buffer overflow.
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include <ctime>
#include<iterator>
#include<vector>
using namespace std;
int main()
{
fstream stream("inputData.txt", ios::in | ios::out | ios::app);
if(!stream.is_open())
{
cout << "stream not opened" << endl;
return 1;
}
ifstream history( "inputData.txt" );
istream_iterator<double> begin( history );
istream_iterator<double> end;
vector<double> input( begin, end );
cout<<fixed<<setprecision(2);
float amount,total,totalTwo;
string copyright="©";
int selection;
struct tm *ptr;
time_t tm;
char str[126];
char str1[126];
tm = time(NULL);
ptr = localtime(&tm);
strftime(str ,100 , "Today is %B %d, %Y. The current time is: %I:%M:%S %p.\n",ptr);
strftime(str1,100,"%B %d, %Y - %I:%M:%S %p",ptr);
cout<<str<<endl;
cout<<"Welcome to the Automated Teller Machine Program."<<endl;
cout<<'\n';
cout<<"What would you like to do today?"<<endl;
cout<<"(1)Withdraw"<<endl;
cout<<"(2)Deposit"<<endl;
cout<<"(3)View Withdrawal & Deposit History"<<endl;
cout<<"(4)Exit"<<endl;
cout<<"(5)About"<<endl;
cout<<"Please choose an option by pressing (1), (2), (3), (4), or (5): ";
cin>>selection;
switch (selection)
{
case 1:
cout<<"How much would you like to withdraw? $";
amount = 0;
do
{
cin>>amount;
if(amount<=0)
{
cout<<"Please enter a positive non-zero withdrawal amount: $";
}
}while(amount <= 0);
totalTwo = 0;
while(stream >> total)
{
totalTwo += total;
}
stream.clear(); // clear errors
totalTwo -= amount;
stream<<-amount << endl;
cout<<"Here is your new balance: $"<<totalTwo<<endl;
break;
case 2:
cout<<"How much would you like to deposit? $";
cin>>amount;
totalTwo = 0;
stream.clear();
stream.seekg(0);
while(stream>>total)
totalTwo += total;
totalTwo += amount;
cout<<"Here is your new balance: $"<<totalTwo<<endl;
stream.clear();
stream << amount << endl;
break;
case 3:
cout<<"Here is your withdrawal and deposit history: "<<endl;
//cout<<setfill('*')<<outputTwo<<setw(30)<<history<<endl;
break;
case 4:
cout<<"Good Bye."<<endl;
break;
case 5:
cout<<"The Automated Teller Machine Program"<<endl;
cout<<"Copyright "<<copyright<<"2005"<<endl;
cout<<"ChackoItty Corporation"<<endl;
break;
default:
cout<<"Incorrect selection. Good Bye."<<endl;
break;
}
cout<<"Exiting the program.... please press any key"<<endl;
cin.ignore();
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
compiles ok with VC++ 2005 Express. post your entire program.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream history( "inputData.txt" );
istream_iterator<double> begin( history );
istream_iterator<double> end;
vector<double> input( begin, end );
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343