Creating a Loop for Arrays

Problem:

I am using a method to pull account ids from an account.txt file.
I'm able to get the code to work as long as the input is valid (i.e. the person types in an ID that is in the txt file).

I cannot seem to find a way to create a nested loop (I'm assuming that's what I need to do) in order to search the array to make sure the ID exists, if it does, then continue on to the next section. If it does not then I need to ask the user again for the ID, so that it can check the array again.


CODE:

void buyStock(Account account[], int size)
{
string tempMatchAccount, tempMatchStockName, tempMatchStockSymbol;
getAccountList(account, 5);
getStockList(stocks, 7);
int AcctPosition, stockNamePosition, stockChoice, stockSymbolPosition, sharesAmount;
double stockCost;

cout << "Stock Purchases: \n"
<< "---------------------------------\n"
<< "Enter a valid account ID: ";
cin >> tempMatchAccount;

for(int i = 0; i < 5; i++)
{
if(account[i].id.compare(tempMatchAccount) == 0)
AcctPosition = i;


}

cout << "Welcome, " << account[AcctPosition].id << ".\n"
<< "Select an option:\n(1) Purchase stock through a name\n(2) Purchase stock through a symbol\n: ";
cin >> stockChoice;

while (stockChoice != 1 && stockChoice != 2)
{
cout << "That is an invalid choice. Please select (1) or (2): ";
cin >> stockChoice;
}

switch (stockChoice) // Does not require a default case because a while loop checks for inputs outside the switch cases.
{
case 1:
cout << "Enter the name of the stock: ";
cin >> tempMatchStockName;

for(int j = 0; j < 7; j++)
{
if(stocks[j].name.compare(tempMatchStockName) == 0)
stockNamePosition = j;
}

cout << "Enter how many shares you wish to purchase: ";
cin >> sharesAmount;
stockCost = (stocks[stockNamePosition].price * sharesAmount);
while ( stockCost > account[AcctPosition].balance && sharesAmount < 0)
{
cout << "Error: Shares must be greater than zero and the cost must not exceed the account balance.\n"
<< "Enter the amount of shares you with to purchase: ";
cin >> sharesAmount;
}

account[AcctPosition].balance = (account[AcctPosition].balance - stockCost);
cout << "Transaction has been processed.";
printTransaction(account[AcctPosition].id, "BUY", stocks[stockNamePosition].symbol, stocks[stockNamePosition].price, sharesAmount);


break;

case 2:
cout << "Enter the stock symbol: ";
cin >> tempMatchStockSymbol;

for(int h = 0; h < 7; h++)
{
if(stocks[h].symbol.compare(tempMatchStockSymbol) == 0)
stockSymbolPosition = h;
}

cout << "Enter how many shares you wish to purchase: ";
cin >> sharesAmount;
stockCost = (stocks[stockSymbolPosition].price * sharesAmount);
while ( stockCost > account[AcctPosition].balance && sharesAmount < 0)
{
cout << "Error: Shares must be greater than zero and the cost must not exceed the account balance.\n"
<< "Enter the amount of shares you with to purchase: ";
cin >> sharesAmount;
}

account[AcctPosition].balance = (account[AcctPosition].balance - stockCost);
cout << "Transaction has been processed.";
printTransaction(account[AcctPosition].id, "BUY", stocks[stockSymbolPosition].symbol, stocks[stockSymbolPosition].price, sharesAmount);

break;

}
}
Last edited on
Topic archived. No new replies allowed.