Loop won't break or return

Sep 23, 2016 at 11:21pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  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 
				else if (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;
		}
}
Sep 23, 2016 at 11:56pm
Try adding the condition for that in the while loop on line 14.
Sep 24, 2016 at 12:11am
So, I should remove the condition from line 16 and add it to line 14? Also, I tried changing return to break and nothing happened as well
Sep 24, 2016 at 1:05am
Nevermind, it was an oversight on my part. The error was actually in my main function.
Topic archived. No new replies allowed.