Hi;About, C4716 Error: Must return a value

hi i have a homework the question is

(Account Class) Create an Account class that a bank might use to represent customers’ bank accounts. Include a data member of type int to represent the account balance. [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)—called floating-point values— to represent dollar amounts.] Provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it’s greater than or equal to 0. If not, set the balance to 0 and display an error message indicating that the initial balance was invalid. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceeded account balance." Member function getBalance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account.





my header file is;;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdio>
#include <cstdlib>

 
using namespace std;
 
class Account
{
private:
  int balance;
public:
  Account (int);
  void setBalance(int);
  int getBalance();
  int debitBalance(int);
  int creditBalance(int);};





my main file is;;;

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
#include <iostream>
#include <cstdio>
#include <cstdlib>

 
using namespace std;
 
#include "Account.h"
 
Account::Account ( int initialBalance )
{
  if (initialBalance >= 0)
    setBalance ( initialBalance );
    cout << "Account balance is set with no problem" << endl;
 
  if (initialBalance < 0)
  {
     balance = 0;
     cout << "Account balance cannot be negative. Account amount set as 0" << endl;
   }
}
 
void Account::setBalance(int initialBalance)
{
  balance = initialBalance;
}
 
int Account::debitBalance( int debitAmount )
{
  if (debitAmount > balance)
     printf( "Your balance is not enough\n");    
  else
   { 
      balance = balance - debitAmount;
  }
 
}
 
int Account::creditBalance( int creditAmount )
{
  balance += creditAmount;
}
 
int Account::getBalance()
{
  return balance;
}





my test file is:;


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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include<math.h>
 
using namespace std;
 
#include "Account.h"
 
int main()
{
  Account accountA( 300 );
  Account accountB( -125 );
 
  int selection;
  int accountNumber;
  int debAmount;
  int credAmount;  
 
  cout << "Initial balance of accountA is " << accountA.getBalance() << endl;
  cout << "Initial balance of accountB is " << accountB.getBalance() << endl;
 
 
 
  cout << "\nPlease select the transaction (enter the number):" << endl;
  cout << "1. Balance Inquiry" << endl;
  cout << "2. Debit" << endl;
  cout << "3. Credit" << endl;
 
 cin>>selection;
 
  if (selection==1)
  {
    cout << "Balance Inquiry is selected" << endl;
      cout << "please select the account (enter the number): " << endl;
      cout << "1. accountA\n2. accountB" << endl;
 
       cin >> accountNumber;
       cout << "You selected Account: " << accountNumber << endl;
 
       if (accountNumber==1 ) 
      { 
             cout << "accountA balance: " << accountA.getBalance() << endl;
	   }
	   else if (accountNumber==2 ) 
           {  cout << "accountB balance: " << accountB.getBalance() << endl;
	   }
	   else {        
           cout << "incorrect option!" << endl;
	   }
		 
          }
   
    if (selection==2)
	{
		cout << "Debit is selected" << endl;
      cout << "please select the account (enter the number): " << endl;
      cout << "1. accountA\n2. accountB" << endl;   
 
       cin >> accountNumber;
       cout << "option selected: " << accountNumber << endl;
	}
      if( accountNumber==1 ) 
      { 
             cout << "accountA balance: " << accountA.getBalance() << endl;
             cout << "enter debit amount: " << endl;
             cin >> debAmount;
             cout << "entered amount is " << debAmount << endl;
             accountA.debitBalance(debAmount);
             cout << "valid balance is " << accountA.getBalance() << endl;
	  }
     
	  else  if( accountNumber==2 ) 
           {  cout << "accountB balance: " << accountB.getBalance() << endl;
             cout << "enter debit amount: " << endl;
             cin >> debAmount;
             cout << "entered amount is " << debAmount << endl;
             accountB.debitBalance(debAmount);
             cout << "valid balance is " << accountB.getBalance() << endl;
	  }
	  else        
          { 
			  cout << "incorrect option!" << endl;
	  }  
 
  if (selection==3)
  {
    cout << "Credit is selected" << endl;
      cout << "please select the account (enter the number): " << endl;
      cout << "1. accountA\n2. accountB" << endl;   
 
       cin >> accountNumber;
       cout << "option selected: " << accountNumber << endl;
  }
      if ( accountNumber==1 ) 
      {
             cout << "accountA balance: " << accountA.getBalance() << endl;
             cout << "enter debit amount: " << endl;
             cin >> credAmount;
             cout << "entered amount is " << credAmount << endl;
             accountA.creditBalance(credAmount);
             cout << "valid balance is " << accountA.getBalance() << endl;
	  }
         
	  else  if ( accountNumber==2 )  
	  { 
		  cout << "accountB balance: " << accountB.getBalance() << endl;
             cout << "enter debit amount: " << endl;
             cin >> credAmount;
             cout << "entered amount is " << credAmount << endl;
             accountB.creditBalance(credAmount);
             cout << "valid balance is " << accountB.getBalance() << endl;
	  }
	  else {        
           cout << "incorrect option!!!" << endl;
	  }
		 
	  if (selection!=1||selection!=3||selection!=2)
   {
      cout << "incorrect option!" << endl;
    }
 
  system("PAUSE");  
  return 0;
}





and my errors;
(37): error C4716: 'Account::debitBalance' : must return a value
(42): error C4716: 'Account::creditBalance' : must return a value
Last edited on
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int Account::debitBalance( int debitAmount )
{
  if (debitAmount > balance)
     printf( "Your balance is not enough\n");    
  else
   { 
      balance = balance - debitAmount;
  }
 
}
 
int Account::creditBalance( int creditAmount )
{
  balance += creditAmount;
}
These functions indicate that they return an int, but no return statement is present.
I fixed it but i have a problem with if statement could u help me with that?
What is your problem? You don't need to ask permission to ask for help ;)
you see on test file line 86 selection 3 does not working when i comple the file
What about it is not working? How do you know?
it says"run-time check failure#3-the variable "accountNumber" is being used without being initialized." but i did it 16. line
On line 16 you declare that it exists, but you do not give it any value.
Topic archived. No new replies allowed.