I have function that should keep looping until "Return" is entered. Whenever I enter return, the program ignores it. If I type it once more, it goes to the else statement. And if I type Return a third time, it ends the program. I just want it to return to main() and I just can't seem to do it.
void select_stocks_buy(char symbol[][SYMB_LEN], char name[][NAME_LEN], double price[], double &bal, int num_held[])
{
int beginning;
int end;
int middle;
int location;
int found;
int num;
double cost;
char temp_name[SYMB_LEN];
string getout = "Return";
cout << "Enter the Ticker Symbol or enter Return to go back to menu: ";
while (cin.getline(temp_name, SYMB_LEN))
{
if (temp_name == getout)return;
beginning = 0;
end = 9;
found = FALSE;
// stay in loop until search is exhausted
while (end >= beginning && !found)
{
middle = (beginning + end) / 2; // divide list in half
if (strcmp(temp_name, symbol[middle]) < 0) // alphabetically less
end = middle - 1; // throw out half the list
elseif (strcmp(temp_name, symbol[middle]) > 0) // alphabetically greater
beginning = middle + 1; // throw out half the list
else
found = TRUE;
}
if (found == TRUE)
{
location = middle; // This is the location.
cout << "How many stocks do you want to buy?";
cin >> num;
cost = num * price[location];
//Makes sure that the cost will not exceeed the balance.
while (cost > bal)
{
cout << "The cost exceeds your balance. Please try again." << endl;
cout << "How many stocks do you want to buy?";
cin >> num;
}
bal = bal - cost;
num_held[location] = num;
cout << "You have bought " << num << " stocks for $" << cost << endl;
cout << "Your current balance is $" << bal << endl;
cin.ignore();
}
else
{
location = 10;
cout << "Ticker Symbol not found." << endl;
}
cout << "Enter a Ticker Symbol to search for or enter Return to go back to menu:" << endl;
}
}