So I'm a function over form kind of person and my code looks it but I'm having two different problems first a little info on the file I am working on for a programming final project. as it stands it reads in allows for new users to sign up and then prints that account into a txt file like so
100000 1530 Matt Smith 500
100001 456 Drew Carey 500
100002 948 David Tennent 500
100003 1115599 Robin Williams 500
the first is the account number (assigned by the program)
second is a pin the user enters
third and fourth are the users name
and the fifth is a floating balance for their account (initial balance assigned by the program)
after someone is enrolled they can re -launch the program and pull up their account by either the account number or their first and last name. The account search works fine but a similar coded name search does not, it only works if the name you are searching for is the first name on the list (I moved the names around to make sure). once the account is found it prompts for a pin and compares that to the pin for the account (also works). Once verified it currently asks the user for an amount to add to their balance, then creates a new txt file copies the data over until it reaches the accessed account as it gets passed to the new file the balance gets updated. The old file is deleted and the new file gets renamed to the old file name. this part works except that the last account gets read twice (not using eof but instead a do statement with a while .good statement.
In summary
1st issue is name search only ever works for first name in list
2nd issue final line printed twice during file copy
Relevent code follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
case 1: // Existing user looking by account number (this works)
{
cout << "Please enter your full account number \n";
cin >> accountNumber;
fstream account ("accounts.txt", ios::in | ios::out);
if(account.is_open())
{
do
{
getline(account,input);
istringstream iss(input);
for(i=0; i<5; i++)
{
iss >> accountInfo[i];
}
temp = atoi(accountInfo[0].c_str());
if(temp == accountNumber)
{
cout << "Your account has been found \n";
accountFound = true;
break;
}
}while(account.good());
if(accountFound == false)
{
cout <<"Your account was not found \n";
return 1;
}
cout << "Welcome " << accountInfo[2] << " " << accountInfo[3] << endl;
cout << "Please enter your pin number \n";
temp = atoi(accountInfo[1].c_str());
for(i = 0; i < 3; i++)
{
cin >> pinNumber;
if(pinNumber == temp)
{
cout << "Thank your your account balance is $" << atof(accountInfo[4].c_str()) << endl;
balance = atof(accountInfo[4].c_str());
accountNumber = atoi(accountInfo[0].c_str());
break;
}
else
{
cout << "That pin does not match our records you have enter the wrong pin " << (i+1) << " time(s) \n" ;
cout << "Please retry your pin \n";
}
if(i==2)
{
cout << "You have entered the wrong pin number 3 times program will now close \n";
return 1;
}
}
break;
account.close();
}
}//exisiting user looking by account number ends here
case 2: //existing user looking by name starts here (this doesnt work except the first account in the file)
{
cout << "Please enter your first name \n";
cin >> firstName;
cout << "Please enter your last name \n";
cin >> lastName;
fstream account ("accounts.txt", ios::in | ios::out);
if(account.is_open())
{
do
{
getline(account,input);
istringstream iss(input);
for(i=0; i<5; i++)
{
iss >> accountInfo[i];
}
tempFirstName = accountInfo[2];
tempLastName = accountInfo[3];
if(firstName == tempFirstName && lastName == tempLastName)
{
cout << "Your account has been found \n";
accountFound = true;
break;
}
if(accountFound == false)
{
cout <<"Your account was not found \n";
return 1;
}
}while(account.good());
cout << "Welcome " << accountInfo[2] << " " << accountInfo[3] << endl;
cout << "Please enter your pin number \n";
temp = atoi(accountInfo[1].c_str());
for(i = 0; i < 3; i++)
{
cin >> pinNumber;
if(pinNumber == temp)
{
cout << "Thank your your account balance is $" << atof(accountInfo[4].c_str()) << endl;
balance = atof(accountInfo[4].c_str());
break;
}
else
{
cout << "That pin does not match our records you have enter the wrong pin " << (i+1) << " time(s) \n" ;
cout << "Please retry your pin \n";
}
if(i==2)
{
cout << "You have entered the wrong pin number 3 times program will now close \n";
return 1;
}
}
}
break;
account.close();
} // existing user looking by name ends here
|
This is where the file is copied
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
cout << "Now that we have your account number lets adjust your balance enter what to add\n"; //temporary balance updater begins here
cin >> bet;
balance += bet;
fstream account ("accounts.txt", ios::in | ios::out);
if(account.is_open())
{
ofstream tempAccount;
tempAccount.open ("tempaccounts.txt");
do
{
getline(account,input);
istringstream iss(input);
for(i=0; i<5; i++)
{
iss >> accountInfo[i];
}
temp = atoi(accountInfo[0].c_str());
if(temp == accountNumber)
{
stringstream ss(stringstream::in | stringstream::out);
ss << balance;
tempBalance = ss.str();
accountInfo[4] = tempBalance;
}
tempAccount << accountInfo[0] << " " << accountInfo[1] << " " << accountInfo[2] << " " << accountInfo[3] << " " << accountInfo[4] << endl;
if(account.eof())
{
break;
}
}while(account.good());
}
account.close();
remove ("accounts.txt");
rename(oldName,newName); //temporary balance updater ends here
|
due to character limits I could not post the whole program but any ideas?