'SalesTaxPct' was not declared in this scope - c++

I am new to C++. I am struggling with the pass by value thing, and no one can explain what I am doing wrong to me in a way I can understand. I know this is my fault, but Ii am asking for help with my code. HELP PLEASE!
#include <iostream>
using namespace std;
double getValues();
double getSalesTax(double SalesTaxPct);
double gettotal_price(double base, double opt);
void PrintFinal(double base,double opt,double SalesTaxPct);
// function to control all other functions
int main()
{
getValues();
getSalesTax(SalesTaxPct);
PrintFinal(base,pt,SalesTaxPct);
}
// function to calculate sales tax percent into decimal
double getSalesTax( double SalesTaxPct )
{
double SalesTax;
SalesTax = SalesTaxPct / 100;
return SalesTax;
}
// function to find total
double gettotal_price(double base, double opt, double SalesTax)
{
return = (base + opt) * (1 + SalesTax);
}
// function to show user all values input and also total
void PrintFinal(double base, double opt, double SalesTaxPct)
{
cout << "Base vehicle price: $" << base << endl;
cout << "Options Price: $" << opt << endl;
cout << "Sales tax pct: " << SalesTaxPct << "%" << endl;
cout << "Total vehicle price: $" << gettotal_price(double base, double opt, double SalesTax) << endl;
}
// function to get input values
void getValues()
{
double base, double opt, double SalesTaxPct;
cout << "Enter a base vehicle price: " << endl;
cin >> base;
cout << "Enter options price: " << endl;
cin >> opt;
cout << "Enter a sales tax percent: " << endl;
cin >> SalesTaxPct;
}

When you are in main, let's go over what the program sees:
int main()
{
getValues();
getSalesTax(SalesTaxPct);
PrintFinal(base,pt,SalesTaxPct);
}
The only variables that your program knows about at this point are: getValues(), getSalesTax(), gettotal_price(), and PrintFinal(). The warning is telling you that at this point of your program, SalesTaxPct was not declared yet, and looking at our list of variables / functions that the program knows about, we see that, indeed, SalesTaxPct is not on the list. Where do we expect the value of SalesTaxPct to come from?
It looks like that comes from the function getValues, and we are getting it from user input. However, any time that you have { ... }, the stuff inside the braces cannot be accessed outside. Therefore, SalesTaxPct is only "in scope" inside the function getValues. If you want it to be accessible outside of that function (which you do), you need to change things around a bit.
int main()
{
double base;
double opt;
double SalesTaxPct;
getValues(base, opt, SalesTaxPct);
getSalesTax(SalesTaxPct);
PrintFinal(base, opt, SalesTaxPct);
}
Now all of our variables still exist when we need them in main. However, there is still a problem here. We want the changes we pass into getValues to change the variables in main. This means we cannot pass "by value" because that will first make a copy, and then change those copies (not what we want). Instead, we need to say that the changes we make need to be returned from the function some how:
void getValues(double & base, double & opt, double & SalesTaxPct);
That little & there means that rather than making a copy and changing that copy, we are telling the function to operate on the variable we pass in directly. This is referred to as "pass by reference".
There are some similar problems in other parts of your code, but perhaps now you can figure out how to fix them.

Related

How to display the values of a class?

I am almost new in c++. I have created a class of a student. First of all, when i returned the media of the marks, it isn't a float value. For example: if I enter 5 and 10, it returns 7 instead of 7.5.
Secondly, when I want to display the name and media with the function disp(), it doesn't work.
Could anyone help a bit?
Thanks
#include <iostream>
using namespace std;
class student{
public:
string name;
int mark1, mark2;
float calc_media(){
float media = (mark1 + mark2)/2;
return media;
}
void disp(){
cout << "Student:" << name << endl;
cout << "media:"<< calc_media() << endl;
}
};
int main (){
student peter;
cout <<"name:" ;
cin>>peter.name;
cout <<"mark1:" ;
cin>>peter.mark1;
cout <<"mark2:" ;
cin>>peter.mark2;
cout <<"ALL:" << peter.disp();
return 0;
}
In this expression:
(mark1 + mark2) / 2;
you are doing integer division, since both the variables and the literal are int types. You could simply do:
(mark1 + mark2) / 2.0;
instead, to get floating point division.
To get disp to work, note that it doesn't return anything, so you need to simply call it like this:
peter.disp();
and not pass the result of this function to cout.
Alternatively, instead of disp, you can overload the operator<< like this:
std::ostream& operator<<(student const &s, std::ostream &out)
{
out << "Student:" << name << endl;
out << "media:"<< calc_media() << endl;
return out;
}
and then use it like this:
cout << peter;
Here what you are doing is called integer division, both variables mark1 and mark2 are integers and so is 2 thus, it will provide you an integer.
float calc_media(){
float media = (mark1 + mark2)/2;
Try replacing 2 with 2.0 to achieve floating point division.
float media = (mark1 + mark2)/2.0;
And as your disp function is not returning something you need not to cout it. You can do this,
cout <<"ALL:";
peter.disp();

Determining length between vectors giving erroneous results

This is the beginning of my code that's supposed to be attached to a much larger code where I have to use 3 vectors to determine if I have a triangle, and if so, compute the area with multiple methods(like Heron's formula) for a homework assignment.
My main question is that, given the code below, which should result in sides with lengths of 5/4/3 respectively... why am I getting results of 4.6e-310/6.9e-310/0.
I am aware that my code may not be pretty, and may have gross conceptual errors, but I've only been learning C++ for 3 weeks after studying Fortran for a month.... and I feel super lost trying to do this.
#include <iostream>
#include <math.h>
// design vector class
class Vector {
private:
double Side1() {
return sqrt(pow(p1x-p2x,2)+pow(p1y-p2y));
}
double Side2() {
return sqrt(pow(p2x-p3x,2)+pow(p2y-p3y,2));
}
double Side3() {
return sqrt(pow(p3x-p1x,2)+pow(p3y-p1y,2));
}
public:
double p1x=1.0, p2x=4.0, p3x=4.0;
double p1y=5.0, p2y=9.0, p3y=5.0;
};
// get length
int main() {
double Side1, Side2, Side3;
std::cout << "Side 1 length is " << Side1 << std::endl;
std::cout << "Side 2 length is " << Side2 << std::endl;
std::cout << "Side 3 length is " << Side3 << std::endl;
return 0;
}
There is a couple of errors in your code:
First, your functions are private. This means you cannot ever call these functions, except from within another function of this class. You probably meant to have public funtions and private data members instead:
class Vector {
public:
double Side1() {
return sqrt(pow(p1x-p2x,2)+pow(p1y-p2y));
}
double Side2() {
return sqrt(pow(p2x-p3x,2)+pow(p2y-p3y,2));
}
double Side3() {
return sqrt(pow(p3x-p1x,2)+pow(p3y-p1y,2));
}
private:
double p1x=1.0, p2x=4.0, p3x=4.0;
double p1y=5.0, p2y=9.0, p3y=5.0;
};
Second, you need an object of the type Vector (because your functions are not static members of this class). Then, on this object, you need to call your methods.
int main() {
Vector v; // create object
std::cout << "Side 1 length is " << v.Side1() << std::endl; //note the call operator ()
std::cout << "Side 2 length is " << v.Side2() << std::endl;
std::cout << "Side 3 length is " << v.Side3() << std::endl;
return 0;
}
Your current code creates 3 unitialized double variables, which have nothing in common with your functions (they just share the name).

Why does input value become corrupt when it displays for my output?

In this code, once I run my program, the output for my death rate becomes corrupt. But all my variables are defined as double and there is no variable type change. Why is my output corrupt? Is it because my value is not being passed correctly?
#include <iostream>;
#include <iomanip>;
using namespace std;
//variable definition
class Population
{
private:
double population, births, deaths;
double b_rate, d_rate;
public:
void setpopulation(double p);
void setdeath(double death);
void setbirth(double b);
double getdrate();
double getbrate();
};
void Population::setpopulation(double p)
{
population = p;
}
void Population::setdeath(double death)
{
deaths = death;
d_rate = births / population;
}
void Population::setbirth(double b)
{
births = b;
b_rate = births/population;
}
double Population::getdrate()
{
cout << d_rate << endl;
return d_rate;
}
double Population::getbrate()
{;
return b_rate;
}
int main()
{
Population pop;
pop.setpopulation(100000);
pop.setdeath(7500);
pop.setbirth(8000);
cout << fixed << setprecision(2);
cout << "Death rate = " << pop.getdrate() << endl;
cout << "Birth rate = " << pop.getbrate() << endl;
return 0;
}
This is the reason. Try to be careful.
This is how you are calling you are functions.
pop.setpopulation(100000);
pop.setdeath(7500);
pop.setbirth(8000);
And here is your setdeadth() -
void Population::setdeath(double death)
{
deaths = death;
d_rate = births / population;
}
Since in main you are calling setdeadth first and in setdeath. You are using births which are not known, or you can say births is uninitialized. So it is undefined.
To correct it do.
pop.setpopulation(100000);
pop.setbirth(8000);
pop.setdeath(7500);
Call setbirth fist. Btw in don't think your formulas are correct. I didn't get why you are using birth in calculating death rate. It should be deaths. But I'm not pointing as it may be possible that you did that by choice. That is up to you.
From the code you provided, in the method setdeath you are making d_rate = births/population, you must be using death there. But at that time births is not initialized, so it takes the garbage value(not zero in all cases, it can be anything as inbuilt default are unknown) which is why you are getting the death rate wrong!!!
Thanks
Rajkumar

how can a void function be called to the main, if there are no returns?

I wanted to create a function that retrieves all the information from previous functions within the same Class, and prints the values that were returned in the format of a bunch of cout statements, there is nothing for me to return in this PrintStatement() function, so I would create a void function, correct? My issue is in the int main(), I cannot cout a void function.
this is my account header file, and the function piece from my account.cpp file.
class Account {
public:
//Object constructor
Account(char firstName[], char lastName[], char sinNumber[], double balance, int accountType, int transactions);
//Object operations
double DepositAmt(double amount);
double WithdrawAmt(double amount);
void PrintStatement();
double getFinalBalance(double fbal);
string getAccountType();
double getTransactions (double Deposit, double Withdraw);
private:
//Object properties
char firstName[255];
char lastName[255];
char sinNumber[255];
double balance;
int accountType;
int transactions;
};
void Account::PrintStatement()
{
cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
cout << "SIN Number: " << sinNumber << endl;
cout << "Account Type: " << accountType << endl;
cout << "Final Balance: " << balance << endl;
cout << "Transactions: " << transactions << endl;
};
the global variables have already been initialized.
What I've tried:
I originally tried to cout << account.PrintStatement() << endl; however I get an error C2679 (binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
I thought maybe changing things to apply to a string function instead would work, but instead I get a bunch of int conversion errors etc.
I'm unsure of what to do.
I am required to put these in a function just to be clear.
I tried using this question https://stackoverflow.com/questions/12766858/how-to-call-void-function-from-main to help me, it made sense that the poster was using a reference, but I do not have that. Is there another way?
I originally tried to cout << account.PrintStatement() << endl;
Well, the expression account.PrintStatement() is abject nothingness because the function has a void return type. As you've indicated, the function returns nothing, so there is nothing to stream to cout.
The function itself has already streamed a bunch of stuff to cout, fulfilling all your couty needs. So, simply invoke it:
account.PrintStatement();
That's it!
Just call the method on your instance of the class. It doesn't need to return anything; it will do the counts you want and then return to main.
When we use cout<<something_here, the compiler will interpret it as "print the value of something_here". Now, something_here can be a lot of things. When it is a function, cout will print the value returned by the function. In your case, the return type is void i.e. nothing. So, there is nothing to print.
To fix your issue, you can directly call account.PrintStatement(); since you have already printed what you wanted to print inside this function.

Passing variables from one function to another trouble (C++)

I seem to be having trouble with passing variables from one function to another. My main class looks like this :
class EmployeeClass {
public:
void ImplementCalculations(string EmployeeName, double hours, double wage);
void DisplayEmployeeInformation();
void Addsomethingup (EmployeeClass, EmployeeClass, EmployeeClass);
string EmployeeName;
double hours;
double wage;
double basepay;
double overtime_hours;
double overtime_pay ;
double overtime_extra ;
double iTotal_salaries ;
double iIndividualSalary;
double iTotal_hours ;
double iTotal_OvertimeHours;
};
I am passing a string and two doubles to the function "ImplementCalculations" as shown here:
Employee1.ImplementCalculations (Employee1.EmployeeName, Employee1.hours, Employee1.wage);
Employee2.ImplementCalculations (Employee2.EmployeeName, Employee2.hours, Employee2.wage);
Employee3.ImplementCalculations (Employee3.EmployeeName, Employee3.hours, Employee3.wage);
These passed variables have some math done to them:
void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage) {
//Initialize overtime variables
double overtime_hours=0;
double overtime_pay=0;
double overtime_extra=0;
double basepay = 0;
double iIndividualSalary = 0;
if (hours > 40)
{
basepay = 40 * wage;
overtime_hours = hours - 40;
overtime_pay = wage * 1.5;
overtime_extra = overtime_hours * overtime_pay;
iIndividualSalary = overtime_extra + basepay;
/*
Implement function call to output the employee information. Function is defined below.
*/
DisplayEmployeeInformation ();
} // if (hours <= 40)
else
{
basepay = hours * wage;
overtime_hours=0;
overtime_extra=0;
iIndividualSalary = basepay;
/*
Implement function call to output the employee information. Function is defined below.
*/
DisplayEmployeeInformation();
}; // End of the else
However this is where things go sour. I think that because I am not passing DisplayEmployeeInformation anything it doesn't want to work, which also makes it so that AddSomethingUp doesn't work as well.
void EmployeeClass::DisplayEmployeeInformation () {
// This function displays all the employee output information.
cout << "Employee Name ............. = " << EmployeeName << endl;
cout << "Base Pay .................. = " << setprecision(5)<< basepay << endl;
cout << "Hours in Overtime ......... = " << setprecision(4)<< overtime_hours << endl;
cout << "Overtime Pay Amount........ = " << setprecision(5)<< overtime_pay << endl;
cout << "Total Pay ................. = " << setprecision(6)<< iIndividualSalary << endl;
} // END OF Display Employee Information
void EmployeeClass::Addsomethingup (EmployeeClass Employee1, EmployeeClass Employee2, EmployeeClass Employee3){
/*
Adds the total hours for objects 1, 2, and 3.
Adds the salaries for each object.
Adds the total overtime hours.
*/
iTotal_hours = Employee1.hours + Employee2.hours + Employee3.hours;
iTotal_salaries = Employee1.iIndividualSalary + Employee2.iIndividualSalary + Employee3.iIndividualSalary;
iTotal_OvertimeHours = Employee1.overtime_hours + Employee2.overtime_hours + Employee3.overtime_hours;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout << "%%%% Total Employee Salaries ..... = " << setprecision(6)<<iTotal_salaries <<endl;
cout << "%%%% Total Employee Hours ........ = " << setprecision(5)<<iTotal_hours << endl;
cout << "%%%% Total Overtime Hours......... = " << setprecision(5)<<iTotal_OvertimeHours << endl;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
} // End of function
The one line in AddSomethingUp that works is the iTotal_hours. No idea why it gets that, but it is the only thing it gets.
So my question is do I have to pass DisplayEmployeeInformation some sort of variables? What should that look like? More like passing the information to ImplementCalculations?
EmployeeClass has a number of member variables such as: hours, overtime_hours and others.
The ImplementCalculations function declares a bunch of local variables right at the start with the exact same name. It also has parameters with the exact same name.
I assume that in the start of ImplementCalculations you don't wish to declare new variables but rather set the values for the ones in the class:
void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage) {
//Initialize overtime variables
overtime_hours=0;
overtime_pay=0;
overtime_extra=0;
basepay = 0;
iIndividualSalary = 0;
... (other code here) ...
Also consider renaming the parameter variables to something like p_EmployeeName and p_hours (etc) so they don't clash with the member variable names in your class.
While it is not 100% clear what your problem is, it is very likely that the cause of it is the fact that you are not using references.
For example, take this function:
void Addsomethingup (EmployeeClass Employee1);
This creates a copy of Employee1, which is thrown away when the function is left. The original object which you pass into the function is not touched and remains the same.
You might try this:
void Addsomethingup (EmployeeClass &Employee1);
You can also pass const references as an alternative to useless copying if you don't intend to modify the object:
void Addsomethingup (EmployeeClass const &Employee1);
In fact, you should do so for your std::string arguments (although C++11 may be kind of a game changer here, but you better consult Google for this, or else it becomes too off-topic for this question). And you probably need to read a good C++ book. References are a pretty basic language feature.
If you come from a Java background, don't be confused by the word "reference". A "reference" in Java is more a like a pointer in C++, whereas C++ references do not have a Java counterpart.