Small program I can't get to work


#include <iostream>
using namespace std; //introduces namespace std
class Account
{
private:
int acctNumber;
string acctName;
double acctBal;
public:
// 2 constructors
Account ()
{
acctNumber=0;
acctName=" ";
}
Account (int a,string ac,double acc)
{
acctNumber=a;
acctName = ac;
}
// sets
void setNum(int a)
{
acctNumber = a;
}

void setName(string ac)
{
acctName = ac;
}
// gets
int getNum()
{
return acctNumber;
}
string getBalance()
{
return acctName;
}
double getBal()
{
return acctBal;
}

};// end class

int main()
{
const int SIZE = 15;
Account bank[SIZE]= {Account (12345, "Smith", 1005.99),
Account (19822, "Lee", 20.21),
Account (18521, "Kim", 3061.77),
Account (85218, "Wall", 9855.33),
Account (85199, "Harry", 9077.88),
Account (91532, "Jess", 63112.99),
Account (43174, "Glee", 641.88),
Account (63173, "Corr", 841.55),
Account (23441, "Matt", 124.92),
Account (65185, "Card", 90.22),
Account (62188, "Kells", 529.77),
Account (32188, "Slyth", 863.22),
Account (90888, "Booth", 7422.88),
Account (52111, "Good", 321.99),
Account (87331, "Williams", 7317.21)};

cout <<"\tWelcome to Bank!";
cout <<"\n\tYou can make a deposit or withdraw ";
cout <<"\n\n\tPlease have your account number";
cout <<"\n\tor the name on the account ready\n";

// all of your code here
int choice;
cout << "Do you want to check account balance by name or account number?";
cout << endl;
cout << "1-Name";
cout << endl;
cout << "2-Account Number";
cout << endl;
cout << "3-Exit";
cout << endl;
cin >> choice;
string name = " ";
int accNum=0;
int acctBal=0;
double bal=0;

if (choice == 1)
{
cout << "Enter your name: ";
cin >> name;
for (int ie = 0; ie < SIZE; ie++)
{

bank[ie].setName(name);
}
}
if (choice == 2)
{
cout << "Enter your account #: ";
cin >> accNum;
for (int ief = 0; ief < SIZE; ief++)
{

bank[ief].setNum(accNum);
}
}

if (choice == 3)
{
cout <<"Have a nice day."<<endl;
}
while (choice <=0||choice >=4)
{
cout << "Wrong choice, please try again: ";
cin >> choice;
}

for(int i=0; i<SIZE; i++)
{
if(bank[i].getBal() == accNum || bank[i].getBalance() == name)
{
cout << bank[i].getBal()<<endl;
}
}

// give user a choice of searching by acct number, name or quit
// make sure withdraw amount is not greater than balance

cout <<"Good Bye";

return 0;
}

(What I'm trying to do)How would I send the parts of the array to the class so I can compare name/number to check if its right so i can print account balance? then ask the person if he wants to withdraw or add(subtract if user wants to withdraw/add if the user wants to add)

--
But, I am stuck here:
I can't get the Number part to search and work. But the name does something wierd, it displays numbers as such: 1.79647e31304etc...
Last edited on
1
2
3
4
5
Account (int a,string ac,double acc)
{
acctNumber=a;
acctName = ac;
}


You are not using "acc" to set the balance for each account
I added the "acc" in, sadly it changed nothing.
Ok.

1
2
3
4
5
6
7
if (choice == 1) {
 cout << "Enter your name: ";
 cin >> name;
 for (int ie = 0; ie < SIZE; ie++) {
  bank[ie].setName(name);
 }
}


This code sets the name of every account to the name your enter. Why?
You also have the same code to set the account number to the number you enter. Why?

 
if(bank[i].getBal() == accNum || bank[i].getBalance() == name)


You are comparing accNum to the Balance, and the Balance to the Name?

 
if(bank[i].getAccNum() == accNum || bank[i].getName() == name)


But then you need to add a getName() function.

That will get you something printing out.
What I'm doing is trying to search the entire array through the class for balance according to the name/number the user enters. Say the user enters a number/name that isn't one of the numbers/names in the bank array, then it would display an error. If the person enters a name/number thats in the array, it would print the name the number and the balance.

Example: if i entered: Smith
it should print:
Account Name: Smith
Account Number: 12345
Account Balance: 1005.99

or
if i entered: 12345
it should print:
Account Name: Smith
Account Number: 12345
Account Balance: 1005.99

In case, this is how it's ordered.
(23441, "Matt", 124.92)
(account number, name,balance)
Last edited on
Ok. Then you shouldn't have
1
2
3
4
5
6
7
if (choice == 1) {
 cout << "Enter your name: ";
 cin >> name;
 for (int ie = 0; ie < SIZE; ie++) {
  bank[ie].setName(name);
 }
}

Or the same for the balance area.

 
if(bank[i].getAccNum() == accNum || bank[i].getName() == name)

Will give you a match based on AccNum or Name. This should be sufficient. Once you have a match then you can print out the details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130


#include <iostream>
using namespace std; //introduces namespace std
class Account
{
	private:
		int acctNumber;
		string acctName;
		double acctBal;
	public:
		// 2 constructors
		Account ()
        {
		acctNumber=0;
        acctName=" ";
        acctBal=0;
		}
	    Account (int a,string ac,double acc)
        {
		acctNumber=a;
		acctName = ac;
		}
		// sets
		void setNum(int a)
		{
        acctNumber = a;
        }

        void setName(string ac)
        {
        acctName = ac;
        }
		// gets
		int getAccNum()
		{
            return acctNumber;
        }
        string getName()
		{
               return acctName;
        }
        double getBal()
        {
               return acctBal;
        }

};// end class

int main()
{
	const int SIZE = 15;
	Account bank[SIZE]= {Account (12345, "Smith", 1005.99),
						 Account (19822, "Lee", 20.21),
						 Account (18521, "Kim", 3061.77),
						 Account (85218, "Wall", 9855.33),
						 Account (85199, "Harry", 9077.88),
						 Account (91532, "Jess", 63112.99),
						 Account (43174, "Glee", 641.88),
						 Account (63173, "Corr", 841.55),
						 Account (23441, "Matt", 124.92),
						 Account (65185, "Card", 90.22),
						 Account (62188, "Kells", 529.77),
						 Account (32188, "Slyth", 863.22),
						 Account (90888, "Booth", 7422.88),
						 Account (52111, "Good", 321.99),
						 Account (87331, "Williams", 7317.21)};

cout <<"\tWelcome to  Bank!";
cout <<"\n\tYou can make a deposit or withdraw ";
cout <<"\n\n\tPlease have your account number";
cout <<"\n\tor the name on the account ready\n";

// all of your code here
int choice;
cout << "Do you want to check account balance by name or account number?";
cout << endl;
cout << "1-Name";
cout << endl;
cout << "2-Account Number";
cout << endl;
cout << "3-Exit";
cout << endl;
cin >> choice;
string name = " ";
int accNum=0;
int acctBal=0;
double bal=0;

if (choice == 1)
			{
			cout << "Enter your name: ";
			cin >> name;

			}
if (choice == 2)
			{
			cout << "Enter your account #: ";
            cin >> accNum;

			}

if (choice == 3)
			{
			cout <<"Have a nice day."<<endl;
			}

while (choice <=0||choice >=4)
{
	cout << "Wrong choice, please try again: ";
	cin >> choice;
}

for(int i=0; i<SIZE; i++)
{
if(bank[i].getAccNum() == accNum || bank[i].getName() == name)
    {
     cout<<"Account Number: " << bank[i].getAccNum()<<endl;
     cout<<"Account Name: " <<bank[i].getName()<<endl;
     cout<<"Account Balance: "<<bank[i].getBal()<<endl;
    }
}

// give user a choice of searching by acct number, name or quit
// make sure withdraw amount is not greater than balance

cout <<"Good Bye";

	return 0;
}


Everything prints correctly except the balance(it gives me "trash")
Last edited on
1
2
3
4
5
Account (int a,string ac,double acc)
{
 acctNumber=a;
 acctName = ac;
}


You have to assign the balance with acc.
Then it will work.
whoa, i did not notice that, i apologize.

i thought i added it in.
Last edited on
It all works now?
Yes, thank you very much!
No worries :)
Topic archived. No new replies allowed.