| | |
Getting all data from an input and output file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2006
Posts: 5
Reputation:
Solved Threads: 0
I am using VC++. I want to display to the user all data that is stored in an input file and an output file. My input file contains the following so far but will continue to grow as the user uses it:
100.00
123.45
234.56
212.45
Here is what my output file has stored but it also will grow as the user uses the program:
$100 February 10, 2006 2:35:34PM
$23.45 February 10, 2006 2:55:54PM
and so on....
my code so far:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include <ctime>
#include<stdio.h>
#include<conio.h>
#include<iterator>
#include<vector>
using namespace std;
int main()
{
fstream inputOne;
inputOne.open("inputData.txt");
fstream depositInput;
depositInput.open("inputData.txt");
fstream inputTwo;
inputTwo.open("inputData.txt");
ifstream history( "inputData.txt" );
istream_iterator<double> begin( history );
istream_iterator<double> end;
vector<double> input( begin, end );
ofstream outputTwo;
outputTwo.open("outData2.txt",ios::app);
cout<<fixed<<setprecision(2);
float amount,total,totalTwo;
string copyright="©";
int selection;
struct tm *ptr;
time_t tm;
char str[60];
char str1[60];
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? $";
cin>>amount;
while(amount<=0)
{
cout<<"Please enter a positive non-zero withdrawal amount: $";
cin>>amount;
}
while(inputOne.eof() == 0)
{
inputOne>>total;
}
inputOne.close();
totalTwo=total-amount;
inputTwo.seekp( 0, ios::end );
inputTwo<<'\n'<<totalTwo;
cout<<"Here is your new balance: $"<<totalTwo<<endl;
outputTwo<<"-$"<<amount<<" - "<<str1<<'\n';
inputTwo.close();
outputTwo.close();
break;
case 2:
cout<<"How much would you like to deposit? $";
cin>>amount;
while(inputOne.eof() == 0)
{
inputOne>>total;
}
inputOne.close();
totalTwo=total+amount;
depositInput.seekp( 0,ios::end );
depositInput<<'\n'<<totalTwo;
cout<<"Here is your new balance: $"<<totalTwo<<endl;
outputTwo<<"+$"<<amount<<" - "<<str1<<'\n';
depositInput.close();
outputTwo.close();
break;
case 3:
cout<<"Here is your withdrawal and deposit history: "<<endl;
cout<<setfill('*')<<outputTwo<<setw(30)<<history<<endl;
outputTwo.close();
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;
getch();
return 0;
}
100.00
123.45
234.56
212.45
Here is what my output file has stored but it also will grow as the user uses the program:
$100 February 10, 2006 2:35:34PM
$23.45 February 10, 2006 2:55:54PM
and so on....
my code so far:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include <ctime>
#include<stdio.h>
#include<conio.h>
#include<iterator>
#include<vector>
using namespace std;
int main()
{
fstream inputOne;
inputOne.open("inputData.txt");
fstream depositInput;
depositInput.open("inputData.txt");
fstream inputTwo;
inputTwo.open("inputData.txt");
ifstream history( "inputData.txt" );
istream_iterator<double> begin( history );
istream_iterator<double> end;
vector<double> input( begin, end );
ofstream outputTwo;
outputTwo.open("outData2.txt",ios::app);
cout<<fixed<<setprecision(2);
float amount,total,totalTwo;
string copyright="©";
int selection;
struct tm *ptr;
time_t tm;
char str[60];
char str1[60];
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? $";
cin>>amount;
while(amount<=0)
{
cout<<"Please enter a positive non-zero withdrawal amount: $";
cin>>amount;
}
while(inputOne.eof() == 0)
{
inputOne>>total;
}
inputOne.close();
totalTwo=total-amount;
inputTwo.seekp( 0, ios::end );
inputTwo<<'\n'<<totalTwo;
cout<<"Here is your new balance: $"<<totalTwo<<endl;
outputTwo<<"-$"<<amount<<" - "<<str1<<'\n';
inputTwo.close();
outputTwo.close();
break;
case 2:
cout<<"How much would you like to deposit? $";
cin>>amount;
while(inputOne.eof() == 0)
{
inputOne>>total;
}
inputOne.close();
totalTwo=total+amount;
depositInput.seekp( 0,ios::end );
depositInput<<'\n'<<totalTwo;
cout<<"Here is your new balance: $"<<totalTwo<<endl;
outputTwo<<"+$"<<amount<<" - "<<str1<<'\n';
depositInput.close();
outputTwo.close();
break;
case 3:
cout<<"Here is your withdrawal and deposit history: "<<endl;
cout<<setfill('*')<<outputTwo<<setw(30)<<history<<endl;
outputTwo.close();
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;
getch();
return 0;
}
•
•
Join Date: Feb 2006
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Ancient Dragon
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?
Oh sorry,
, I wanted to know how to display to the user the input and output files. The input contains floats, and the output contains floats and strings. How do I display the data?If I loop the files starting from the beginning using seek, will it work?, how do I write it, an example would be great, Thanks!!
first, this is wrong
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.
Not sure about the other question(s) -- don't have time right now to look at it.
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
while(inputOne>>total) { // do something }
Not sure about the other question(s) -- don't have time right now to look at it.
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.
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.
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Feb 2006
Posts: 5
Reputation:
Solved Threads: 0
ifstream history( "inputData.txt" );
istream_iterator<double> begin( history );
istream_iterator<double> end;
vector<double> input( begin, end ); - - - - - - - Error occurred here....why does this happen, it happened before when I had my original code.
error C2664: '__thiscall std::vector<double,class std::allocator<double> >::std::vector<double,class std::allocator<double> >(unsigned int,const double &,const class std::allocator<double> &)' : cannot convert parameter 1 fr
om 'class std::istream_iterator<double,char,struct std::char_traits<char> >' to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
istream_iterator<double> begin( history );
istream_iterator<double> end;
vector<double> input( begin, end ); - - - - - - - Error occurred here....why does this happen, it happened before when I had my original code.
error C2664: '__thiscall std::vector<double,class std::allocator<double> >::std::vector<double,class std::allocator<double> >(unsigned int,const double &,const class std::allocator<double> &)' : cannot convert parameter 1 fr
om 'class std::istream_iterator<double,char,struct std::char_traits<char> >' to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
compiles ok with VC++ 2005 Express. post your entire program.
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- fstream problems. No output to file. (C++)
- passing data to a program (C++)
- "Input past end of File" error #62 (Legacy and Other Languages)
- Please I need help formatting an output file and sorting the names! (C++)
- Sequential Access Data Files (Visual Basic 4 / 5 / 6)
- transfer input.txt to output.txt help prz (C++)
Other Threads in the C++ Forum
- Previous Thread: problem opening files
- Next Thread: assigning a string from a file to a variable
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






