c++ - Array of structs prints zeros when called, but displays correct input when not called (short code) -


the purpose of program bank account interface, user can create 4 accounts , can transfer funds between them. opted array of structs handle “bank account” info, , switch handle user options.

problem:

the account creation function, called in case ‘a’, appears create accounts intended, , displays them properly. however, results in other cases not designed.

case ‘b’ display inputted array information, only if nothing called (including function displays array information), otherwise when display() called prints zeros (what initialized before account creation).

case ‘c’ display inputted array information if nothing called, if function displays date/time called. otherwise prints zeros.

question:

why array of structs displaying zeros when call display function, display user input when it’s not called, long nothing else (except time function in case ‘c’), , can fix it?

notes:

i converted account creation function return pointer struct hoping might help, didn’t seem change anything. converted switch statement series of if’s see if change anything, did not (reversed switch). case ‘d’ , case default work intended. program not complete (my transfer() isn’t written). i’m aware “using namespace std;” nono.

i appreciate time , advice offered.

#include <iostream> #include <ctime> const int size = 4; using namespace std;  struct bank  //bank account structure hold account numbers , balances {      int num; // number of account      int checkdigit; // ending account number checkdigit appended account number      float bal; // balance of account };  void menu(); //menuprompt  bank* input(bank[]); // struct account input void display(bank[]); // struct account display void initialize(bank[]); // struct account initialization  char transferprompt(bank s[]); // transfer menu  int createaccounts(); //create account numbers , balances void time();    //displays current date, month, year, , time. void clearscreen(); //prints 40lines clear screen  int main() {     bank accounts[size]; // array of bank-account structs     initialize(accounts); // removes garbage values     char select; //user input variable     bank *ptr;     ptr = &accounts[0];     menu(); //runs menuprompt     cin >> select; //grabs user input     select = static_cast<char>( tolower( select ) ); //ensures user input lowercase (for switch statement)     clearscreen();      switch ( select )     {         case 'a': //creates accounts         input(accounts);         clearscreen();         main();         display(accounts);         break;          case 'b': //transfers between accounts         //display(accounts);         transferprompt(accounts);         //main();         break;          case 'c': //displays day, month, year, time, + account information.         time();         break;          case 'd': //exits program         break;          default: //invalid input failsafe; restarts menu prompt.         cout<<"invalid selection. restarting...\n";         main();         break;     } } char transferprompt(bank s[]) { } bank* input(bank s[]) // creates bank accounts {         bank accounts[size]; // array of bank-account structs         bank *ptr;           // pointer struct         ptr = &accounts[0];  // points pointer array of bank-account structs     cout << "account creation selected; create " << size << " accounts..." << endl;     (int = 0; < size ; i++)     {         cout << "enter account number: ";         cin >> s[i].num;         s[i].checkdigit = s[i].num % 5;         cout << "enter account balance: ";         cin >> s[i].bal;     }     return ptr; } void display(bank s[]) //displays bank accounts {     (int = 0; < 4; i++)     {         cout << "account: " << s[i].num << s[i].checkdigit;         cout << " has balance: $" << s[i].bal << endl;     }  } void initialize(bank s[]) // removes garbage values { (int = 0; < 4; i++)     {         s[i].num = 0;         s[i].checkdigit = 0;         s[i].bal = 0;     } } void clearscreen() // clears screen {     cout << string(40, '\n'); } void time() // display time function {     time_t t = time(null);     tm* ptr = localtime(&t); // ptr = pointer compute time     cout << endl;     cout << endl;     cout << "date:  " << (ptr->tm_mon)+1 <<"/"<< (ptr->tm_mday)<< "/" << (ptr->tm_year)+1900 << endl;     cout << "time:  " << (ptr->tm_hour) <<":"<< (ptr->tm_min)<< ":" << (ptr->tm_sec) << endl;  } void menu() // menu prompt {     cout << "parkville bank client program; edit accounts:\n";     cout << "a. create " <<size<<" accounts\n";     cout << "b. transfer money 1 account another\n";     cout << "c. display account balances\n";     cout << "d. quit program\n\n"; } 

this function not correct:

bank* input(bank s[]) // creates bank accounts {    bank accounts[size]; // array of bank-account structs    bank *ptr;           // pointer struct    ptr = &accounts[0];  // points pointer array of bank-account structs    // ... lines removed ...    return ptr;  // <-- undefined behavior } 

you returning pointer local variable / array. returning pointers or references local variables undefined behavior. reason why since array local, once function returns, doesn't logically exist anymore. you're pointing non-existing entity.

what wanted this:

bank* input(bank* s) // creates bank accounts {     cout << "account creation selected; create " << size << " accounts..." << endl;     (int = 0; < size ; i++)     {         cout << "enter account number: ";         cin >> s[i].num;         s[i].checkdigit = s[i].num % 5;         cout << "enter account balance: ";         cin >> s[i].bal;     }     return &s[0]; } 

since s passed in, meant pass address of first element of entity instead. note this:

bank* input(bank s[]) // creates bank accounts 

is no different this:

bank* input(bank* s) // creates bank accounts 

as arrays decay pointers.

i recommend use containers such std::vector instead of raw arrays , global constants keep track of number of entities (for example, size).


another issue program you're recursively calling main. don't this, not legal c++. instead, use proper looping constructs such while() , do-while().


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -