QUESTION: Suppose we wanted to validate the data captured for a customer that has two bank accounts in a while loop. The
variable names for the bank account numbers are bankAccount1 and bankAccount2. The variable names for
the balances of each account are balance1 and balance2. A customer cannot have negative balances for each
of the accounts. Additionally bankAccount1 and bankAccount2 cannot hold the same bank account number.
Write down a correct condition for the while loop that validates the bank account numbers and the balances of
each account.
I answered this question with this code,please help me restructure it so it usks me to re-enter the account number is zero is entered.
#include<iostream>
usingnamespace std;
int main ()
{
//declaring variables
int bankAccount1, bankAccount2;
float balance1, balance2;
float total;
cout << "Please enter your first account number:";
cin >> bankAccount1;
cout << "Enter the balance on that account: ";
cin >> balance1;
cout << "Enter your second account number: ";
cin >> bankAccount2;
cout <<"Enter the balance on this account: ";
cin >> balance2;
while (bankAccount1 == bankAccount2)
{
cout << "The account numbers cannont be the same."<<endl;
cout << "Enter the second account number again";
cin >> bankAccount2;
}
while((balance1 <=0.0) || (balance2 <= 0.0))
{
cout << "The balace on an account cannont be 0 or below."<<endl;
cout <<"Re-enter the First accounts balance: ";
cin >> balance1;
cout <<"Re-enter the First accounts balance: ";
cin >> balance2;
total = balance1 + balance2;
}
cout <<endl;
cout <<"=Bank Statements+================================"<<endl;
cout << "================================================="<<endl;
cout << "== Acount Number " << bankAccount1 << " has R ";
cout <<balance1 <<"\t =="<<endl;
cout << "== Acount Number " << bankAccount2 << " has R ";
cout <<balance2 <<endl;
cout << "Your total in the bank is : R" << total <<endl;
return 0;
}
We haven't done do whiles yet. I was thinking of nesting the while with if statements.
I'm just not sure how to go about it, maybe i could change this to an if statement. After the first balance is entered it checks whether its zero and if it is it request the user re-enter it. My worry is that the if statement wont repeat until the right answer is given.
Please help me!
1 2 3 4 5 6 7 8 9
while((balance1 <=0.0) || (balance2 <= 0.0))
{
cout << "The balace on an account cannont be 0 or below."<<endl;
cout <<"Re-enter the First accounts balance: ";
cin >> balance1;
cout <<"Re-enter the First accounts balance: ";
cin >> balance2;
total = balance1 + balance2;
i would write something like this:
1 2 3 4 5 6
if (balance1== 0.0)
{
cout << "The balace on an account cannont be 0 or below."<<endl;
cout <<"Re-enter the First accounts balance: ";
cin >> balance1;
It is basically the same as the while loop but with the condition at the end rather than the beginning. This simply means that one loop is always completed, regardless of the loop condition.
Example:
1 2 3 4 5
char input;
do {
std::cin >> input;
// etc
} while (input != 'q');
EDIT: A more relevant example:
1 2 3 4 5 6
unsignedint day_of_month;
do {
std::cout << "Please enter a valid day of the month: " << std::endl;
std::cin >> day_of_month;
std::cout << std::endl;
} while (number > 31); // flow only leaves the loop once a suitable value has been found
cout << "Please enter your first account number:";
cin >> bankAccount1;
cout << "Enter the balance on that account: ";
cin >> balance1;
//check that the balance is not less than or equal to 0
while (balance1 <= 0)
{
cout << "The balace on an account cannont be 0 or below."<<endl;
cout <<"Re-enter the accounts balance: ";
cin >> balance1;
}
// request and read the second account number
cout << "Enter your second account number: ";
cin >> bankAccount2;
while (bankAccount1 == bankAccount2)
{
cout << "The account numbers cannont be the same."<<endl;
cout << "Enter the second account number again";
cin >> bankAccount2;
}
//request and second account number
cout <<"Enter the balance on the account: ";
cin >> balance2;
//check that the balance is not less than or equal to 0
while (balance2 <= 0)
{
cout << "The balace on an account cannont be 0 or below."<<endl;
cout <<"Re-enter the accounts balance: ";
cin >> balance2;
}
The code for balance1 and balance2 is the same, if you studied functions you can put lines 3-12 in a different function and call it for the two balances.
Otherwise looks fine to me.