0

I am trying to write a program using functions that calculates county tax, sales tax, adds them together, and outputs them in main. I have also made a printData function so I could output everything at once but i'm confused on how to use it, because of an initialization error in run time. I am not very good with function and was hoping I could get some help.

Here is my written code: (updated)

#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;
    printData(TotalSales, CountySalesTax, StateSalesTax);
    cout << TotalSales << CountySalesTax << StateSalesTax;
    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;
}
3
  • 2
    Having functions returning values is easier to understand Sep 21, 2014 at 19:04
  • 3
    But you never call your functions? Sep 21, 2014 at 19:04
  • 1
    You also need to read up on the comma operator, because the output in printData will not be what you expect. Sep 21, 2014 at 19:05

2 Answers 2

0

Why don't you rather return a value from your functions?

double calcCounty(double& totalSales) {
return totalSales * 0.4;
}

Then in your main do:

countySalesTax = calcCounty(totalSales);
0

You need to initialize your variables first (the ones you need).

For example, this function:

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

should have TotalSales initialized before been called.

As suggested by the comments, you also need to call the functions at some point.

Moreover, the printing should be done like this in your case:

cout << TotalSales << ", " << CountySalesTax << ", " << StateSalesTax;

First, I would suggest you to read this example:

#include <iostream>

using namespace std;

/*
 * Write a program, which will declare two variables, 'a' and 'b'.
 * They will be initialized with the values 10 and 100, respectively.
 * Write a function 'int find_min(int a, int b)`, which will find
 * which of the given parameters is less and will return it.
 *
 * Then, write `void find_min_no_return(int a, int b, int& min)`,
 * which will do the same job as `find_min()`, but with no return
 * statement.
 */

int find_min(int a, int b) {
  if(a < b) {
    return a;
  }
  else {
    return b;
  }
}

/*
 * 'min' is passed by reference because it is
 * going to be modified. 'a' and 'b' are passed
 * by value.
 */
void find_min_no_return(int a, int b, int& min) {
  if(a < b) {
    min = a;
  }
  else {
    min = b;
  }
}

int main() {
  int a = 10;
  int b = 100;

  int min;

  min = find_min(a, b);
  cout << "min of first function called = " << min << endl;

  find_min_no_return(a, b, min);
  cout << "min of second function called = " << min << endl;

  return 0;
}

and then solve your problem.

2
  • i'm still really lost. i've spent so much time trying to understand this but i'm still confused. apparently got rid of the initilization error. I tried adding this function call in main but get nothing back: printData(TotalSales, CountySalesTax, StateSalesTax);
    – user4041267
    Sep 22, 2014 at 0:47
  • I want you @VishantKhunti to modify your code and update your question, after you have read my example.
    – gsamaras
    Sep 22, 2014 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.