I need help initializing/calling with functions

I didn't finish my lab for my class and couldn't get a chance to ask my professor for some help. My book only has pseudocode and not c++ examples. I think I was getting an intialization error before (not showing up now) and I don't know how to call the functions. I want to only call the "printData" function so the information is outputted.

9/22/2014 (figured out myself)

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

using namespace std;

void calcCounty(double &TotalSales, double &CountySalesTax);
void calcState(double &TotalSales, double &StateSalesTax);
void calcTotal(double &TotalSales, double &CountySalesTax, double &StateSalesTax);
void printData(double &TotalSales, double &CountySalesTax, double &StateSalesTax);

double TotalSales;
double CountySalesTax;
double StateSalesTax;

int main()
{

	cout << "Tax Calculation program" << endl;
	cin >> TotalSales;

	return 0;
}

void calcCounty(double &TotalSales, double &CountySalesTax)
{
	CountySalesTax = TotalSales * 0.4;
}

void calcState(double &TotalSales, double &StateSalesTax)
{
	StateSalesTax = TotalSales * 0.2;
}

void calcTotal(double &TotalSales, double &CountySalesTax, double &StateSalesTax)
{
	TotalSales = CountySalesTax + StateSalesTax;
}

void printData(double &TotalSales, double &CountySalesTax, double &StateSalesTax)
{
	cout << TotalSales, CountySalesTax, StateSalesTax;
}
Last edited on
bump
You must be using a pretty bad book for it to not have example of function calls.

To call a function you must type the function name and its required arguments.

To call printData

printData(TotalSales,CountySalesTax,StateSalesTax);

Also in your printData function you are incorrectly printing multiple values. It is done like so,
cout<<TotalSales<<" "<<CountySalesTax<<" "<<StateSalesTax;

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
#include <iostream>
#include <iomanip>

double county_sales_tax( double nett_sales );
double state_sales_tax( double nett_sales );
double total_amount( double nett_sales, double county_tax, double state_tax ) ;
void print_invoice( double nett_sales, double county_tax, double state_tax ) ;

int main()
{
    std::cout << "Tax Calculation program\nnett sales? " ;
    double nett_sales ;
    std::cin >> nett_sales ;

    if( nett_sales > 0 )
    {
        const double county_tax = county_sales_tax(nett_sales) ;
        const double state_tax = state_sales_tax(nett_sales) ;
        print_invoice( nett_sales, county_tax, state_tax ) ;
    }
}

double county_sales_tax( double nett_sales )
{
    const double rate = 0.4 ;
    return nett_sales * rate ;
}

double state_sales_tax( double nett_sales )
{
    const double rate = 0.2 ;
    return nett_sales * rate ;
}

double total_amount( double nett_sales, double county_tax, double state_tax )
{
    return nett_sales + county_tax + state_tax ;
}

void print_invoice( double nett_sales, double county_tax, double state_tax )
{
    // see: https://stdcxx.apache.org/doc/stdlibug/28-3.html#2832
    std::cout << "   nett sales: " << std::setw(10) << std::fixed << std::setprecision(2) << nett_sales << '\n'
              << " + county tax: " << std::setw(10) << county_tax << '\n'
              << " +  state tax: " << std::setw(10) << state_tax << '\n'
              << "-------------------------\n"
              << " total amount: " << std::setw(10) << total_amount( nett_sales, county_tax, state_tax ) << '\n' ;
}
this is still frustrating me. why isn't TotalSales being affected?

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

using namespace std;

double calcCounty(double &TotalSales, double &CountySalesTax);
double calcState(double &TotalSales, double &StateSalesTax);
double calcTotal(double &TotalSales, double &CountySalesTax, double &StateSalesTax);
void printData(double &TotalSales, double &CountySalesTax, double &StateSalesTax);

double TotalSales;
double CountySalesTax;
double StateSalesTax;

int main()
{

	cout << "Tax Calculation program" << endl;
	cin >> TotalSales;
	printData(TotalSales,CountySalesTax,StateSalesTax);
	return 0;
}

double calcCounty(double &TotalSales)
{
	const double tax = 0.4;
	return TotalSales * tax;
}

double calcState(double &TotalSales, double &StateSalesTax)
{
	const double tax = 0.2;
	return TotalSales * tax;
}

double calcTotal(double &TotalSales, double &CountySalesTax, double &StateSalesTax)
{
	return TotalSales = CountySalesTax + StateSalesTax;
}

void printData(double &TotalSales, double &CountySalesTax, double &StateSalesTax)
{
	cout <<TotalSales<<""<<CountySalesTax<<""<< StateSalesTax;
}
Topic archived. No new replies allowed.