called function not returning to main after execution

This is part of the complete program. We are writing classes and derivative classes. I have another function for a checking account and the included .h files with the related .cpp functions.

Everything is working except the end of the function. When the functions are called from the switch in the main instead of returning to the main,the functions loop to line 105 (beginning of the do while loop)even when 'q' or 'Q' is entered. What am I missing?

My intent is to run the main switch as a selection menu, choose savings, checking, perform deposits, withdrawals and balance inquiries then exit the function and allow the user to select the savings, checking as many time as desired and give the option to exit the program


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

void Savings();
void Checking();

int main()
{
	char choice;
	cout << "\nEnter S for savings:\t"<<endl;
	cout << "\nEnter C for Checing:\t"<<endl;
	cout << "\nEnter Q to exit:\t"<<endl;

	cin >> choice;
	while (choice != 'q'|| choice != 'Q')
	{
	do
	{
	switch (choice)
	{
	case 's':
	case 'S':
		Savings();
		break;
	case 'c':
	case 'C':
		 Checking();		
		break;
	case 'q':
	case 'Q':
		cin.get();
		cout << "\nAre you sure you want to exit?" << endl;
		{
			cin.get ();
			exit (0);
		}
	}
	}while (choice != 'Q'|| choice != 'q');

	system("pause");
	}	
}
	////////////////////////////////////////////////////////////////////////
	//Saving account Test;
	


void Savings(void)
{
	int i = 0;
	int newAccountNumber = 0;
	double depositAmount = 0.0;
	double withdrawAmount = 0.0;
	double newInterestRate = 0.0;
	double draw = 0, deposit = 0, savingsDraw = 0;
	char choice;
        CheckingAccount account1;
	SavingsAccount account2;

	srand(GetTickCount());     //Seed the random number generator

	
	cout << "Enter a five digit integer for your new savings account number:  ";
	cin >> newAccountNumber;
	account2.setAccountNumber(newAccountNumber);
	cout << endl;

	if (account2.getAccountNumber() == account1.getAccountNumber())
{
	while (account2.getAccountNumber() == account1.getAccountNumber())
	{
		cout << "Account number not available."<< endl;
		cout << "Enter a five digit integer for your new account number:  ";
		cin >> newAccountNumber;
		account2.setAccountNumber(newAccountNumber);
		cout << endl;
	}

}

	cout << "Retrieving new savings account number" << endl;
	cout << "New Savings Account Number:  " << account2.getAccountNumber() << endl << endl;
	cout << "Set savings account interest rate:\t";
	cin >> newInterestRate;
	account2.setInterestRate(newInterestRate);
	
	cout << showpoint << fixed << setprecision(2);

	account2.depositMoney(100.0);
	cout << "\nSavings account balance for account # " << account2.getAccountNumber() <<
			" is $" << account2.getAccountBalance() << endl << endl; // FIXED CODE
	
	


	do
	{
	
	cout << "What would you like to do today?"<< endl;
	cout << "Enter D for deposit, W for withdraw"<<
		"\nI for balance inquiry or Q to exit \t";
	cin >> choice;

	
		if (choice == 'd' || choice == 'D')
		{ 
		cout << "Enter amount of deposit\t"<< endl;
		cin >> deposit;
		account2.depositMoney(deposit);
		cout << "\nSavings account balance for account # " << account2.getAccountNumber() <<
			" is $" << account2.getAccountBalance() << endl << endl;
	}

	else if (choice == 'w' || choice =='W')
	{
		cout << "Enter amount of withdraw\t";
		cin >> savingsDraw;
		account2.withdrawMoney(savingsDraw);
		

		if(savingsDraw >= account2.getAccountBalance() )
		{
			cout << "\nWithdrawal denied, insufficient funds" << endl; 
		}
		cout << "\nSavings account balance for account # " << account2.getAccountNumber() <<
			" is $" << account2.getAccountBalance() << endl << endl;
	}
		else if (choice == 'i' || choice =='I')
	{
		cout << "\nSavings account balance for account # " << account2.getAccountNumber() <<
			" is $" << account2.getAccountBalance() << endl << endl;
		cout << "Total interest earned:  " << account2.getInterestEarned() << " over " 
			 << account2.getNumberOfDays()<< " days" << endl << endl;

	}
		else if(choice == 'q' || choice =='Q')
		{
			cout << "\nSavings account balance for account # " << account2.getAccountNumber() <<
			" is $" << account2.getAccountBalance() << endl << endl;
		cout << "Total interest earned:  " << account2.getInterestEarned() << " over " 
			<< account2.getNumberOfDays()<< " days" << endl << endl;
	}
	
	}while (choice != 'Q'|| choice != 'q');
	
	
}
I think the format is suppose to be like this.

If (stuff) {do things}
else if (stuff) {do things}
else if (stuff) {do things}
else (stuff) {do things}

Maybe if you made that last one just ELSE it would work?


Also not sure, are you trying to return a value from

 
void Savings(void)


to the main function?

If you are you may want to use something other than a void function.
Last edited on
}while (choice != 'Q'|| choice != 'q');

The truth table for logical or looks like the following:
true  || true  = true
true  || false = true
false || true  = true
false || false = false


So, when choice is 'q', choice != 'Q' is true and choice != 'q' is false. By the truth table above, true || false is true and the while loop continues executing.

When choice is 'Q', choice != 'Q' is false and choice != 'q' is true. By the truth table, false || true is true and the while loop continues executing.

The truth table for logical and looks like the following:
true  && true  = true
true  && false = false
false && true  = false
false && false = false


How would that work out in the while condition?
I have had the final else statement with and without the if with no change. I cannot exit the do/while loop. It will display
1
2
3
4
cout << "\nSavings account balance for account # " << account2.getAccountNumber() <<
			" is $" << account2.getAccountBalance() << endl << endl;
		cout << "Total interest earned:  " << account2.getInterestEarned() << " over " 
			<< account2.getNumberOfDays()<< " days" << endl << endl;


as desired. When 'Q' is entered instead of exiting the loop it will go to the beginning of the do/while loop.

1
2
3
4
cout << "What would you like to do today?"<< endl;
	cout << "Enter D for deposit, W for withdraw"<<
		"\nI for balance inquiry or Q to exit \t";
	cin >> choice;


I am not returning a value to the main.

Variables are in the defined classes.
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
#pragma once

class BankAccount

{
protected:
// Customer bank account number
int accountNumber;
//Customer bank account balance
double accountBalance;

public:
BankAccount(void);
~BankAccount(void);
// Accessor for customer account number
int getAccountNumber(void);
// Return customer account ballance
double getAccountBalance(void);
void depositMoney(double depositAmount);
// Deposit money into customer bank account
bool withdrawMoney(double withdrawAmount);
// Set customer bank account number
void setAccountNumber(int newAccountNumber);

};


This is my base class and I have 2 derivative classes for saving and checking accounts.

I also have implementation files for each of my classes.

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
#pragma once
#include "BankAccount.h"

BankAccount::BankAccount(void)
{
	accountNumber = 0;
	accountBalance = 0.0;
}

BankAccount::~BankAccount(void)
{
}

// accessor for customer account number
int BankAccount::getAccountNumber(void)
{
	return accountNumber;
}

//Return customer account balance
double BankAccount::getAccountBalance(void)
{
	return accountBalance;
}

//Deposit money into customer bank account
void BankAccount::depositMoney(double depositAmount)
{
	accountBalance += depositAmount;
}

//Withdraw money from customer bank account
bool BankAccount::withdrawMoney(double withdrawAmount)
{
	if(accountBalance - withdrawAmount < 0)
		return false;
	else
	{
		accountBalance -= withdrawAmount;
		return true;
	}
}

//Set customer bank account number
void BankAccount::setAccountNumber(int newAccountNumber)
{
	accountNumber = newAccountNumber;
}

Hope this explains it well enough.

Topic archived. No new replies allowed.