Can someone correct my code? I am receiving a few errors and I cannot think anym
ID: 3917377 • Letter: C
Question
Can someone correct my code? I am receiving a few errors and I cannot think anymore.. I need helping reveiwing my main checking.cpp program and the accompaning checking.h files
here is the checking.cpp
#include "checking.h"
#include <iostream>
// header file includede for input output stream
#include <iomanip>
using namespace std; //namespace to differentiate same names of classes,functions,etc
int main ()
{
checking();
// way to define constructor checking() of checking class,to initialise default values to data members
{
Name = "unassigned";
// default values initialised
Type = 'D';
AcctNum = 0;
CurrBalance = 0;
OverDraftBalance = 0;
OverDraftLimit = 0;
}
void checking::input() // void input() function of checking class is defined here with its body
{
char t;
double amt;
cout << "Enter name: ";
//cout is used to display to console
cin >> Name; //cin is used to take input from console typed by user
cout << "Account type (D/S/P): ";
cin >> t;
if(t == 'D' || t == 'S' || t == 'P') // == is relational operator used to check whether t is equal to 'D' , 'S' , 'P' types or not
// when condition inside if braces () is true ,if block executes otherwise else
// pipe symbol || is OR operator. gives false when both conditions are false, otherwise true
Type = t;
else
{
cout << "Invalid account type! " << endl;
}
cout << "Enter current balance(0-9999): ";
cin >> amt;
if(amt < 0 || amt > 9999) // >,>=,<,<=,==,!= are six relational operators in C++
cout << "Invalid current balance!" << endl;
else
CurrBalance = amt;
cout << "Enter overdraft balance: ";
cin >> OverDraftBalance;
cout << "Enter overdraft limit: ";
cin >> OverDraftLimit;
}
void checking::deposit (double amount) // deposit function with a parameter amount that can be used inside function
{ // function opening braces, function body(statements inside function) starts here
if(amount < 0 || amount > 9999)
cout << "Invalid deposit amount! Should be in range 0-9999." << endl;
else
CurrBalance += amount; // amount added to balance in bank account
} // function ends here
void checking::withdraw (double amount)
{
if(amount < 0 || amount > 9999)
cout << "Invalid withdrawal amount! Should be in range 0-9999." << endl;
else
{
double oldCurBal = CurrBalance;
oldOverdraftBal = OverDraftBalance;
CurrBalance -= amount; //inside withdraw function, amount withdrawn is deducted from balance.
// -= is the shorthand operator. Expression used means the same as CurrBalance = CurrBalance - amount.
if(CurrBalance < 0)
{
OverDraftBalance += (-CurrBalance); // OverDraftBalance = OverDraftBalance + (-CurrBalance)
if(OverDraftBalance > OverDraftLimit) //more than overdraft limit , reset to old values
{
cout << "Overdraft Limit exceeded" << endl;
CurrBalance = oldCurBal;
OverDraftBalance = oldOverdraftBal;
}
}
}
}
void checking::updatelimit (double lim) //updatelimit function with lim parameter of type double.Various data types are in C++.Int,double,float,etc
{
if(lim < 0 || lim > 2000)
cout << "Invalid overdraft limit! Should be in range 0-2000." << endl;
else
{
OverDraftLimit = lim;
}
}
void checking::updatetype (char type) // function updatetype(char type) with parameter type having data type char
{
if(type == 'D' || type == 'S' || type == 'P')
Type = type;
else
{
cout << "Invalid account type! " << endl;
}
}
void checking::output () // function output
{
cout << setprecision(2); //show 2 decimal places
cout << "Current balance: $" << CurrBalance << endl;
cout << "Overdraft balance: $" << OverDraftBalance << endl;
cout << "Overdraft limit: $" << OverDraftLimit << endl;
if(CurrBalance != 0 && OverDraftLimit != 0)
{
double DOR = OverDraftBalance/CurrBalance * (OverDraftBalance/OverDraftLimit); // DOR formula. * multiplication operator and / is division
cout << "Deposit Overdraft Ratio DOR: " << DOR << "%" << endl;
}
HERE IS THE checking.h code for the other file********************************************
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class checking
{
private:
string Name;
int AcctNum;
char Type;
double CurrBalance;
double OverDraftBalance;
double OverDraftLimit;
public:
checking();
void input ();
void deposit (double amount);
void withdraw (double amount);
void updatelimit (double lim);
void updatetype (char type);
void output ();
};
checking::checking()
{
Name = "unassigned";
Type = 'D';
AcctNum = 0;
CurrBalance = 0;
OverDraftBalance = 0;
OverDraftLimit = 0;
}
void checking::input ()
{
char t;
double amt;
cout << "Enter name: ";
cin >> Name;
cout << "Account type (D/S/P): ";
cin >> t;
if(t == 'D' || t == 'S' || t == 'P')
Type = t;
else
{
cout << "Invalid account type! " << endl;
}
cout << "Enter current balance(0-9999): ";
cin >> amt;
if(amt < 0 || amt > 9999)
cout << "Invalid current balance!" << endl;
else
CurrBalance = amt;
cout << "Enter overdraft balance: ";
cin >> OverDraftBalance;
cout << "Enter overdraft limit: ";
cin >> OverDraftLimit;
}
void checking::deposit (double amount){
if(amount < 0 || amount > 9999)
cout << "Invalid deposit amount! Should be in range 0-9999." << endl;
else
CurrBalance += amount;
}
void checking::withdraw (double amount){
if(amount < 0 || amount > 9999)
cout << "Invalid withdrawal amount! Should be in range 0-9999." << endl;
else {
double oldCurBal = CurrBalance, oldOverdraftBal = OverDraftBalance;
CurrBalance -= amount;
if(CurrBalance < 0)
{
OverDraftBalance += (-CurrBalance);
if(OverDraftBalance > OverDraftLimit) //more than overdraft limit , reset to old values
{
cout << "Overdraft Limit exceeded" << endl;
CurrBalance = oldCurBal;
OverDraftBalance = oldOverdraftBal;
}
}
}
}
void checking::updatelimit (double lim){
if(lim < 0 || lim > 2000)
cout << "Invalid overdraft limit! Should be in range 0-2000." << endl;
else {
OverDraftLimit = lim;
}
}
void checking::updatetype (char type){
if(type == 'D' || type == 'S' || type == 'P')
Type = type;
else
{
cout << "Invalid account type! " << endl;
}
}
void checking::output (){
cout << setprecision(2); //show 2 decimal places
cout << "Current balance: $" << CurrBalance << endl;
cout << "Overdraft balance: $" << OverDraftBalance << endl;
cout << "Overdraft limit: $" << OverDraftLimit << endl;
if(CurrBalance != 0 && OverDraftLimit != 0){
double DOR = OverDraftBalance/CurrBalance * (OverDraftBalance/OverDraftLimit);
cout << "Deposit Overdraft Ratio DOR: " << DOR << "%" << endl;
}
}
Id really appreciate it if someone could proofread, make corrections and repost so I can see what I am doing wrong. The more //comments the better to help me as I am learning this course on my own with the help of friends and colleagues
Explanation / Answer
Hi errors are because you have already declared functions in .h file and defined in that .h only as well. And again you have included .h file and again you have redefined all the functions of checking.h in checking.cpp. to delete these erros either you change names of functions in cpp file or doesnot include checking.h header and remove Checking:: before all the function definitions.
And one more error is with the following block of code
Name = "unassigned";
// default values initialised
Type = 'D';
AcctNum = 0;
CurrBalance = 0;
OverDraftBalance = 0;
OverDraftLimit = 0;
The errors here are you hav declared these in checking.h file but you cannot directly access them. To access them and no need to define deafult constrcutor as well.
TO make your code work n=below are the modifications that I have done.
checking.h
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class checking
{
private:
string Name;
int AcctNum;
char Type;
double CurrBalance;
double OverDraftBalance;
double OverDraftLimit;
public:
checking();
void input ();
void deposit (double amount);
void withdraw (double amount);
void updatelimit (double lim);
void updatetype (char type);
void output ();
};
checking::checking()
{
Name = "unassigned";
Type = 'D';
AcctNum = 0;
CurrBalance = 0;
OverDraftBalance = 0;
OverDraftLimit = 0;
}
/*
void checking::input ()
{
char t;
double amt;
cout << "Enter name: ";
cin >> Name;
cout << "Account type (D/S/P): ";
cin >> t;
if(t == 'D' || t == 'S' || t == 'P')
Type = t;
else
{
cout << "Invalid account type! " << endl;
}
cout << "Enter current balance(0-9999): ";
cin >> amt;
if(amt < 0 || amt > 9999)
cout << "Invalid current balance!" << endl;
else
CurrBalance = amt;
cout << "Enter overdraft balance: ";
cin >> OverDraftBalance;
cout << "Enter overdraft limit: ";
cin >> OverDraftLimit;
}
void checking::deposit (double amount){
if(amount < 0 || amount > 9999)
cout << "Invalid deposit amount! Should be in range 0-9999." << endl;
else
CurrBalance += amount;
}
void checking::withdraw (double amount){
if(amount < 0 || amount > 9999)
cout << "Invalid withdrawal amount! Should be in range 0-9999." << endl;
else {
double oldCurBal = CurrBalance, oldOverdraftBal = OverDraftBalance;
CurrBalance -= amount;
if(CurrBalance < 0)
{
OverDraftBalance += (-CurrBalance);
if(OverDraftBalance > OverDraftLimit) //more than overdraft limit , reset to old values
{
cout << "Overdraft Limit exceeded" << endl;
CurrBalance = oldCurBal;
OverDraftBalance = oldOverdraftBal;
}
}
}
}
void checking::updatelimit (double lim){
if(lim < 0 || lim > 2000)
cout << "Invalid overdraft limit! Should be in range 0-2000." << endl;
else {
OverDraftLimit = lim;
}
}
void checking::updatetype (char type){
if(type == 'D' || type == 'S' || type == 'P')
Type = type;
else
{
cout << "Invalid account type! " << endl;
}
}
void checking::output (){
cout << setprecision(2); //show 2 decimal places
cout << "Current balance: $" << CurrBalance << endl;
cout << "Overdraft balance: $" << OverDraftBalance << endl;
cout << "Overdraft limit: $" << OverDraftLimit << endl;
if(CurrBalance != 0 && OverDraftLimit != 0){
double DOR = OverDraftBalance/CurrBalance * (OverDraftBalance/OverDraftLimit);
cout << "Deposit Overdraft Ratio DOR: " << DOR << "%" << endl;
}
}*/
checking.cpp
#include "checking.h"
#include <iostream>
// header file includede for input output stream
#include <iomanip>
using namespace std; //namespace to differentiate same names of classes,functions,etc
int main ()
{
checking();
// way to define constructor checking() of checking class,to initialise default values to data members
{
string Name = "unassigned";
// default values initialised
char Type = 'D';
int AcctNum = 0;
double CurrBalance = 0;
double OverDraftBalance = 0;
double OverDraftLimit = 0;
}
}
void checking::input() // void input() function of checking class is defined here with its body
{
char t;
double amt;
cout << "Enter name: ";
//cout is used to display to console
cin >> Name; //cin is used to take input from console typed by user
cout << "Account type (D/S/P): ";
cin >> t;
if(t == 'D' || t == 'S' || t == 'P') // == is relational operator used to check whether t is equal to 'D' , 'S' , 'P' types or not
// when condition inside if braces () is true ,if block executes otherwise else
// pipe symbol || is OR operator. gives false when both conditions are false, otherwise true
Type = t;
else
{
cout << "Invalid account type! " << endl;
}
cout << "Enter current balance(0-9999): ";
cin >> amt;
if(amt < 0 || amt > 9999) // >,>=,<,<=,==,!= are six relational operators in C++
cout << "Invalid current balance!" << endl;
else
CurrBalance = amt;
cout << "Enter overdraft balance: ";
cin >> OverDraftBalance;
cout << "Enter overdraft limit: ";
cin >> OverDraftLimit;
}
void checking::deposit (double amount) // deposit function with a parameter amount that can be used inside function
{ // function opening braces, function body(statements inside function) starts here
if(amount < 0 || amount > 9999)
cout << "Invalid deposit amount! Should be in range 0-9999." << endl;
else
CurrBalance += amount; // amount added to balance in bank account
} // function ends here
void checking::withdraw (double amount)
{
if(amount < 0 || amount > 9999)
cout << "Invalid withdrawal amount! Should be in range 0-9999." << endl;
else
{
double oldCurBal = CurrBalance;
double oldOverdraftBal = OverDraftBalance;
CurrBalance -= amount; //inside withdraw function, amount withdrawn is deducted from balance.
// -= is the shorthand operator. Expression used means the same as CurrBalance = CurrBalance - amount.
if(CurrBalance < 0)
{
OverDraftBalance += (-CurrBalance); // OverDraftBalance = OverDraftBalance + (-CurrBalance)
if(OverDraftBalance > OverDraftLimit) //more than overdraft limit , reset to old values
{
cout << "Overdraft Limit exceeded" << endl;
CurrBalance = oldCurBal;
OverDraftBalance = oldOverdraftBal;
}
}
}
}
void checking::updatelimit (double lim) //updatelimit function with lim parameter of type double.Various data types are in C++.Int,double,float,etc
{
if(lim < 0 || lim > 2000)
cout << "Invalid overdraft limit! Should be in range 0-2000." << endl;
else
{
OverDraftLimit = lim;
}
}
void checking::updatetype (char type) // function updatetype(char type) with parameter type having data type char
{
if(type == 'D' || type == 'S' || type == 'P')
Type = type;
else
{
cout << "Invalid account type! " << endl;
}
}
void checking::output () // function output
{
cout << setprecision(2); //show 2 decimal places
cout << "Current balance: $" << CurrBalance << endl;
cout << "Overdraft balance: $" << OverDraftBalance << endl;
cout << "Overdraft limit: $" << OverDraftLimit << endl;
if(CurrBalance != 0 && OverDraftLimit != 0)
{
double DOR = OverDraftBalance/CurrBalance * (OverDraftBalance/OverDraftLimit); // DOR formula. * multiplication operator and / is division
cout << "Deposit Overdraft Ratio DOR: " << DOR << "%" << endl;
}
}