Please support our C++ advertiser: Programming Forums
Views: 3941 | Replies: 11
![]() |
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
No, you don't need brackets inside the case statements.
I misled you on the way to accomplish this; C++ style strings don't play well with the strings.h library. The C++ style strings can actually be compared by the use of the ==, <=,>=,<, and > operators. So: here's code that works.
You can pass the string arrays by using
and not worrying about the pointers. At least, for logging in.
You'll note I've modified the signin function to return an integer.
Sure, if you return a string, it will let you find the index of the associated balances, but why not just return the index and save yourself a step or two?
In C/C++, a function can only return one value. Anything that is passed to a function as an argument and changed (except for pointers) is unchanged outside of that function.
You need to worry about pointers to the arrays of floats in order to process transfers correctly, so you might as well use pointers for the simpler operations as well.
I've made a few other changes to enhance robustness for you, and made it a little more sensible in terms of the user interface. A few infinite loops let you keep running the program until you explicitly exit, which is always a good thing in this kind of program. You might consider adding in another case to allow the user to logout without exiting the program. That's a very easy (or, as we in the Elec.engineering department say, 'trivial'), addition, but not needed to complete the project.
What you need to do to make the program usable is add the savings and checking array(s), and modify menu to take pointers to the appropriate entries in the float arrays, and have your transaction functions call on those entries. The current use of 'userID' is to remind you to use it (it's the index of the username) to only send two individual entries from the float arrays.
Make sense?
I misled you on the way to accomplish this; C++ style strings don't play well with the strings.h library. The C++ style strings can actually be compared by the use of the ==, <=,>=,<, and > operators. So: here's code that works.
You can pass the string arrays by using
void functionname(string strarray[]);
You'll note I've modified the signin function to return an integer.
Sure, if you return a string, it will let you find the index of the associated balances, but why not just return the index and save yourself a step or two?
In C/C++, a function can only return one value. Anything that is passed to a function as an argument and changed (except for pointers) is unchanged outside of that function.
You need to worry about pointers to the arrays of floats in order to process transfers correctly, so you might as well use pointers for the simpler operations as well.
I've made a few other changes to enhance robustness for you, and made it a little more sensible in terms of the user interface. A few infinite loops let you keep running the program until you explicitly exit, which is always a good thing in this kind of program. You might consider adding in another case to allow the user to logout without exiting the program. That's a very easy (or, as we in the Elec.engineering department say, 'trivial'), addition, but not needed to complete the project.
#include <iostream>
#include <stdlib.h>
using namespace std;
//Function Prototypes
int Signin (string[], string[]);
void Menu (int ID);
void ViewBalance ();
void Deposit ();
void Withdrawal ();
void Transfer ();
void main (void)
{
string Username[] = {"Ohlone1","Ohlone2","Ohlone3","Ohlone4","Ohlone5"};
string PWmain[] = {"Ohlone1pw", "Ohlone2pw", "Ohlone3pw", "Ohlone4pw", "Ohlone5pw"};
int userID;
cout << "WELCOME TO PROGRAMMING BANK!\n\n";
cout << "~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~\n\n";
userID=Signin (Username, PWmain);
Menu (userID);
}
int Signin (string userID[], string PWuser[])
{
string ID, PW;
while(1) //Infinite while loop.
{
cout << "Please enter your username: \n";
cin.width(32);
cin >> ID;
for (int i =0; i < 5; i++)
{
if(ID==userID[i])
{
for(;;) //Infinite for loop
{
cout<<"Please enter "<<userID[i]<<"'s Password."<<endl;
cin >> PW;
if (PW==PWuser[i])
{
cout << "YOU'RE LOGGED IN!\n";
return i;
}
}//end infinite for loop
}
}//end for loop cycling through User IDs.
cout<<"Invalid Login, please try again."<<endl; // Entry was not any of the five known.
}//end while loop
return (-1);//error condition. Should be unreachable.
}
void Menu (int ID)
{
int choice;
while (1)
{
cout << "\t1. View Your Balance.\n";
cout << "\t2. Deposit.\n";
cout << "\t3. Withdraw.\n";
cout << "\t4. Transfer.\n";
cout << "\t5. Exit.\n\n";
cout << "Please enter your choice: \n";
cin >> choice;
switch (choice){
case 1:
//cout << "Your checking account has : " << << endl;
//cout << "Your saving account has : " << << endl;
case 2:
//deposit
case 3:
//withdrawal
case 4:
//transfer
case 5:
//exit. Better to have this explicit than rely on default.
//In this case, 6 will make the person exit the program. Not a good thing
cout << "Thank you for using this program.\n";
return; //Exits menu(), which then exits the program.
default:
cout << "Invalid selection; please select another option.\n";
break;
}//end switch
}//end while loop. This lets you keep choosing until choice==5.
}//end menu()
What you need to do to make the program usable is add the savings and checking array(s), and modify menu to take pointers to the appropriate entries in the float arrays, and have your transaction functions call on those entries. The current use of 'userID' is to remind you to use it (it's the index of the username) to only send two individual entries from the float arrays.
Make sense?
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode