944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4573
  • C++ RSS
Feb 13th, 2006
0

Getting all data from an input and output file

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sixtysecasasin is offline Offline
5 posts
since Feb 2006
Feb 13th, 2006
0

Re: Getting all data from an input and output file

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?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Feb 13th, 2006
0

Re: Getting all data from an input and output file

Quote 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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sixtysecasasin is offline Offline
5 posts
since Feb 2006
Feb 13th, 2006
0

Re: Getting all data from an input and output file

first, this is wrong
C++ Syntax (Toggle Plain Text)
  1. while(inputOne.eof() == 0)
  2. {
  3. inputOne>>total;
  4. }

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)
  1. while(inputOne>>total)
  2. {
  3. // do something
  4. }

Not sure about the other question(s) -- don't have time right now to look at it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Feb 13th, 2006
0

Re: Getting all data from an input and output file

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.


C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<string>
  5. #include <ctime>
  6. #include<iterator>
  7. #include<vector>
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. fstream stream("inputData.txt", ios::in | ios::out | ios::app);
  13. if(!stream.is_open())
  14. {
  15. cout << "stream not opened" << endl;
  16. return 1;
  17. }
  18.  
  19. ifstream history( "inputData.txt" );
  20.  
  21. istream_iterator<double> begin( history );
  22. istream_iterator<double> end;
  23.  
  24. vector<double> input( begin, end );
  25.  
  26. cout<<fixed<<setprecision(2);
  27.  
  28. float amount,total,totalTwo;
  29. string copyright="©";
  30. int selection;
  31.  
  32. struct tm *ptr;
  33. time_t tm;
  34. char str[126];
  35. char str1[126];
  36.  
  37. tm = time(NULL);
  38. ptr = localtime(&tm);
  39. strftime(str ,100 , "Today is %B %d, %Y. The current time is: %I:%M:%S %p.\n",ptr);
  40. strftime(str1,100,"%B %d, %Y - %I:%M:%S %p",ptr);
  41. cout<<str<<endl;
  42.  
  43. cout<<"Welcome to the Automated Teller Machine Program."<<endl;
  44. cout<<'\n';
  45. cout<<"What would you like to do today?"<<endl;
  46. cout<<"(1)Withdraw"<<endl;
  47. cout<<"(2)Deposit"<<endl;
  48. cout<<"(3)View Withdrawal & Deposit History"<<endl;
  49. cout<<"(4)Exit"<<endl;
  50. cout<<"(5)About"<<endl;
  51. cout<<"Please choose an option by pressing (1), (2), (3), (4), or (5): ";
  52. cin>>selection;
  53.  
  54. switch (selection)
  55. {
  56. case 1:
  57. cout<<"How much would you like to withdraw? $";
  58. amount = 0;
  59. do
  60. {
  61. cin>>amount;
  62. if(amount<=0)
  63. {
  64. cout<<"Please enter a positive non-zero withdrawal amount: $";
  65. }
  66. }while(amount <= 0);
  67. totalTwo = 0;
  68. while(stream >> total)
  69. {
  70. totalTwo += total;
  71. }
  72. stream.clear(); // clear errors
  73. totalTwo -= amount;
  74. stream<<-amount << endl;
  75. cout<<"Here is your new balance: $"<<totalTwo<<endl;
  76. break;
  77.  
  78. case 2:
  79. cout<<"How much would you like to deposit? $";
  80. cin>>amount;
  81. totalTwo = 0;
  82. stream.clear();
  83. stream.seekg(0);
  84. while(stream>>total)
  85. totalTwo += total;
  86.  
  87. totalTwo += amount;
  88. cout<<"Here is your new balance: $"<<totalTwo<<endl;
  89. stream.clear();
  90. stream << amount << endl;
  91. break;
  92.  
  93. case 3:
  94. cout<<"Here is your withdrawal and deposit history: "<<endl;
  95.  
  96. //cout<<setfill('*')<<outputTwo<<setw(30)<<history<<endl;
  97. break;
  98.  
  99. case 4:
  100. cout<<"Good Bye."<<endl;
  101. break;
  102.  
  103. case 5:
  104. cout<<"The Automated Teller Machine Program"<<endl;
  105. cout<<"Copyright "<<copyright<<"2005"<<endl;
  106. cout<<"ChackoItty Corporation"<<endl;
  107. break;
  108.  
  109. default:
  110. cout<<"Incorrect selection. Good Bye."<<endl;
  111. break;
  112. }
  113.  
  114. cout<<"Exiting the program.... please press any key"<<endl;
  115. cin.ignore();
  116.  
  117. return 0;
  118. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Feb 13th, 2006
0

Re: Getting all data from an input and output file

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sixtysecasasin is offline Offline
5 posts
since Feb 2006
Feb 13th, 2006
0

Re: Getting all data from an input and output file

compiles ok with VC++ 2005 Express. post your entire program.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. ifstream history( "inputData.txt" );
  10.  
  11. istream_iterator<double> begin( history );
  12. istream_iterator<double> end;
  13.  
  14. vector<double> input( begin, end );
  15. return 0;
  16. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem opening files
Next Thread in C++ Forum Timeline: assigning a string from a file to a variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC