Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys i'm working on a bank system project and every thing is working well
except when i try to print the values i entered i get wierd strings and random numbers ,, here's the code :
C++
#include <iostream>
using namespace std;

struct Address{
	char stName[10];
	int houseNum;
};

struct Account{
	char name[10];
	int accnumber;
	double balance;
	Address addr;
};
// ------------------------------------------------------------------------------------------------- //
void getData( Account accounts[], int size , int *counter);


// ------------------------------------------------------------------------------------------------ //
int main(){
int choice;
int i = 0;
int size = 10;
Account a[size];
while (true){

	cout << "\t\t\t --- Enter your choise --- " << endl;
	cout << endl;
	cout << "\t\t\t 1 - Create Account\n";
	cout << endl;
	cout <<"\t\t\t 2 - Deposit Money" <<endl;
	cout << endl;
	cout << "\t\t\t 3 - Withdraw Money" <<endl;
	cout << endl;
	cout << "\t\t\t 4 - Display" << endl;
	cout << endl;
	cout << "\t\t\t 5 - Exit" << endl;
	cout << endl;
	cout << "\t\t\t choice is :  ";
	cin >> choice;
	if (choice == 1)
	getData(a , size ,&i);
	
	// printnig the value -> just checking
	cout << endl << endl;
	cout << "Name: " << a[i].name << endl;
	cout << "Account number : " << a[i].accnumber << endl;
	cout << "Balance: " << a[i].balance << endl;
	cout << "House num: " << a[i].addr.houseNum << endl ;
	cout << "Street name: " << a[i].addr.stName << endl;
	
}
	
}

void getData(Account accounts[], int size,int *counter ){
	int i = *counter;
	
	cout << "Name: ";
	cin >> accounts[i].name;
	
	cout << "acc number : " ;
	cin >> accounts[i].accnumber;
	cout << "balance : " ;
	cin >> accounts[i].balance;
	cout << "Address info " << endl;
	cout << "House number : " ;
	cin >> accounts[i].addr.houseNum;
	cout << "Street name : ";
	cin >> accounts[i].addr.stName ;
	i++;
	*counter = i;
	cout << endl;
	cout << "Done ...." << endl;
	system("pause");
}


What I have tried:

i have no clue ,, what is causing it to behave like that !!
Posted
Updated 17-May-22 20:27pm
v2
Comments
Richard MacCutchan 27-Nov-16 3:07am    
Do not use double types for financial amounts, as you will get problems. Use integer types (convert values to all cents, pence, paisa etc.).

1 solution

Your problem is i++... You are storing the values at position i, but increments it and prints the values at position i++...
 
Share this answer
 
Comments
Member 12173667 26-Nov-16 14:28pm    
that is correct ,, fixed it
thank you very much man :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900