Input validation in Class

I know, I've asked a lot in this forum, I apologize for that, but I'm really stumped with this program.
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
if (choice == 1)
{
cout << "Enter your account name: ";
cin >> name;
for(i=0; i<15; i++)
{
if(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;
cout << endl;
}
}
cout << "Do you want to withdraw or deposit?"<<endl;
cout << "1=Deposit"<<endl;
cout << "2=Withdraw"<<endl;
cin >> choice2;
if (choice2==1)
{
cout << "How much would you like to deposit?: ";
cin >> depo;
for (i=0;i<15;i++)
{
if(bank[i].getName() == name)
cout << "You have "<<bank[i].getBal()+depo<<" as a balance now."<<endl;
}
}
}
if (choice2==2)
{
cout << "How much would you like to withdraw?";
cin >> with;
for (i=0;i<15;i++)
{
if(bank[i].getName() == name)
{
while (with > bank[i].getBal())
{
cout << "You cannot withdraw that much."<<endl;
cout << "Try again: ";
cin >> with;
}
cout << "You have "<<bank[i].getBal()-with<<" as a balance now."<<endl;
}
}
}
}


I'm having trouble telling the person if the account he/she entered does not exist.

I've tried this, but it displays the message no matter what.

1
2
3
4
5
6
7
8
9
if(bank[i].getAccNum() != accNum)
																			{
cout << "That account does not exist."
																			}

if(bank[i].getName() != name)
{
cout << "That account does not exist."
}

They work, but they always print. I only want them to print when the account can not be found.
Are you doing it in a loop and bailing on the first error? Or are you checking every item in bank[ i ] before declaring that you haven't found a match?

1
2
3
4
5
6
7
8
9
10
int find_account( int accNum )
  {
  // I'm assuming that bank[] is a vector of accounts.
  for (int i = 0; i < bank.size(); i++)
    if (bank[i].getAccNum() == accNum)
      return n;
  // If we get this far, then we've traversed the whole vector
  // without finding the desired account, so it doesn't exist.
  return -1;
  }


Hope this helps.
Last edited on
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#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;
		acctBal = acc;
		}
		// sets
		void setNum(int a)
		{
        acctNumber = a;
        }

        void setName(string ac)
        {
        acctName = ac;
        }

        void setBal(double acc)
        {

        acctBal = acc;
        }

		// gets
		int getAccNum()
		{
            return acctNumber;
        }
        string getName()
		{
               return acctName;
        }
        double getBal()
        {


               return acctBal;
        }




};// end class

int main()
{
int choice;
string name = " ";
int accNum=0;
int acctBal=0;
double bal=0;
int choice2;
int choice3;
float depo;
float with;
int i =0;
	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

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;

if (choice == 1)
{
cout << "Enter your account name: ";
cin >> name;
for(i=0; i<15; i++)
{
if(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;
cout << endl;
}
}

cout << "Do you want to withdraw or deposit?"<<endl;
cout << "1=Deposit"<<endl;
cout << "2=Withdraw"<<endl;
cin >> choice2;
if (choice2==1)
{
cout << "How much would you like to deposit?: ";
cin >> depo;
for (i=0;i<15;i++)
{
if(bank[i].getName() == name)
{
cout << "You have "<<bank[i].getBal()+depo<<" as a balance now."<<endl;
}
}

}
if (choice2==2)
{
cout << "How much would you like to withdraw?";
cin >> with;
for (i=0;i<15;i++)
{
if(bank[i].getName() == name)
{
cout << "You have "<<bank[i].getBal()-with<<" as a balance now."<<endl;
}
}
}

}

if (choice == 2)
{
cout << "Enter your account number: ";
cin >> accNum;
for(i=0; i<15; i++)
{
if(bank[i].getAccNum() == accNum)
{
cout<<"Account Number: " << bank[i].getAccNum()<<endl;
cout<<"Account Name: " <<bank[i].getName()<<endl;
cout<<"Account Balance: "<<bank[i].getBal()<<endl;
cout << endl;
}
}
{
cout << "Do you want to withdraw or deposit?"<<endl;
cout << "1=Deposit"<<endl;
cout << "2=Withdraw"<<endl;
cout << "3=Exit"<<endl;
cin >> choice2;
if (choice2==1)
{
cout << "How much would you like to deposit?: ";
cin >> depo;
for (i=0;i<15;i++)
{
if(bank[i].getAccNum() == accNum)
{
cout << "You have "<<bank[i].getBal()+depo<<" as a balance now."<<endl;
}
}

}
if (choice2==2)
{
cout << "How much would you like to withdraw?";
cin >> with;
for (i=0;i<15;i++)
{
if(bank[i].getAccNum() == accNum)
{
cout << "You have "<<bank[i].getBal()<<" as a balance now."<<endl;
}
}
}
}
}
cout <<"Good Bye";
return 0;
}

Here is the actual code, I removed some of it(due to being a repeat).
Last edited on
Easiest way to do it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool bMatch = false;
for(i=0; i<15; i++)
{
	if(bank[i].getAccNum() == accNum)
       {
	     cout<<"Account Number: " << bank[i].getAccNum()<<endl;
	     cout<<"Account Name: " <<bank[i].getName()<<endl;
	     cout<<"Account Balance: "<<bank[i].getBal()<<endl;
	    cout << endl;
            bMatch = true;
       }
}

if (!bMatch) {
 cout << "Account does not exist" << endl;
 return;
}


Definitely not the best, or most efficient way. But it's simple :) That should help you out.

Z.
Wow, thanks.

I have one more question(sorry :( )

How would I not allow the person to have negative balance or withdraw more than he/she has using the class?

//EXAMPLE

Enter your account name: Smith
Account Number: 12345
Account Name: Smith
Account Balance: 1005.99

Do you want to withdraw or deposit?
1=Deposit
2=Withdraw
Choice: 1
How much would you like to deposit?
Amount: -9000
(What I want it to display)You can not deposit that much.
(But if I enter)
Amount: -900
You have 105.99 as a balance now.

Do you want to withdraw or deposit?
1=Deposit
2=Withdraw
Choice: 2
How much would you like to deposit?
Amount: 9000
(What I want it to display)You can not withdraw that much.
(But if I enter)
Amount: 900
You have 105.99 as a balance now.
//END EXAMPLE

I've tried sending it to the setBal/getBal function, but instead I got an error and I have no idea how to fix. it
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (choice2==2) {
do {
 cout << "How much would you like to withdraw?";
 cin >> with;
} while (with <= 0); // Stop Negatives

for (i=0;i<15;i++) {
 if(bank[i].getAccNum() == accNum) {
  if (bank[i].getBal() > with) {
   cout << "Cannot Withdrawal more than you have" << endl;
  } else {
   // Do Withdrawal
  }
  cout << "You have "<<bank[i].getBal()<<" as a balance now."<<endl;
 }
}
}



Last edited on
Topic archived. No new replies allowed.