# Getting void function to work

• (2 Pages)
• 1
• 2

## 28 Replies - 2679 Views - Last Post: 30 October 2010 - 10:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=197603&amp;s=7d77acf60e378c45dd3992435433fb51&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 120
• Joined: 30-October 10

# Getting void function to work

Posted 30 October 2010 - 07:06 PM

```#include<iostream>

using namespace std;

double salesTax, subTotal;
bool isFresno = false;
double num, sum;
void calculateTax(double subtotal, bool isFresno, double& salesTax);

int main()
{

cout<<"Enter number(s): ";
cin>>num;

while (num != -999)
{
sum = sum + num;
cin>>num;
double subTotal = sum;

}

if (subTotal > 100)
isFresno = true;

void calculateTax(subTotal, isFresno, salesTax);//
cout<<"salesTax = "<<salesTax<<endl;//

return 0;
}

void calculateTax(sum, isFresno, salesTax)
{

if (isFresno = true)
salesTax = subTotal * (.0775 + .001)
else
salesTax = subTotal * .0775

}

```

This post has been edited by no2pencil: 30 October 2010 - 07:08 PM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: Getting void function to work

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3832
• Posts: 13,971
• Joined: 08-August 08

## Re: Getting void function to work

Posted 30 October 2010 - 07:13 PM

Since you haven't indicated what is happening that is wrong, I'm going to assume that you expect salesTax to be altered but it isn't changing. That would be because you're passing a value and not a reference.

Oh, and you've got a problem with "sum" too!

### #3 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Getting void function to work

Posted 30 October 2010 - 07:41 PM

These are the errors the code produces
```DIC.cpp: In function ‘int main()’:
DIC.cpp:21: warning: unused variable ‘subTotal’
DIC.cpp:29: error: variable or field ‘calculateTax’ declared void
DIC.cpp: At global scope:
DIC.cpp:36: error: variable or field ‘calculateTax’ declared void

```

This is line 29
void calculateTax(subTotal, isFresno, salesTax);//
Is that how your textbook says you should call a function?

This is line 36
void calculateTax(sum, isFresno, salesTax)
Is that how your textbook says you should declare a function?

Reputation: 0
• Posts: 120
• Joined: 30-October 10

## Re: Getting void function to work

Posted 30 October 2010 - 07:47 PM

/*Write the code for a method named calculateTax that accepts
a double parameter named subTotal and a boolean parameter
named isFresno and calculates and returns the sales tax.
If isFresno is false, the sales tax should .0775.
If it's true, the sales tax should be increased by .001.
If the subTotal is less than or equal to zero,
an IllegalArgumentException should be thrown with the message
"Subtotal must be greater than zero."*/

#include<iostream>

using namespace std;

void calculateTax(double subtotal, bool isFresno, double& salesTax);

int main()
{
double salesTax=0.0;
bool isFresno = false; //assigning the value of false
double num=0.0, sum=0.0;

cout<<"Enter number(s): ";
cin>>num;

while (num != -999)
{
sum = sum + num;
cin>>num;
}
if (sum > 100.0)
isFresno = true; //if sum is greater than 100, then//
//change value of isFresno to true//
calculateTax(sum, isFresno, salesTax);
cout<<"salesTax is "<<salesTax<<endl;

return 0;
}

void calculateTax(double subTotal, bool isFresno, double& salesTax)
{
int rate1 = (.0775 + .001);
int rate2 = .0775;
if (isFresno == true)
{
salesTax = subTotal * rate1;
cout<<"rate1 is "<<rate1<<endl;
}
else
{
salesTax = subTotal * rate2;
cout<<"rate2 is "<<rate2<<endl;
}

}

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3832
• Posts: 13,971
• Joined: 08-August 08

## Re: Getting void function to work

Posted 30 October 2010 - 07:49 PM

Two posts and you couldn't bother to read the rules either time!
[/end help]

Reputation: 0
• Posts: 120
• Joined: 30-October 10

## Re: Getting void function to work

Posted 30 October 2010 - 07:52 PM

The problem I am having with the code is that it is not giving me the answers that I am expecting. Not sure how to correct its implementation.

### #7 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Getting void function to work

Posted 30 October 2010 - 07:54 PM

What CTphpnwb was talking about is that you failed to wrap your code in code tags.

Please edit your posting (see the "EDIT" button in lower right). (If Edit is not available make a new posting on this thread and skip ( a ) below.)
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.

You have told us now an idea of the problem you are having but we need a few more details
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Ideally copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
( e ) What have you already tried to fix it?

This post has been edited by janotte: 30 October 2010 - 07:55 PM

### #8 jimblumberg

Reputation: 5835
• Posts: 17,791
• Joined: 25-December 09

## Re: Getting void function to work

Posted 30 October 2010 - 07:56 PM

nquadr, on 30 October 2010 - 07:52 PM, said:

The problem I am having with the code is that it is not giving me the answers that I am expecting. Not sure how to correct its implementation.

Jim

Reputation: 0
• Posts: 120
• Joined: 30-October 10

## Re: Getting void function to work

Posted 30 October 2010 - 08:11 PM

/*Write the code for a method named calculateTax that accepts
a double parameter named subTotal and a boolean parameter
named isFresno and calculates and returns the sales tax.
If isFresno is false, the sales tax should .0775.
If it's true, the sales tax should be increased by .001.
If the subTotal is less than or equal to zero,
an IllegalArgumentException should be thrown with the message
"Subtotal must be greater than zero."*/

```#include<iostream>

using namespace std;

void calculateTax(double subtotal, bool isFresno, double& salesTax);

int main()
{
double salesTax=0.0;
bool isFresno = false;  //assigning the value of false
double num=0.0, sum=0.0;

cout<<"Enter number(s): ";
cin>>num;

while (num != -999)
{
sum = sum + num;
cin>>num;
}
if (sum > 100.0)
isFresno = true;  //if sum is greater than 100, then//
//change value of isFresno to true//
calculateTax(sum, isFresno, salesTax);
cout<<"salesTax is "<<salesTax<<endl;

return 0;
}

void calculateTax(double subTotal, bool isFresno, double& salesTax)
{
int rate1 = (.0775 + .001);
int rate2 = .0775;
if (isFresno == true)
{
salesTax = subTotal * rate1;
cout<<"rate1 is "<<rate1<<endl;
}
else
{
salesTax = subTotal * rate2;
cout<<"rate2 is "<<rate2<<endl;
}

}
```

( I am not sure how to copy from ide, I am just copying and pasting from visual studio 2008, I am totally brand new to programming.

© My code does compile but it gives me errors, the values for rate and salesTax are both zero.

### #10 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Getting void function to work

Posted 30 October 2010 - 08:18 PM

Okay.
Why are you getting 'num' from cin in 2 different places?

```    cout<<"Enter number(s): ";
cin>>num;

while (num != -999)
{
sum = sum + num;
cin>>num;
}

```

Move the first collection of 'num' inside the while() loop and get rid of this line
cin>>num;
inside the while() loop.

EDIT
Give us a list of 3 numbers to enter into the program so we are all testing the same input data.

This post has been edited by janotte: 30 October 2010 - 08:19 PM

Reputation: 0
• Posts: 120
• Joined: 30-October 10

## Re: Getting void function to work

Posted 30 October 2010 - 08:27 PM

```#include<iostream>

using namespace std;

void calculateTax(double subtotal, bool isFresno, double& salesTax);

int main()
{
double salesTax=0.0;
bool isFresno = false;  //assigning the value of false
double num=0.0, sum=0.0;

cout<<"Enter number(s): ";

while (num != -999)
{
cin>>num;
sum = sum + num;
cin>>num;
}
if (sum > 100.0)
isFresno = true;  //if sum is greater than 100, then//
//change value of isFresno to true//
calculateTax(sum, isFresno, salesTax);
cout<<"salesTax is "<<salesTax<<endl;

return 0;
}

void calculateTax(double subTotal, bool isFresno, double& salesTax)
{
int rate1 = (.0775 + .001);
int rate2 = .0775;
if (isFresno == true)
{
salesTax = subTotal * rate1;
cout<<"rate1 is "<<rate1<<endl;
}
else
{
salesTax = subTotal * rate2;
cout<<"rate2 is "<<rate2<<endl;
}

}

```

(a) Here is the code with the changes you recommended
( I used the input of 50 100 200 and -999

### #12 jimblumberg

Reputation: 5835
• Posts: 17,791
• Joined: 25-December 09

## Re: Getting void function to work

Posted 30 October 2010 - 08:30 PM

In the following code:

```void calculateTax(double subTotal, bool isFresno, double& salesTax)
{
int rate1 = (.0775 + .001);
int rate2 = .0775;

```

Jim

### #13 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Getting void function to work

Posted 30 October 2010 - 08:41 PM

nquadr, on 31 October 2010 - 12:27 PM, said:

Here is the code with the changes you recommended

Not quite.

What I meant was.
```#include<iostream>

using namespace std;

void calculateTax(double subtotal, bool isFresno, double& salesTax);

int main()
{
double salesTax=0.0;
bool isFresno = false;  //assigning the value of false
double num=0.0, sum=0.0;

while (num != -999)
{
cout<<"Enter number(s): ";
cin>>num;
sum = sum + num;
}
```

Do you see why that is better than what you did?

This post has been edited by janotte: 30 October 2010 - 08:42 PM

Reputation: 0
• Posts: 120
• Joined: 30-October 10

## Re: Getting void function to work

Posted 30 October 2010 - 08:52 PM

```#include<iostream>

using namespace std;

void calculateTax(double subtotal, bool isFresno, double& salesTax);

int main()
{
L1:
double salesTax=0.0;
bool isFresno = false;  //assigning the value of false
double num=0.0, sum=0.0;

cout<<"Enter number(s): ";

while (num != -999)
{
cin>>num;
sum = sum + num;
}
if (sum > 100.0)
isFresno = true;  //if sum is greater than 100, then//
//change value of isFresno to true//
calculateTax(sum, isFresno, salesTax);
cout<<"salesTax is "<<salesTax<<endl<<endl;
goto L1;

return 0;
}

void calculateTax(double subTotal, bool isFresno, double& salesTax)
{
double rate1 = (.0775 + .001);
double rate2 = .0775;
if (isFresno == true)
{
salesTax = subTotal * rate1;
cout<<"rate1 is "<<rate1<<endl;
}
else
{
salesTax = subTotal * rate2;
cout<<"rate2 is "<<rate2<<endl;
}

}

```

(a) I am assuming getting rid of cin statement was because it was repetitive.

( Unfortunately, the error I am getting is that it is always giving me rate2, and the sales tax is negative. I can only assume that the negative is coming from the sentinel value, but I do not know how to exclude that from the list of values.

This post has been edited by nquadr: 30 October 2010 - 08:54 PM

### #15 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Getting void function to work

Posted 30 October 2010 - 08:56 PM

nquadr, on 31 October 2010 - 12:52 PM, said:

I do not know how to exclude that from the list of values.

How about you wrap this line
sum = sum + num;
in an if() test and only add 'num' to the sum if it is not the sentinel?