initialize class functions but output still 0 - c++

I am having problems with this program. When I compile it, I intialize all of the variables based upon the users input, but the cout still shows that the problem has '0' for most of the statements and for one of the statements its a '-negative' number. Any thoughts?
#include <iostream>
#include <conio.h>
#include <cmath>
#include <stdexcept>
using namespace std;
class MortgageCalc
{
protected:
float term;
public:
void setData(float, float, float);
float setTerm ();
float monthly;
float total;
float interest;
int years;
float loan;
};
void MortgageCalc::setData(float l, float i, float y)
{
loan = l;
interest = i;
years = y;
setTerm();
}
float MortgageCalc::setTerm()
{ //simple interest calculation with power calc to establish whole number translation
term = pow((1 + ((interest/100) / 12)), (12 * years));
return term;
}
class mPayment : public MortgageCalc
{
public:
int monthly()
{
return ((loan * ((interest/100) / 12) * term ) / (term - 1));
}
};
class tPayment : public mPayment
{
public:
int total()
{
return (monthly() * (years * 12));
}
};
class iPayment : public tPayment
{
public:
int plusInterest()
{
return (total() - loan);
}
};
int main()
{
double loan(0), interest(0);
int years = 0;
MortgageCalc mort1;
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
mPayment m;
cout << "Monthly payment due is " << m.monthly() << "." << endl;
tPayment t;
cout << "Total payment will be " << t.total() << "." << endl;
iPayment i;
cout << "Total payment plus Interest will be " << i.plusInterest() << "." << endl;
return 0;
};

You are taking all different objects like MortgageCalc mort1; mPayment m; tPayment t; iPayment i;.
These object do not have any relation.
Example:
mort1 = {term, monthly, total, interest, years, loan}
and suppose you have initialize with 1
mort1 = {term=1, monthly=1, total=1, interest=1, years=1, loan=1}
but it doesnot impact to the m because both are stored in memory on different location.
m = {term=0, monthly=0, total=0, interest=0, years=0, loan=0}
You can check both have different base address like cout<<&mort1<<endl<<&m; .
Data member you have set is part of MortgageCalc mort1 instead of mPayment m; tPayment t;.
You need to brush up your C++ basic's.

You use default constructors on those lines:
mPayment m;
tPayment t;
iPayment i;
They have no notion of previously input data held in mort1. You did not take care for any way to "share" or "communicate" this data.
m,t,i were all initialized with random data. There is no relation to mort1.
I won't go into details of what correct architecture here would be, but you should read about base class initialization. As a hint I'd say in your (a little weird) example you could try making this syntax work:
mPayment m(mort1);

Related

Calculations wont display in output

I'm a student in a basic programming class and I'm trying to complete this program for a class assignment. It's a simple program that calculates compounded interest by the inputs of the user. However, when writing the code, I noticed that the the result is 0 even though based on the input I would expect otherwise. Could anyone tell me why the program isn't showing results?
#include <iostream>
#include <cmath>
using namespace std;
// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);
// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;
// Start of Main function
int main(void)
{
cout << "What is the current value of the account?";
cin >> presentValue;
cout << "How many months will Interest be added to the account?";
cin >> months;
cout << "And what will be the Interest Rate of the account?";
cin >> interestRate;
cout << "After " << months << " months, your account balence will be $" << value << ".";
return 0;
}
void futureValue()
{
if (presentValue <= 0)
{
cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
return;
}
else
{
value = presentValue * pow(interestRate + 1, months);
return;
}
}
Yes. You are not calling the futureValue function which would compute the value for you. Due to the value not being computed, it remains 0. Fix:
#include <iostream>
#include <cmath>
using namespace std;
// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);
// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;
// Start of Main function
int main(void)
{
cout << "What is the current value of the account?";
cin >> presentValue;
cout << "How many months will Interest be added to the account?";
cin >> months;
cout << "And what will be the Interest Rate of the account?";
cin >> interestRate;
futureValue(); //Here we compute the value
cout << "After " << months << " months, your account balence will be $" << value << ".";
return 0;
}
void futureValue()
{
if (presentValue <= 0)
{
cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
return;
}
else
{
value = presentValue * pow(interestRate + 1, months);
return;
}
}

Required output is 66.6 but I am getting gibberish (as posted below), how to fix this?

To calculate total in void display, I need the values of part1marks, part2marks and score. Hence I have created return functions (This is how I have to do it as per my assignment). However, I am getting undesirable output as shown below.
#include<iostream>
using namespace std;
class student
{
int rollno;
public:
void getnumber()
{
cout << "Enter roll number: ";
cin >> rollno;
cout << endl;
}
int putnumber()
{
return rollno;
}
};
class test : virtual public student
{
float part1marks;
float part2marks;
public:
void getmarks()
{
cout << "Enter Marks(Parts 1 and 2): ";
cin >> part1marks >> part2marks;
cout << endl;
}
float putmarks()
{
float marks = part1marks + part2marks;
return marks;
}
};
class sports : virtual public student
{
float score;
public:
void getscore()
{
cout << "Enter score: ";
cin >> score;
cout << endl;
}
float putscore()
{
return score;
}
};
class result : public test, public sports
{
float total;
public:
void display()
{
test t;
sports s;
float sc = s.putscore();
float ms = t.putmarks();
total = sc + ms;
cout <<"Total marks= "<< total;
}
};
int main()
{
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
system("pause");
return 0;
}
Expected Output
Enter roll number: 21
Enter Marks(Parts 1 and 2): 22.2
22.2
Enter score: 22.2
Total marks= 66.6 Press any key to continue . . .
Actual Output
Enter roll number: 21
Enter Marks(Parts 1 and 2): 22.2
22.2
Enter score: 22.2
Total marks= -3.22123e+08Press any key to continue . . .
You are creating new objects with uninitialized values in display().
void display()
{
test t; // <- This is a new object with uninitialized members variables
sports s; // <- This is a new object with uninitialized members variables
float sc = s.putscore(); // Replace with this->putscore() or simply putscore()
float ms = t.putmarks(); // Replace with this->putmarks() or simply putmarks()
total = sc + ms;
cout <<"Total marks= "<< total;
}
Multiple inheritance is almost never a good idea. Is your assignment really forcing you to do that?

no candidates for error compile error

I am trying to compile my application and it keeps running into "no match for 'operator<<'... it is not clear what the exact error of the program seems to be because he object is configured correctly as far as I can see.
#include <iostream>
#include <conio.h>
#include <cmath>
#include <stdexcept>
using namespace std;
class MortgageCalc
{
protected:
float term;
public:
void setData(float, float, float);
float setTerm ();
float monthly;
float total;
float interest;
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
int years;
float loan;
};
void MortgageCalc::setData(float l, float i, float y)
{
loan = l;
interest = i;
years = y;
setTerm();
}
float MortgageCalc::setTerm()
{ //simple interest calculation with power calc to establish whole number translation
term = pow((1 + ((interest/100) / 12)), (12 * years));
return term;
}
float MortgageCalc::setLoan(void)
{ //returns loan amt to private member
return loan;
}
float MortgageCalc::setIntrest(void)
{ //returns interest amt to private member
return interest;
}
float MortgageCalc::setYears(void)
{ //returns years to private member
return years;
}
class mPayment : public MortgageCalc
{
public:
int monthly()
{
return ((loan * ((interest/100) / 12) * term ) / (term - 1));
}
};
class tPayment : public mPayment
{
public:
int total()
{
return (monthly() * (years * 12));
}
};
class iPayment : public tPayment
{
public:
int plusInterest()
{
return (total() - loan);
}
};
int main()
{
double loan(0), interest(0);
int years = 0;
MortgageCalc mort1;
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
mPayment m;
cout << "Monthly payment due is " << m.monthly() << "." << endl;
tPayment t;
cout << "Total payment will be " << t.total() << "." << endl;
iPayment i;
cout << "Total payment plus Interest will be " << i.plusInterest() << "." << endl;
return 0;
};
cout << "Total payment plus Interest will be " << i.plusInterest << "." << endl;
plusInterest is a class method pointer. Unsurprisingly, std::ostream has no clue what to do with a class method pointer, and is rightfully voicing its very strong objection, to such a preposterous proposition that it knows what to do with some strange class's method pointer.
You probably meant to write:
cout << "Total payment plus Interest will be " << i.plusInterest() << "." << endl;
Now, that's a proper function call, that returns an int, and std::ostream is now delighted to take this int, and do its magic with it.

Find compound interest using operator overloading

I am writing a code that tries to find out the simple and compound interest using operator overloading.
While I have found the simple interest, I am having problem with the compound interest.
#include<iostream>
#include<iomanip>
using namespace std;
class Interest
{
private:
double P;
double R;
double I;
public:
Interest(){};
~Interest(){};
void setP(double recieveP){P = recieveP;}
void setR(double recieveR){R = recieveR/100;}
double getP(){return P;}
double getI(){return I;}
Interest operator*(int T)
{
class Interest int1;
int1.I= P*R*T;
return int1;
}
};
int main()
{
class Interest simp1;
class Interest comp1;
double Principle,Rate,Years;
cout << "Enter the Principle Amount" << endl;
cin >> Principle;
simp1.setP(Principle);
comp1.setP(Principle);
cout << "Enter the Rate Amount" << endl;
cin >> Rate;
simp1.setR(Rate);
comp1.setR(Rate);
cout << "Enter the number of years:";
cin >> Years;
simp1 = simp1*Years;
cout << "The Simple Interest is: " << simp1.getI() << endl;
for(int i =0; i < Years; i++)
{
comp1 = comp1*1;
comp1.setP(comp1.getI()+comp1.getP());
}
cout << "The compound Interest is: " << comp1.getI() << endl;
return 0;
}
No matter what I enter the values for the compound Interest is always zero.
When you create object Interest int1 in operator *, you set only it's I value. P and R are uninitialized, so the have trash value, like 1.314e-304. You must copy values from the source:
Interest operator*(int T)
{
class Interest int1;
int1.P = P;
int1.R = R;
int1.I= P*R*T;
return int1;
}
You also should set default values for your class members in default constructor, to avoid future mistakes:
Interest() : P(0.0), R(0.0), I(0.0)
{
};

variable is undefined, yet clearly defined in my constructor C++

I have a C++ assignment that requires me to create a class called Tips, that:
has a single member variable, float taxRate
has two constructors, a default that sets taxRate to .65, and a one parameter constructor, that sets taxRate to whatever float the user enters in my tipMain.cpp file.
has only one function, computeTip, that accepts two arguments: totBill and tipRate, both floats that must calculate the before-tax meal cost, and return the tip based on that value and the desired tip rate.
My issue occurs when I attempt to use taxRate inside the computeTip function.
If I use taxRate, computeTip doesn't understand the value of taxRate, stating it is undefined, and even if I do specify tips::taxRate, before I even compile Visual Studio states
Error: a nonstatic member reference must be relative to a specific object.
I vaguely understand this error, but even if I declare taxRate to be static in my .h file, I get LNK2019 errors.
Tips.cpp
#include <iostream>
#include <float.h>
#include "Tips.h"
using namespace std;
Tips::Tips()
{
taxRate = 0.65;
}
Tips::Tips(float a)
{
taxRate = a;
}
float computeTip(float totBill, float tipRate)
{
float mealOnly = 0;
mealOnly = (totBill/taxRate); //written both ways to show errors. taxRate undefined here.
mealOnly = (totBill/Tips::taxRate); //Error: a nonstatic member reference must be relative to a specific object
return (mealOnly * tipRate);
}
Tips.h
#ifndef Tips_H
#define Tips_H
#include <float.h>
using namespace std;
class Tips
{
public:
Tips();
Tips(float);
float computeTip(float, float, float);
float taxRate;
};
#endif
and my tipMain.cpp
#include "Tips.h"
#include <iostream>
#include <iomanip>
using namespace std;
float tax;
float meal;
float tip;
void tipProcessor();
int main()
{
char entry;
int toggle = 1;
cout << "Welcome to the Gratuity Calculator!" << endl;
cout << endl;
while (toggle != 0)
{
cout << "Enter 1 to calculate tip." << endl;
cout << endl;
cout << "Enter 2 to exit." << endl;
cout << endl;
cout << "Entry: ";
cin >> entry;
switch (entry)
{
case '1':
tipProcessor();
break;
case '2':
toggle = 0;
break;
default:
cout << "Enter 1 or 2." << endl;
cout << endl;
break;
}
}
system("pause");
return 0;
}
void tipProcessor()
{
cout << "Enter bill total: ";
cin >> meal;
cout << endl;
cout << "Enter tax rate: ";
cin >> tax;
cout << endl;
Tips thisTip(tax);
cout << "Enter tip percent: ";
cin >> tip;
cout << endl;
cout << "Tip is $" << setprecision(4) << thisTip.computeTip(meal, tip, thisTip.taxRate) << "." << endl;
cout << endl;
}
You use
float computeTip(float totBill, float tipRate)
But it should be:
float Tips::computeTip(float totBill, float tipRate)
in the implementation. Also you should hide your data members.
You are defining computeTip() as global function, therefore only public static member access is possible. Define as float Tips::computeTip(float ...) and it should compile.
You've not specified that computeTip is a method of Tips. Change it to this:
float Tips::computeTip(float totBill, float tipRate)
{
float mealOnly = 0;
mealOnly = (totBill/taxRate);
return (mealOnly * tipRate);
}
And all should be well.
Your method declaration and and method definition do not have the same signature. In your header file you indicate that computeTip should take 3 float arguments, but when you define it in the .cpp file, it only takes two arguments.
This is in addition to what other users suggest, prefixing your function with Tips:: to indicate that it is part of the Tips class and thus should be able to access member variables such as taxRate.
Thus your header declaration should look like:
float computeTips(float, float);
And your method definition should look like
float Tips::computeTip(float totBill, float tipRate)
{
...