I have looked and looked and looked at this. I can not figure out why I am getting a random crazy number for salary. Can someone please help? (Sorry for the length)

//Personal Class Definition

#include <iostream>
#include <string>
using namespace std;

//Personal class definition
class Personal
{
	public:
		Personal(string, string, int, int, int); //constructor that initiates the Income 
		void setName(string);
		string getName();
		void setAddress(string);
		string getAddress();
		void setDOB(int);
		int getDOB() const;
		void setSSN(int);
		int getSSN() const;
		void setDep(int);
		int getDep() const;
	private:
		string Name;
		string Address;
		int DOB;
		int SSN;
		int Dep;
}; //end class Income
//Personal Member Function Definitions

#include <iostream>
#include <string>
#include "Personal.h"
using namespace std;

//constructor initializes Name, Address, DOB, and SSN
Personal::Personal(string n, string a, int d, int s, int e)
{
	Name = n;
	Address = a;
	DOB = d;
	SSN = s;
	Dep = e;

} //end Personal constructor

//function to set the name of the person
void Personal::setName(string n)
{
	Name = n;

}//end function setName

//function to return the name of the person
string Personal::getName()
{	
	return Name;

}//end function getName

//function to take in the address for the person
void Personal::setAddress(string a)
{
	Address = a;

}//end function setAddress

//function to return the address of the person
string Personal::getAddress()
{
	return Address;

}//end function getAddress

//function to take in the DOB of the person
void Personal::setDOB(int d)
{
	DOB = d;

}//end function setBirthday

//function to return the DOB of the person
int Personal::getDOB() const
{
	return DOB;

}//end function getBirthday

//function to take in the SSN of the person
void Personal::setSSN(int s)
{
	SSN = s;

}//end function getSSN

//function to return the SSN of the person
int Personal::getSSN() const
{
	return SSN;

}//end function getSSN

//function to set the number of dependents of the person
void Personal::setDep(int e)
{
	Dep = e;
}//end function setDep

//function to return the number of dependents of the person
int Personal::getDep() const
{
	return Dep;
}
//Income Class Definition

#include <iostream>
using namespace std;

//Income class definition
class Income
{
	public:
		Income(double); //constructor that initiates the Income 
		void setSalary(); //function that gets a total income from all the jobs inputted
		double getSalary();
		//void setDividends(Title_Amount);
		//void getDivide) const;
		void setCapital(double);
		double getCapital() const;
		void setTaxRate();
		double getTaxRate();
		void setTotalIncome();
		double getTotalIncome();
	private:
		double salary;
		//string stock;
		//double dividend;
		double capitalgains;
		double taxrate;
		double totalincome;

}; //end class Income
//Income Member Function Definitions

#include <iostream>
#include <string>
#include "Income.h" //include definition of class Income
using namespace std;

//constructor initializes salary, stock name, stock amount, and capital gains
Income::Income(double c)
{	
	//stock = k;
	//dividend = d;
	capitalgains = c;

} //end Income constructor

//function to set the salary of the person
void Income::setSalary()
{
	double s = 0;

	do
	{
		cout << "Please enter in a salary (press 0 when done): ";
		cin >> s;

		salary += s;
	}while(s != 0);
	
}

//function to return the salary of the person
double Income::getSalary()
{	
	return salary;

}//end function getName

//function to set the capital gains of the person
void Income::setCapital(double c)
{

	capitalgains = c;

}

//function to get the capital gains of the person
double Income::getCapital() const
{

	return capitalgains;

}//end function getCapital

//function to set the total income of the person
void Income::setTotalIncome()
{
	

	totalincome = capitalgains + salary;

}

//function to get the total income of the person
double Income::getTotalIncome()
{
	return totalincome;
}

//function to set the tax rate of the person
void Income::setTaxRate()
{
	if (totalincome <= 8350)
	{
		taxrate = 0.1;
	}
	else if (totalincome >= 8351 && totalincome <= 33950)
	{
		taxrate = 0.15;
	}
	else if (totalincome >= 33951 && totalincome <= 82250)
	{
		taxrate = 0.25;
	}
	else if (totalincome >= 82251 && totalincome <= 171550)
	{
		taxrate = 0.28;
	}
	else if (totalincome >= 171551 && totalincome <= 372950)
	{
		taxrate = 0.33;
	}
	else if (totalincome >= 373951)
	{
		taxrate = 0.35;
	}

}

//function to get the tax rate of the person
double Income::getTaxRate()
{
	return taxrate;
}
//Exemption Class Definition

#include <iostream>
using namespace std;

//Exemption class definition
class Exemption
{
	public:
		Exemption(string,int, double); //constructor that initiates the Exemption class
		void setDonationName(string); //function that sets the name for each donation
		void setDonationAmount(double); //function that sets the amount for each donation
		void setNumofDonations(int); //function that sets the number of donations that the user wishes to enter
		string getDonationName(); //function that get the donation name
		int getNumofDonations() const; //function that gets the number of donations that the user wishes to enter
		double getDonationAmount() const; //function that gets the donation amount

		Exemption(double); //constructor that initiates the Exemption class
		void setEducation(double); //function that sets the Education Costs
		double getEducation() const; //function that retrieves the Education Costs
	private:
		double education; //value for education costs
		double donationamount; //value for donation amount
		int numofdonations; //value for number of donations
		string donationname; //value for donation name

}; //end class Exemptions
//Exemption Member Function Definitions

#include <iostream>
#include <string>
#include "Exemption.h" //include definition of class Exemption
using namespace std;

//constructor initializes donation names, amounts, and education costs
Exemption::Exemption(double edu)
{
	education = edu;

} //end Exemption constructor

//function to set the education costs of the person
void Exemption::setEducation(double edu)
{
	education = edu;
}

//function to return the education costs of the person
double Exemption::getEducation() const
{	
	return education;

}//end function getEducation

void Exemption::setDonationName(string donname)
{

}
//IncomeTaxProject Main Function

#include <iostream>
#include <string>
#include "Personal.h" //include definition of class Personal
#include "Exemption.h" //include definition of class Exemption
#include "Income.h" //include definition of class Income
using namespace std;

//function main begins program execution
int main ()
{

	string n, a;
	int d, s, e;

	cout << "\nPlease enter your name: ";
	getline(cin,n);

	cout << "Please enter your address: ";
	getline(cin,a);

	cout << "Please enter your date of birth (MMDDYY):  ";
	cin >> d;

	cout << "Please enter your social security number (exclude spaces/dashes): ";
	cin >> s;

	cout << "Please enter the number of dependents that you have for the 2009 tax year: ";
	cin >> e;
	
	Personal person(n, a, d, s, e);
	
	float c = 0;
	
	Income person1(c);
	
	person1.setSalary();

	cout << "Please enter your total capital gains for the 2009 tax year: ";
	cin >> c;
    
	person1.setCapital(c);
	
	float edu = 0;

	Exemption person2(edu);
	
	cout << "Please enter your total education costs for the 2009 tax year: ";
	cin >> edu;

	cout << "\n\nIncome tax for year 2009: ";
	cout << "\nName: " << person.getName() << endl;
	cout << "Address: " << person.getAddress() << endl;
	cout << "DOB: " << person.getDOB() << endl;
	cout << "SSN: " << person.getSSN() << endl;
	cout << "Number of Dependents: " <<person.getDep() << endl;
	cout << "\n\n\nSingle taxpayer, " <<person.getDep() << " dependents, taking standard deduction:" << endl;
	cout << "Gross Income" << endl;
	cout << "\tSalary - \t\t\t\t$" << person1.getSalary() << endl;
	cout << "\tDividends - \t\t\t\t$" << endl;
	//need to add a string_value
	cout << "\tCapital Gains - \t\t\t$" << person1.getCapital() << endl;
	cout << "\tTotal - \t\t\t\t$" << person1.getTotalIncome() << endl;
	cout << "\nDeduction" << endl;
	cout << "\tStandard deduction - \t\t\t$5000" << endl;
	cout << "\tDeduction for dependents - \t\t$" << 5000*person.getDep() << endl;
	cout << "\tTotal - \t\t\t\t$" << 5000+(5000*person.getDep()) << endl;
	cout << "\nExemption" << endl;
	cout << "\tDonation - \t\t\t\t$" << endl; //need to add a string_value
	//
	cout << "\tEducation Cost - \t\t\t$" << person2.getEducation() << endl;
	cout << "\tTotal - \t\t\t\t$" << endl;
	cout << "Taxable income is $" << endl;
	cout << "Tax rate is " << person1.getTaxRate()*100 << "%" << endl;
	cout << "Total income tax is $" /*<< person1.getTotalIncome - ((5000+(5000*person.getDep()) + person2.getTotalExemption) */<< endl;

	
}//end main

With thanks to Firefox for its ability to highlight the word salary

Line 27 of Income Member Function Definitions salary += s; You add the user input value s to salary but salary has not been initialised to anything and contains garbage therefore

garbage in = garbage out

With thanks to Firefox for its ability to highlight the word salary

Not the point. And what if the variable name was salry?

I agree and it wasn't so I (or may be Tech E) got lucky :D

I certainly wouldn't have gone hunting for it in that much code without help.

I have tried to initialize salary under the member function setSalary but it doesn't help. I still get garbage. What I don't understand is I had that do while loop in my main function originally and it worked fine but when I moved into my setSalary member function definition I got garbage for my salary total. I moved into my setSalary member function definition because this is supposed to be object oriented design so I assumed that I should not leave a do while loop out in my main function and that it needs to be under an object in class.

I moved into my setSalary member function definition because this is supposed to be object oriented design so I assumed that I should not leave a do while loop out in my main function and that it needs to be under an object in class.

Bad assumption. The setSalary() member function ideally should do only that -- set the salary. No input. No output. No calculations. Just take the parameter and load it into the variable member. This is true for all primitive functions like setters and getters

Well that is why I am a noob and why I joined this site. So that I MIGHT just recieve some help in understanding this object oriented design process better. So, if I create a function called void calculateSalary() would that be more appropiate for the purpose of my program?

Edited 6 Years Ago by Tech E: n/a

Without the input, calculateSalary() sounds like a very useful function.

In my opinion (other opinions welcome), the functions in a class should generally do one thing only. That one thing might be complicated, but still only one thing. If it inputs, that's all it does. If it calculates, that's it. Keep the class functions primitive.

This article has been dead for over six months. Start a new discussion instead.