How to pass and return arrays in C++? -


this question has answer here:

i making simple bank account handling system , saving account information array have come across difficulties when comes passing account information, reading array text file program needs passed function reads file functions handle withdraws, deposits , viewing balance, array passed intended store current balance, stand in bool overdraft last 3 withdraws , deposits.

the withdraw function looks like

float withdraw()                                        //function handled withdraw requests { //variables const int m = 3;                                //declare const int withdraws const int n = 8;                                //declare const int account float withdrawamount = 0.0f;                    //used internam laths in function    float currentbalance = 0.0f;                    //used internally in function float newbalance = 0.0f;                        //passed write function float withdraws[m];                             //passed write function float account[n];                               //passed , returned read function //call readfile function readfile(account[n]);  cout << account[0]; //user interface cout << "withdraw opnened" << endl;                 //prompts user input of withdraw amount , displays current balance cout << "your current balance is: " << currentbalance << endl; cout << "how withdraw?" << endl; cin >> withdrawamount; newbalance = currentbalance - withdrawamount;           //calculates balance after withdraw withdraws[2] = withdraws[1]; withdraws[1] = withdraws[0]; withdraws[0] = withdrawamount; system("pause"); system("cls");  writefile(newbalance, withdraws[m]); menu(); return 0; } 

and function read file looks like

float readfile(float account[8]) { //variables const int n = 8; float accountread[n];  //read file ifstream file("floats.txt"); if (!file.is_open()) {     cerr << "error opening file" << endl;     return 0; } (int = 0; < n && file >> accountread[i]; ++i)     ; if (file) { } account = accountread; return account[n]; } 

any guidance appreciated have spent hours trying research have gotten nowhere

use double, not float. e.g. literal 3.14 of type double. that's because double default floating point type in c++, floating point type use matter of course when there no weighty reasons otherwise.

use std::vector , std::array, not raw arrays.

for example, can return std::vector or std::array function.

also, remember that

float readfile(float account[8]) 

is equivalent to

float readfile(float account[]) 

and to

float readfile(float* account) 

but don't problem std::vector , std::array.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -