why is Return 0 not leaving my method??

I have written this as a void and int method still returns me back to the top of the method, when i want to leave and end the program PLEASE Help!

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
  int checkingNum(checking numbers[], int accNum) {
	int i = 0; char input; double money = 0.0; int years = 0;
	 numbers[i].setAcct(accNum);
	
	cout << "Enter s (set balance)" << endl;//method for each letter
	cout << "Enter d (display account info)" << endl;
	cout << "Enter i(enter number of years for interest )" << endl;
	cout << "Enter q(quit processing this account)" << endl;
	cin >> input;
	while (input != 'q') {
		if (input == 's') {
			cin >> money;
			try {
				if (money < 700) {
					throw money;
				}
			}
			catch (double money) {
				cout << "Error balance must be greater than 700. Re Enter " << endl;
				cin >> money;
			
			}
			numbers[i].setBalance(money);
			
	if (input == 'q') {
		cout << "Account# : ";
		cin >> accNum;
		if (accNum == 0) {
			return 0;//takes me back to beginning of method..and i want to go to main WHY?? confused
		}
		//else 
			//pickAcctNum(accNum);

	}
	i++;
	
	
}
Last edited on
Line 29 is unreachable. It isn't taking you back to the beginning of the method.
cire why? can u tell me what i should do? i figured this would be the easy part of my program
You see line 10? A cursory inspection of the compound statement governed by the while condition tells us that input does not change anywhere inside the loop (which is odd for the sole variable in a loop condition, isn't it?) So, if the loop can only be entered if input is not equal to 'q' then the condition on line 25, which is inside the loop, cannot be true. And if that condition cannot be true, then line 29 cannot be reached.
Last edited on
okay i made a mistake cire, the issue is not that simple
imagine the if statement is outside the loop such as
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
int i = 0; char input; double money = 0.0; int years = 0;
	 numbers[i].setAcct(accNum);
	
	cout << "Enter s (set balance)" << endl;//method for each letter
	cout << "Enter d (display account info)" << endl;
	cout << "Enter i(enter number of years for interest )" << endl;
	cout << "Enter q(quit processing this account)" << endl;
	cin >> input;
	while (input != 'q') {
		if (input == 's') {
			cin >> money;
			try {
				if (money < 700) {
					throw money;
				}
			}
			catch (double money) {
				cout << "Error balance must be greater than 700. Re Enter " << endl;
				cin >> money;
			
			}
			numbers[i].setBalance(money);
			cout << "Enter s (set balance)" << endl;//method for each letter
			cout << "Enter d (display account info)" << endl;
			cout << "Enter i(enter number of years for interest )" << endl;
			cout << "Enter q(quit processing this account)" << endl;
			cin >> input;
		
	}
	
		cout << "Account# : ";
		cin >> accNum;
		if (accNum == 0) {
			return 0;//takes me back to beginning of method..and i want to go to main
		}
		//else 
			//pickAcctNum(accNum);
		i++;
		return 0;
	
	
Where is the closing brace for the while statement?
What makes you think it takes you back to the beginning of the method? If that line is reached control is returned to the calling function. If the calling function is main, it returns to main. If it was some other function or method, it will return to that function or method.
I made a mistake copy and pasting, i have this code as a better example :Why does it enter the while loop if i enter accNum as 0?
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
int pickAcctNum(int accNum) {
	checking numbers[5];
	savings access[5];
	int *point[max];
	int i = 0;

	
	if (accNum == 0) {
		return 0;//when accNum enters the method as 0 it skips this and goes into while loop
	}
	while (accNum != 0) {//once user hits 'q' then 0, goes inside while loop when it should not
		try {
			if (accNum == 101 || accNum == 102 || accNum == 103 || accNum == 100 || accNum == 104) {
				point[i] = &accNum;
				checkingNum(numbers, *point[i]);//call checking account method
				

			}
			if (accNum == 200 || accNum == 201 || accNum == 202 || accNum == 203 || accNum == 204) {
				point[i] = &accNum;
				savingsNum(access, *point[i]);//calls savings account method
				
			}
			else
				throw accNum;
		}//try block
		catch (int accnum) {
			cout << "Error enter 100-104 ,200-104 or 0 to quit Caught" << endl;//Why does it print this out?? if accNum enters the method as 0?
			return 0;
		}
		//cin >> accNum;
		
		i++;
	}//while block

}
Last edited on
:Why does it enter the while loop if i enter accNum as 0?

Why do you say "enter accNum as 0" when there is no entry here? The only way the loop is entered is if the accNum fed to the function is non-zero.
because there is a possibility 0 could be passed into the function as 0, if so i want it to return to the main, if not...follow the directions
As I mentioned in my previous post, the only way the loop is entered is if the accNum fed to the function is non-zero. It does return if accNum is 0.

It follows, then, that if the loop is being entered into, then the function is not being fed 0, so the perceived problem is probably at the point the function is being called, not within the function.
i see Thanks!
Topic archived. No new replies allowed.