This is my Code

#include<iostream>
using namespace std;
struct BankAccount
{
  int accountNum;
  double accountBal;
  double rate;
};
int main()
{
    const int ACCOUNT = 5;
    BankAccount info[ACCOUNT];
    int sub;
    double balTotal = 0;
    double balAverage;
    int accountNumber;
    bool isFound = false;
for(sub =0; sub<ACCOUNT ; ++sub)
{
    cout<<"Enter Account Number: ";
    cin>>info[sub].accountNum;
    cout<<"Enter Account Balance: ";
    cin>>info[sub].accountBal;
    cout<<"Enter  Interest rate: ";
    cin>>info[sub].rate;

}
for(sub=0; sub<ACCOUNT ; ++sub)
{
 balTotal +=info[sub].accountBal;
     ++sub;

 balAverage = balTotal/ACCOUNT;
}

for(sub=0; sub<ACCOUNT ; ++sub)
{

 cout <<"Account Number";
 cout<<info[sub].accountNum<<endl;
 cout <<"Account Balnce";
 cout<<info[sub].accountBal<<endl;
 cout<<"Account Rate";
 cout<<info[sub].rate<<endl;

}


 cout<<"Total Account Balance"<<balTotal<<endl;
 cout<<"Average for Account Balance"<<balAverage<<endl;

cout<<"Enter Account Number";
cin>>accountNumber;
 for(sub = 0; sub < ACCOUNT; ++ sub)
    if(accountNumber == info[sub].accountNum)
    {
    cout<<"Account Information :" <<endl;
    cout<<info[sub].accountNum<<endl;
    cout<<info[sub].accountBal<<endl;
    cout<<info[sub].rate<<endl;
    isFound = true;
   }  
if(isFound == false)
cout<<"invalid Account "<<endl;
 system("pause");
 return 0;
}

I want to be able to loop the invalid part of the program, they use can re-enter the Account number if is invalid , Help please

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please enclose your code with the proper tags

#include<iostream>
using namespace std;
struct BankAccount
{
int accountNum;
double accountBal;
double rate;
};
int main()
{
const int ACCOUNT = 5;
BankAccount info[ACCOUNT];
int sub;
double balTotal = 0;
double balAverage;
int accountNumber;
bool isFound = false;
for(sub =0; sub<ACCOUNT ; ++sub)
{
cout<<"Enter Account Number: ";
cin>>info[sub].accountNum;
cout<<"Enter Account Balance: ";
cin>>info[sub].accountBal;
cout<<"Enter Interest rate: ";
cin>>info[sub].rate;

}
for(sub=0; sub<ACCOUNT ; ++sub)
{
balTotal +=info[sub].accountBal;
++sub;

balAverage = balTotal/ACCOUNT;
}

for(sub=0; sub<ACCOUNT ; ++sub)
{

cout <<"Account Number";
cout<<info[sub].accountNum<<endl;
cout <<"Account Balnce";
cout<<info[sub].accountBal<<endl;
cout<<"Account Rate";
cout<<info[sub].rate<<endl;

}


cout<<"Total Account Balance"<<balTotal<<endl;
cout<<"Average for Account Balance"<<balAverage<<endl;

cout<<"Enter Account Number";
cin>>accountNumber;
for(sub = 0; sub < ACCOUNT; ++ sub)
if(accountNumber == info[sub].accountNum)
{
cout<<"Account Information :" <<endl;
cout<<info[sub].accountNum<<endl;
cout<<info[sub].accountBal<<endl;
cout<<info[sub].rate<<endl;
isFound = true;
}
if(isFound == false)
cout<<"invalid Account "<<endl;
system("pause");
return 0;
}

Edited 6 Years Ago by gerard4143: n/a

I assume you are talking about the last lines of your code. You can throw it in a while statement and get rid of your return and system statement.

while(true)
{
    cout<<"Enter Account Number";
    cin>>accountNumber;
    for(sub = 0; sub < ACCOUNT; ++ sub)
    {
        if(accountNumber == info[sub].accountNum)
        {
            cout<<"Account Information :" <<endl;
            cout<<info[sub].accountNum<<endl;
            cout<<info[sub].accountBal<<endl;
            cout<<info[sub].rate<<endl;
            isFound = true;
        } 
        if(isFound == false)
        cout<<"invalid Account "<<endl;
    }
}

This is just one way to do it.

Edited 6 Years Ago by LevyDee: n/a

This article has been dead for over six months. Start a new discussion instead.