Hello, attempted a computer science degree at one time, then figured it wasnt for me, and now I want to be a paramedic. Anywho, I somehow got thru the first 2 courses of compsci, along with the first assembly language course, so I have some programming in my brain, but I can't remember the really complex stuff like stacks, pointers, headers, that sort of stuff. I went to write a program the other day for fun, just to see if I could do it, and I remembered some shit. I then attempted to write a "bank" program for ppl who owe me money. Now, my main problem is that each time I run the program, it erases the data that was sent to the file. I'm sure I'm forgetting something really simple, or else my design isn't complex enough, but if there is a solution without using a bunch of pointers and a stack and such, that'd be great, but if not, could someone plz remind me how to do that stuff. Anyways, here is the source code I have thus far:
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
char accnt(string &out);
int stats(string &out, int &dollaz, char &paid);
string name;
int sel;
bool end = 1;
char choice;
int main ()
{
ifstream input;
input.open("Accounts.txt");
string aname;
int moneyz;
char paidup;
cout<<"Oh hello, who the fuck might you be?\n";
getline(cin,name);
while ( end != 0)
{
input.open("Accounts.txt");
cout<<"Well " << name << " we is in bank. Make a selection:\n";
cout<<"1. Account Stats\n";
cout<<"2. Credit Ratings\n";
cout<<"3. Edit Info\n";
cout<<"You choose...";
cin>>sel;
if(sel == 1)
{
input.open("Accounts.txt");
while(end != 0)
{
accnt(aname);
cout<<"Your Account for"<<" "<<aname<<":\n";
stats(aname,moneyz,paidup);
cout<<"You wanna see any other stats?\n";
cin>>choice;
if(choice == 'y')
end = 1;
else
{
cout<<"Bye Bitch!\n";
end = 0;
}
}
input.close();
}
else if(sel == 2)
{
input.open("Accounts.txt");
cout<<"Enter name of person:";
cin>>aname;
cout<<"Hmm... Credit rating of"<<" "<<aname<<" "<<"is..."<<" ";
if(paidup == 'y')
cout<<"rating is good, for now...\n";
else
cout<<"AW SHIT, THIS MOTHER FUCKER ISN'T GONNA BE USING MY BANK AGAIN! HORRIBLE!\n";
input.close();
}
else if(sel == 3)
{
ofstream oot("Accounts.txt");
input.open("Accounts.txt");
cout<<"Who's Account will you be fuckin with?\n";
cin>>aname;
oot<<aname;
cout<<"Account for"<<" "<<aname<<" ";
cout<<"How much does"<<" "<<aname<<" "<<" owe?\n";
cin>>moneyz;
oot<<moneyz;
cout<<"Is"<<" "<<aname<<" "<<"paid up?(y or n)\n";
cin>>paidup;
oot<<paidup;
input.close();
oot.close();
}
else
cout<<"Invalid Selection.";
cout<<"Are you finished today?(y or n)\n";
cin>>choice;
if(choice == 'y')
{
cout<<"Bye Bitch!\n";
end = 0;
}
else
end = 1;
input.close();
}
input.close();
}
char accnt(string &out)
{
bool final = 1;
char aans;
Yes I have a twisted sense of humor, the comments are not aimed at anyone in particular, I simply find them amusing to add in, so plz don't take offence. Any help is greatly appreciated.
I'll be playin bass until a reply,
Thanks,
Uncle Ernie
--------
| o o |
| |
| || |
| |
| |
| ------|
---------
I think you need to read the entire file into memory then save it with the changes after the program is finished... maybe with a date/time in the file name. Before you find out who is in the bank your data file has to be brought in. The simplest way is probably to have a data file like:
name | amount owed | as of: date
Then you can load the data into say three parallel vectors name, amount, date. So that name[i] owes amount[i] as of date[i]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <vector>
#include <string>
...
vector<string> name(size); // make size big enough, but the vector will resize if needed
vector<double> amount(size);
vector<int> date(size); //an easy date format would be YEARMONTHDAY so today is 20090806, this is good for sorting too
//now we can get the data
int i=0;
while(!EOF){ //continue until end of file
input >> name[i] >> amount[i] >> date[i];
i++;
}
//now use your data to tell people nasty things........
//then write the data to a file using another loop
You could make some different objects here to handle your data and the if/elseif/elseif but maybe you should finish this version first.
Oh ya, thanks a lot, I was thinking I needed to possibly use something to check for end of file, but I couldn't remember the syntax for it. Thanks, I shall try that, as it makes a lot of sense that you would want to keep being able to input until the end of the file.
int main ()
{
Name_Ptr head = NULL;
ofstream oot;
ifstream iin;
oot.open("Accounts.txt");
char aname[MAX_NAME_LEN + 1];
int moneyz;
char paidup[MAX_NAME_LEN + 1];
cout<<"Oh hello, who the fuck might you be?\n";
getline(cin,name);
while ( end != 0)
{
oot.open("Accounts.txt");
cout<<"Well " << name << " we is in bank. Make a selection:\n";
cout<<"1. Account Stats\n";
cout<<"2. Credit Ratings\n";
cout<<"3. Update\n";
cout<<"You choose...";
cin>>sel;
if (in.fail())
cout<<"Couldn't open this shit up..."<<endl;
}while(in.fail());
}
My questions are, 1. It still seems to be erasing the file each time I open the program, and 2.
What would be the best way to get input from the user, and where?