My apologies for bothering any of you all, but I have a looping issue with the following code. I'm using Visual C++ 2008, and the code compiles just fine, but in the program, it does not loop and display the requested information as it should. It re-loops and asks the user for an account number again, but it never displays the requested information. I haven't the faintest idea what I'm doing wrong. Here's the code:
#include "stdafx.h"
#include<iostream>
using namespace std;
struct Account
{
int acctNum;
double acctBalance;
double acctIntRate;
};
int main()
{
const int ACCT_NUM = 5;
Account acct[ACCT_NUM];
Account balance[ACCT_NUM];
Account interest[ACCT_NUM];
int start;
int isFound = 0;
int neededAcct;
double acctnumbers = 0;
double balancenumbers = 0;
double interestnumbers = 0;
double average = 0;
double total = 0;
cout << "Total of All Balances: " << total << endl;
cout << "Balance Average: " << average << endl;
cout << endl;
cout << endl;
cout << "Enter the account number of which you wish to see information: ";
cin >> neededAcct;
// Had trouble around this area - I am not sure if the loop is incorrect or what it is, but I'd like to
// find out what I did wrong. I'm sure it's simple, but the inclusion of the arrays sort of threw me off.
for (start = 0; start < ACCT_NUM; ++start)
{ if (neededAcct == acct[start].acctNum)
cout << "The balance is " << acct[start].acctBalance << " and the interest rate is " << acct[start].acctIntRate << endl;
isFound = 1;
cout << "Enter the account number of which you wish to see information: ";
cin >> neededAcct;
if (isFound == 0)
cout << "No such account number exists." << endl;
cout << "Enter the account number of which you wish to see information: ";
cin >> neededAcct;
}
return 0;
}
----------------------------------
Any help would be immensely appreciated on this. I've gone completely blank.
while(true)
{
cout << "Enter the account number of which you wish to see information ( -1 to quit): ";
cin >> neededAcct;
if (neededAcct == -1)
break;
isFound = 0;
for(start = 0; start < ACCT_NUM; ++start)
{
if (neededAcct == acct[start].acctNum)
{
cout << "The balance is " << acct[start].acctBalance << " and the interest rate is " << acct[start].acctIntRate << endl;
isFound = 1;
}
}
if (isFound == 0)
cout << "No such account number exists." << endl;
}
I think this is closer to what you want. You enter accout number, it checks for it, displays information if it is found, if it is not found it displays the no such account number exists message then it loops back and asks you to enter a new accout number and will continue to do so until you enter -1.