Write a Program That Uses a Structure to Store the Following Data About a Customer Account in C++
#1
- D.I.C Head
Reputation: 0
- Posts: 61
- Joined: 09-December 08
Struct with arrays in functions
Posted 14 January 2009 - 07:26 AM
I'm working on an assignment its been awhile since i have been coding and need a little help, also i have yet to ever do struct before
Write a program that uses a structure to store the following data about a customer account: * Name (full name in one variable) The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element and display all the data stored in the array. The program should have a menu-driven interface and use functions as appropriate. Input validation: when the data for a new account is entered, be sure the user enters data for all the fields (no empty fields). No negative account balances should be entered.
* Address (full street address in one variable)
* City, State and Zip
* Telephone number
* Account balance
* Date of last payment
Help need with:
1) calling the function to display just the balance.
2) calling the function just too allow the person edit the balance.
-I dont know how to call a different customer (i.e. 2 customers they want to edit #2)
#include <cstdlib> #include <iostream> using namespace std; void Acc(string , int, int AcArray[]); void Load(string, string, int AcArray[]); void Display(int AcArray[]); void menu(); double CBalance(int AcArray[]); void question(); struct account { char name[30]; //customer name char address[30]; // address char city[16]; //city char state[2]; // State two letters char zip[5]; // zip 5 numbers char tele[12]; // telephone number 12 characters double account; // account total char date[20]; // date int total[30]; // total array length }; int main(int argc, char *argv[]) { int AcArray[30]; //menu; system("PAUSE"); return EXIT_SUCCESS; } void menu { int choice; cout << "Welcome to you account management software, which action would you like to take?" << endl; cout << "Press 1. Enter a new account." << endl; cout << "Press 2. Display current account balances." << endl; cout << "Press 3. Edit current accounts." << endl; cout << "Press 4. Edit current balance of an account." << endl; cin >> choice; swtich(choice) { case 1: cout << "You have chosen to enter a new account." << endl; Acc; break; case 2: CBalance;//call account balance function break; case 3: cout << "Which account would you like to edit?" //Display account numbers //Call account number selected break; case 4: cout << " Which account balance would you like to edit?" //Display all current account numbers //Call the array stored with account selected number } } void question { char YN; cout << "Would you like to return to the main menu y or n?"; cin >> YN; switch(YN) { case y: menu; break; case n: return 0; break; } } double CBalance(int AcArray[]) { cout <<"current balance is: " << account << endl; } void Acc(int total, int c, AcArray[]) { cout << "How many account would you like to have?"; cin >> total; Accout AcArray[total] for(int c = 0; c < total; c++) { Load(total, c, AcArray); } for(int d = 0; d < total; d++) { Display(AcArray); } } void Load(int total, int c, AcArray[]) { cout << "Enter the full name of customer(First Last) "; cin >> name; cout << endl; cout << "Enter the Address(i.e. 3470 NW 193 ST) of the customer "; cin >> address; cout << endl; cout << "Enter the City of customer "; cin >> city; cout << endl; cout << "Enter the State of customer "; cin >> state; cout << endl; cout << "Enter the Zip Code of customer "; cin >> zip; cout << endl; cout << "Enter the Telephone number(i.e 4597894561) of customer "; cin >> tele; cout << endl; cout << "Enter the current account balance(i.e 789.55) of customer "; cin >> account; cout << endl; cout << "Enter the last Day(i.e March 5, 2009) paid by customer "; cin >> date; cout << endl; } void Display { cout << " Customer " << c << " Name is " << AcArray[] << endl; cout << " Customer " << c << " Address is " << AcArray[] << endl; cout << " Customer " << c << " City is " << AcArray[] << endl; cout << " Customer " << c << " State is " << AcArray[] << endl; cout << " Customer " << c << " Zip Code is " << AcArray[] << endl; cout << " Customer " << c << " Telephone number is " << AcArray[] << endl; cout << " Customer " << c << " current account balance is $" << AcArray[] << endl; cout << " Customer " << c << " last Day paid is " << AcArray[] << endl; }
Is This A Good Question/Topic? 0
#2 Linkowiezi
- D.I.C Regular
Reputation: 58
- Posts: 316
- Joined: 07-October 08
Re: Struct with arrays in functions
Posted 14 January 2009 - 08:07 AM
Hi I changed some things in your code, see the comments for more detail.
struct account { // Cnanged all char's to strings string name; //customer name string address; // address string city; //city string state; // State two letters string zip; // zip 5 numbers string tele; // telephone number 12 characters // double account; // account isn't a double, it is declared as a struct so remove it char date[20]; // date // int total[30]; // total array length should't be here neither, but if you want it, don't use it as an array int balance; // Added balance, as it is a requirement for the assignment and also quite hard to display if it itsn't here ;)/> }; void editBalance( account AcArray[], const int MAXACCOUNTS ) { for( int i = 0; i < MAXACCOUNTS; i++ ) AcArray[i].balance = i; // This will edit the balance of each account } void displayBalance( account AcArray[], const int MAXACCOUNTS ) { for( int i = 0; i < MAXACCOUNTS; i++ ) cout << AcArray[i].balance; // This will print the balance of each account } int main(int argc, char *argv[]) { const int MAXACCOUNTS = 30; // Added a variable to use trought the program int account AcArray[MAXACCOUNTS]; // Add 'account' here //menu; editBalance( AcArray, MAXACCOUNTS ); displayBalance( AcArray, MAXACCOUNTS ); system("PAUSE"); return EXIT_SUCCESS; }
EDIT: Was in a hurry since I'm at work atm so I just tossed it togeather real quick, hope it works. Also didn't go trough you entire code fully in my example. You'll need to change quite a bit in the rest of your code for it to compile but I hope this will help you a bit along the road
This post has been edited by Linkowiezi: 14 January 2009 - 08:11 AM
#3 codecsmash49
- D.I.C Head
Reputation: 0
- Posts: 61
- Joined: 09-December 08
Re: Struct with arrays in functions
Posted 14 January 2009 - 09:02 AM
Thank you very much
#4 David W
Reputation: 298
- Posts: 1,839
- Joined: 20-September 08
Re: Struct with arrays in functions
Posted 14 January 2009 - 02:42 PM
Since you are using C++ and C++ strings now ... you might as well go for a vector container to hold each struct (instead of an array of structs) ...
See the simple example below ...
Shalom,
David
http://developers-he.../index.p...opic,106.0.html
http://developers-he...index.php/topic,46.0.html
//You could do something like this ... #include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; struct Data { string name; string id; int balance; // in cents }; bool more(); int main() { // create a empty vector like this vector< Data > myBank; // now using push_back ... read in the file Data tmp; // create a tmp entry do { cout << "Name : "; getline( cin, tmp.name ); cout << "ID : "; getline( cin, tmp.id ); cout << "Balance : "; cin >> tmp.balance; if( !cin.good() ) { cin.clear(); cin.sync(); continue; } cin.sync(); myBank.push_back( tmp ); // now ... enlarge the vector }while( more() ); // show elements in vector ... for (unsigned i=0; i<myBank.size(); ++i ) { cout << setw(4)<< i+1 << " " << setw(20) << myBank[i].name << " " << setw(8) << myBank[i].id << " " << setw(8) << myBank[i].balance << endl; } system("pause"); } bool more() { cout << "More (y/n) ? "; int reply = cin.get(); return !(reply=='n' || reply=='N'); }
#5 David W
Reputation: 298
- Posts: 1,839
- Joined: 20-September 08
Re: Struct with arrays in functions
Posted 14 January 2009 - 05:18 PM
Or ... since ...
// The program should use an array of at least 20 structures.
// It should let the user enter data into the array, change
// the contents of any element and display all the data stored
// in the array.
You could do something like this ... ( as a shell to get started )
#include <iostream> #include <string> #include <iomanip> using namespace std; const int MAX = 20; struct Data { string name; string id; int balance; // in cents }; bool more(); int main() { // create a empty array like this Data myBank[MAX]; int i=0; do { cout << "Name : "; getline( cin, myBank[i].name ); cout << "ID : "; getline( cin, myBank[i].id ); cout << "Balance : "; cin >> myBank[i].balance; if( !cin.good() ) { cin.clear(); cin.sync(); continue; } cin.sync(); ++i; }while( more() && i<MAX ); // show elements in each record ... for (int j=0; j<i; ++j ) { cout << setw(4)<< j+1 << " " << left << setw(20) << myBank[j].name << " " << setw(8) << myBank[j].id << " " << right << setw(8) << myBank[j].balance << endl; } cout << "\nPress 'Enter' to continue ... " << flush; cin.sync(); cin.get(); } bool more() { cout << "More (y/n) ? "; int reply = cin.get(); return !(reply=='n' || reply=='N'); }
This post has been edited by David W: 14 January 2009 - 07:11 PM
#6 codecsmash49
- D.I.C Head
Reputation: 0
- Posts: 61
- Joined: 09-December 08
Re: Struct with arrays in functions
Posted 17 January 2009 - 11:58 AM
Thanks for the help here is what i have been able to complete. I'm having some problems debugging the program tho =/
#include <cstdlib> #include <iostream> #include <iomanip> using namespace std; void menu(); void acc(string ,int AcArray[30]); //c for customer number. void display(string , int AcArray[30]); void editBalance(string,int AcArray[30]); void displayBalance(); struct customer { string name; string address; string city; string state; string zip; string tele; //double acc; char date[20]; double balance; }; int main(int argc, char *argv[]) { const int MaxAccounts = 30; int customer AcArray[MaxAccounts]; menu; editBalance( AcArray, MaxAccounts ); displayBalance( AcArray, MaxAccounts ); system("PAUSE"); return EXIT_SUCCESS; } void menu { int choice; //char YN; cout << "Welcome to you bank company that has lost all your money. " << endl; sleep(1000); cout << "Please choose an action from the menu displayed below." << endl; cout << "Press 1 to enter a new account(s)." << endl; cout << "Press 2 to see current balance." << endl; cout << "Press 3 to edit current balance." << endl; cout << "Press 4 to exit." << endl; cin >> choice switch(choice) { case 1: acc; break; case 2: cout << "current account balances are " << customer[c].acc << endl; bool more() { cout << "Continue (y/n) ? "; int reply = cin.get(); return !(reply=='n' || reply=='N'); } break; case 3: editBalance; break; case 4: return 0; break; } } double acc(customer AcArray[], const int MaxAccounts ) { //int c; int total; //char YN; cout << "How many customers would you like to enter?" << endl; cin >> total; for(int c = 0; c < total; c++) { cout << "Enter the name( First Last) of customer " << total; cin >> AcArray[c].name; //validate cout << "Enter the adress(i.e. 4758 nw 0 st) of customer " << total; cin >> AcArray[c].address; //validate cout << "Enter the city(i.e. Florida) of customer " << total; cin >> AcArray[c].city; //validate no numbers cout << "Enter the state(i.e. FL) of customer " << total; cin >> AcArray[c].state; //validate 2 letters only! cout << "Enter the 5 digit zip code(i.e.12345) of customer " << total; cin >> AcArray[c].zip; //validate 5 numbers cout << "Enter the telephone number(ie. 123-456-7890) of customer " << total; cin >> AcArray[c].tele; //validate no letters. cout << "Enter the current account balance of customer " << total; cin >> AcArray[c].balance; //validate double && non-neg cout << "Enter the Date(i.e. 11 05 2009) of the last payment of customer " << total; cin >> AcArray[c].date; //validate } /* cout << endl; cout << "Would you like to return to the menu(y or n): "; cin >> YN*/ bool more() { cout << "Continue (y/n) ? "; int reply = cin.get(); return !(reply=='n' || reply=='N'); } } void display(string, int c, int AcArray[]) { for(int c = 0; c < total; c++) { cout << "Customer " << c << " account information: " << endl; cout << endl; cout << "The customer's name is " << AcArray[c].name << endl; cout << AcArray[c].name <<"'s address is " << AcArray[c].address << endl; cout << AcArray[c].name <<" is currently living in the city " << AcArray[c].city << endl; cout << AcArray[c].name <<" is currently living in the state of " << AcArray[c].state << endl; cout << AcArray[c].name <<"'s zip code is " << AcArray[c].zip << endl; cout << AcArray[c].name <<"'s telephone number is " << AcArray[c].tele << endl; cout << AcArray[c].name <<"'s current account balance is " << AcArray[c].balance << endl; cout << "The last day "<< AcArray[c].name <<" paid was " << AcArray[c].date << endl; } switch(YN) { case y: menu; break; case n: return 0; } } void editBalance( account AcArray[], const int MaxAccounts ) //Display each account seperaetely { for( int c = 0; c < MaxAccounts; c++ ) AcArray[c].balance = c; } void displayBalance( account AcArray[], const int MaxAccounts ) { for( int c = 0; c < MaxAccounts; c++ ) cout << AcArray[c].balance; bool more() { cout << "Continue (y/n) ? "; int reply = cin.get(); return !(reply=='n' || reply=='N'); } }
#7 David W
Reputation: 298
- Posts: 1,839
- Joined: 20-September 08
Re: Struct with arrays in functions
Posted 17 January 2009 - 04:41 PM
I not sure if you 'appreciate the point' of giving you a 'shell of WORKING code' to start out from ... ?
It is helpful to add only ONE new function at a time and compile and test that function ... and to get that all debugged and tested before you add any more.
This way ... you will limit where you need to look in your code for problems ... as to why it won't now compile ... or if it compiles ok ... why it gives the 'not so funny' output it does now.
Please post your last code that compiled and produced the output that you desired ... and then describe what (next) function(s) you wish to add and the code that you have for them so far ...
Shalom,
David
#8 codecsmash49
- D.I.C Head
Reputation: 0
- Posts: 61
- Joined: 09-December 08
Re: Struct with arrays in functions
Posted 17 January 2009 - 07:14 PM
I had it working up until i made getting the info into a function and made the Menu function
#include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; struct Info { string name; string address; string city; string state; string zip; string tele; double balance; // in cents string date; }; bool more(); void start(); int main() { menu(); system("pause"); } bool more() { cout << "More (y/n) ? "; int reply = cin.get(); return !(reply=='n' || reply=='N'); } void menu() { int choice; //char YN; cout << "Welcome to you bank company that has lost all your money. " << endl; cout << "Please choose an action from the menu displayed below." << endl; cout << "Press 1 to enter a new account(s)." << endl; cout << "Press 2 to see current balance." << endl; cout << "Press 3 to edit current balance." << endl; cout << "Press 4 to exit." << endl; cin >> choice switch(choice) { case 1: start(); break; case 2: cout << "current account balances are " << << endl; bool more() { cout << "Return (y/n) ? "; int reply = cin.get(); return !(reply=='n' || reply=='N'); } break; case 3: break; case 4: return 0; break; } } void start { vector < Info > Bank; Info file; do { cout << "Name : "; getline( cin, file.name ); cout << "Address : "; getline( cin, file.address ); cout << "City : "; getline( cin, file.city ); cout << "State : "; getline( cin, file.state ); cout << "Zip : "; getline( cin, file.zip ); cout << "Tele : "; getline( cin, file.tele ); cout << "Date : "; getline( cin, file.date ); cout << "Balance : "; cin >> file.balance; if( !cin.good() ) { cin.clear(); cin.sync(); continue; } cin.sync(); Bank.push_back( file ); }while( more() ); for (double i = 0; i < Bank.size(); ++i ) { cout << setw(4) << "Customer's number " << i+1 << endl; cout<< setw(20) << "Customer's name is " << Bank[i].name << endl; cout<< setw(20) << "Customer's address is " << Bank[i].address << endl; cout<< setw(20) << "Customer's current city of residence is " << Bank[i].city << endl; cout<< setw(20) << "Customer's current state of residence is " << Bank[i].state << endl; cout<< setw(20) << "Customer's zip code is " << Bank[i].zip << endl; cout<< setw(20) << "Customer's current telephone number is " << Bank[i].tele << endl; cout<< setw(20) << "Customer's late date of payment is " << Bank[i].date << endl; cout<< setw(20) << "Customer's late recorded account balance is " << Bank[i].balance << endl; } }
#9 David W
Reputation: 298
- Posts: 1,839
- Joined: 20-September 08
Re: Struct with arrays in functions
Posted 18 January 2009 - 03:59 AM
Your code, modified as below, will now compile ... Please take it from there.
Shalom,
David
http://developers-he...index.php/topic,46.0.html
http://developers-he.../index.p...opic,106.0.html
#include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; // Globals .... struct Info { string name; string address; string city; string state; string zip; string tele; double balance; string date; }; // using a Global variable here (to keep function calls simple ... to start) vector < Info > Bank; bool more(); Info takeIn(); void flushCin(); void newAcc(); void menu(); void showAll(); void editAcc( int ); int main() { for(;;) menu(); // loops forever ... until exit(1) called in menu ... } // defaults to no ... bool more() { cout << "More (y/n) ? "; int reply = cin.get(); if(reply != '\n') flushCin(); return reply=='y' || reply=='Y'; } void flushCin() { while( cin.get() != '\n' ); } Info takeIn() { Info file; bool ok; do { cout << "Name : "; getline( cin, file.name ); cout << "Address : "; getline( cin, file.address ); cout << "City : "; getline( cin, file.city ); cout << "State : "; getline( cin, file.state ); cout << "Zip : "; getline( cin, file.zip ); cout << "Tele : "; getline( cin, file.tele ); cout << "Date : "; getline( cin, file.date ); for(;;) // forever loop ... until break { cout << "Balance : "; cin >> file.balance; if( !cin.good() ) { cout << "\nERROR! Entry NOT accepted! ... Re-enter numbers only!\n"; cin.clear(); flushCin(); continue; // from the top of this inner 'for loop' right now ... } // if reach here ... then a number was entered flushCin(); break; } // confirm ok ... cout<< "\nYou entered:" << endl; int w =45; cout<< left; cout<< setw(w) << "Customer's name is " << file.name << endl; cout<< setw(w) << "Customer's address is " << file.address << endl; cout<< setw(w) << "Customer's current city of residence is " << file.city << endl; cout<< setw(w) << "Customer's current state of residence is " << file.state << endl; cout<< setw(w) << "Customer's zip code is " << file.zip << endl; cout<< setw(w) << "Customer's current telephone number is " << file.tele << endl; cout<< setw(w) << "Customer's late date of payment is " << file.date << endl; cout<< setw(w) << "Customer's late recorded account balance is " << file.balance << endl; cout<< right; cout << "Ok ... (y/n) ? "; int reply = cin.get(); if(reply != '\n') flushCin(); ok = (reply=='y' || reply=='Y'); // defaults to no ... }while(!ok ); return file; } void menu() { cout << "\nWelcome to you bank company that has lost all your money.\n" << "Please choose an action from the menu displayed below.\n\n" << "Press 1 to enter a new account(s).\n" << "Press 2 to see ALL your current balances.\n" << "Press 3 to edit a current balance.\n" << "Press 4 to exit.\n\n" << "Your choice : " << flush; string temp; getline( cin, temp ); // convert temp to C string; atoi returns 0 if non-integer for first char's int choice = atoi( temp.c_str() ); switch(choice) { case 1: do { newAcc(); } while( more() ); break; case 2: cout << "All your current account balances are ..." << endl; showAll(); break; case 3: showAll(); cout << "\nWhich account to edit ? "; getline( cin, temp ); // convert temp to C string; atoi returns 0 if non-integer for first char's choice = atoi( temp.c_str() ); editAcc( choice-1 ); break; case 4: exit(1); default: cout << "\nNot a valid choice ..." << endl; } } void newAcc() { Info tmpFile = takeIn(); Bank.push_back( tmpFile ); } void showAll() { int w = 45; for(unsigned i = 0; i < Bank.size(); ++i ) { cout<< setw(4) << "Customer's number " << i+1 << endl; cout<< left; cout<< setw(w) << "Customer's name is " << Bank[i].name << endl; cout<< setw(w) << "Customer's address is " << Bank[i].address << endl; cout<< setw(w) << "Customer's current city of residence is " << Bank[i].city << endl; cout<< setw(w) << "Customer's current state of residence is " << Bank[i].state << endl; cout<< setw(w) << "Customer's zip code is " << Bank[i].zip << endl; cout<< setw(w) << "Customer's current telephone number is " << Bank[i].tele << endl; cout<< setw(w) << "Customer's late date of payment is " << Bank[i].date << endl; cout<< setw(w) << "Customer's late recorded account balance is " << Bank[i].balance << endl; cout<< right << "\nPress 'Enter' to continue ... "; flushCin(); // wait for Enter key press ... } } void editAcc( int i ) { if( i > int(Bank.size()) || i < 0 ) { cout << "\nNo Account with this number " << i+1 << endl; return; } // ok ... Account exists ... so ... cout << "\nEnter new data ...\n" << flush; Info tmp = takeIn(); // example of editing all the data ... Bank[i].name = tmp.name; Bank[i].address = tmp.address; Bank[i].city = tmp.city; Bank[i].state = tmp.state; Bank[i].zip = tmp.zip; Bank[i].tele = tmp.tele; Bank[i].date = tmp.date; Bank[i].balance = tmp.balance; }
This post has been edited by David W: 18 January 2009 - 04:09 AM
#10 David W
Reputation: 298
- Posts: 1,839
- Joined: 20-September 08
Re: Struct with arrays in functions
Posted 18 January 2009 - 04:29 PM
P.S.
One way to structure your Main Bank, where each client may have several accounts ...
would be to use a vector of vectors ... (or perhaps a linked list of vectors ...)
and just push_back (or append) each new 'mini-bank' vector that holds the accounts for one customer ... onto the company NEWBank vector (or list) of vectors of your struct's ...
But first ... get the program working as you want it to ... for just one customer. (and then you can go for the 2 or 3 or so ... customers modifications ... that may take a fair bit more work and a quite a bit more time.)
Shalom,
David
Write a Program That Uses a Structure to Store the Following Data About a Customer Account in C++
Source: https://www.dreamincode.net/forums/topic/80834-struct-with-arrays-in-functions/
0 Response to "Write a Program That Uses a Structure to Store the Following Data About a Customer Account in C++"
Postar um comentário