Hello again,

My assignment is to calculate a 10% bonus based on sales figures. I have to use a main() function, obviously, and 3 void functions, getSales(), calcBonus(), and displayBonus().

I'm pretty sure I have the functions coded correctly, I'm just having some issues getting them to actually run correctly within the structure of my main() function.

Here's the code so far.

//Ch10AppE03.cpp
//Calculates and displays a bonus amount
//Created/revised by Greg Schader on 7/6/2009

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
void getSales();
void calcBonus(double, double);
void displayBonus(double);

int main()
{	
  	//declare constant and variables
	const double RATE = .1;
	double sales = 0.0;
	double bonus = 0.0;
	
	//get input item
	getSales();
	//calculate bonus

	//display output item
	cout << fixed << setprecision(2);

    return 0;
}   //end of main function

//*****function definitions*****
void getSales()
{
	double sales = 0.0;

	while (sales != 0)
	{
		double salesTotal = 0.0;
		cout << "Enter the sales amount (0 to stop): ";
		cin >> salesTotal;

		sales = sales + salesTotal;
	}
}

void calcBonus(const double RATE, double sales)
{
	double bonus = 0.0;
	
	bonus = sales * RATE;

}

void displayBonus(double bonus)
{

	cout << "Bonus is: $" << bonus << endl;

}

The getSales() function shouldn't be void - it needs to return the sales!

That's what I thought as well, but the textbooks says that getSales(), calcBonus(), and displayBonus() should all be void functions... = /

Well that's not gona work unless you make everything global, which is a TERRIBLE idea. displayBonus() wont work for the same reason unless you call it from calcBonus(). I duno what to tell you - the assignment is just wrong i guess :(

Ignoring the return issue for the moment...
Sales will fall right through the loop!

double sales = 0.0; while (sales != 0)

You set it to zero, then loop while not zero!

Use a

do {


  } while (sales != 0 );

Switched to a do while, now everything workds except for my displayBonus() function.

//Ch10AppE03.cpp
//Calculates and displays a bonus amount
//Created/revised by Greg Schader on 7/6/2009

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
void getSales(double, double);
void calcBonus(double, double);
void displayBonus(double);

int main()
{	
  	//declare constant and variables
	const double RATE = .1;
	double sales = 0.0;
	double bonus = 0.0;
	double salesTotal = 0.0;
	
	//get input item
	getSales(sales, salesTotal);

	//calculate bonus
	calcBonus(sales, RATE);

	//display output item
	cout << fixed << setprecision(2)<< displayBonus(double);

    return 0;
}   //end of main function

//*****function definitions*****
void getSales(double sales, double salesTotal)
{
	do{
		cout << "Enter the sales amount (0 to stop): ";
		cin >> sales;

		salesTotal = salesTotal + sales;
	}	while (sales != 0);
}

void calcBonus(const double RATE, double sales)
{
	double bonus = 0.0;
	
	bonus = sales * RATE;

}

void displayBonus(double bonus)
{

	cout << "Bonus is: $" << bonus << endl;

}

Displays these errors:

------ Build started: Project: Ch10AppE03 Project, Configuration: Debug Win32 ------
Compiling...
Ch10AppE03.cpp
c:\documents and settings\greg\desktop\83618-4\cpp5\chap10\ch10appe03 solution\ch10appe03 project\ch10appe03.cpp(34) : error C2144: syntax error : 'double' should be preceded by ')'
c:\documents and settings\greg\desktop\83618-4\cpp5\chap10\ch10appe03 solution\ch10appe03 project\ch10appe03.cpp(34) : error C2660: 'displayBonus' : function does not take 0 arguments
c:\documents and settings\greg\desktop\83618-4\cpp5\chap10\ch10appe03 solution\ch10appe03 project\ch10appe03.cpp(34) : error C2059: syntax error : ')'
Build log was saved at "file://c:\Documents and Settings\Greg\Desktop\83618-4\Cpp5\Chap10\Ch10AppE03 Solution\Ch10AppE03 Project\Debug\BuildLog.htm"
Ch10AppE03 Project - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

oh oh, what was I thinking hahaha, you can "return" the values by reference! What you'll need to do is put a '&' here:

void getSales(double &sales, double salesTotal)

and the same with bonus:

void calcBonus(const double RATE, double sales, double &bonus)

global variables ARE a bad idea, which is why it's not the only way to accomplish this with a void function. Try passing things by reference:

void getSales(double &sales); //try this instead
//other functions
 
int main()
{
       //declare constant and variables
       const double RATE = .1;
       double sales = 0.0;
       double bonus = 0.0;
 
       getSales(sales);

       //sales is now whatever you calculated it to be from inside getSales

       //blah blah blah
}

make sure you change your function appropriately (should be simple) and also implement wildgoose's suggestion of a do-while loop. Hope that's helpful

~J

Well I don't need to return any values other than the bonus amount. So would I make my displayBonus function like

void displayBonus(double &bonus)

?

Well I don't need to return any values other than the bonus amount. So would I make my displayBonus function like

void displayBonus(double &bonus)

?

no, since you are not altering 'bonus' in displayBonus, you don't need to pass it by reference, you also don't need to return it, you're only displaying what it is.

This looked like a good explanation of what passing by reference means if you are a bit confused as to what is actually going on:
http://www.tech-recipes.com/rx/1232/c-pointers-pass-by-value-pass-by-reference/

give that a look over and then you can see when it's needed and when it's not.

~J

To make your assignment work you need to be able to pass data between your functions and the main program. To do so requires one of two things:

a) A return statement... meaning the functions can't be void. That's not part of the assignment so let's forget about this.

b) To use the "&" symbol with your functions. If you use the & then any variables that you change in the function will also be changed in the main program. So something like this:

void calcBonus(double &variable1, double variable2)
{
/* Swap in the correct calculation of course */
variable1 += variable2;
}

OK, I think I get the passing by reference now.

Updated code:

//Ch10AppE03.cpp
//Calculates and displays a bonus amount
//Created/revised by Greg Schader on 7/6/2009

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
void getSales(double, double);
void calcBonus(double, double);
void displayBonus(double);

int main()
{	
  	//declare constant and variables
	const double RATE = .1;
	double sales = 0.0;
	double bonus = 0.0;
	double salesTotal = 0.0;
	
	//get input item
	getSales(sales, salesTotal);

	//calculate bonus
	calcBonus(sales, RATE);

	//display output item
	cout << fixed << setprecision(2) << displayBonus(bonus) << endl;

    return 0;
}   //end of main function

//*****function definitions*****
void getSales(double sales, double salesTotal)
{
	do{
		cout << "Enter the sales amount (0 to stop): ";
		cin >> sales;

		salesTotal = salesTotal + sales;
	}	while (sales != 0);
}

void calcBonus(const double RATE, double &sales)
{
	double bonus = 0.0;
	
	bonus = sales * RATE;

}

void displayBonus(double &bonus)
{

	cout << "Bonus is: $" << bonus << endl;

}

Only error turning up is a big one. If you want to see it, I can post it, but its massive. It's on line 34 with my cout << displayBonus(bonus) << endl;

Alright, I got rid of the error, but now the bonus amount is returning only as $0.00.

I don't think I have my reference points right on the bonus variable.

//Ch10AppE03.cpp
//Calculates and displays a bonus amount
//Created/revised by Greg Schader on 7/6/2009

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
void getSales(double, double);
void calcBonus(double &, double, double &);
void displayBonus(double &);

int main()
{	
  	//declare constant and variables
	const double RATE = .1;
	double sales = 0.0;
	double bonus = 0.0;
	double salesTotal = 0.0;
	
	//get input item
	getSales(sales, salesTotal);

	//calculate bonus
	calcBonus(sales, RATE, bonus);

	//display output item
	displayBonus(bonus);

    return 0;
}   //end of main function

//*****function definitions*****
void getSales(double sales, double salesTotal)
{
	do{
		cout << "Enter the sales amount (0 to stop): ";
		cin >> sales;

		salesTotal = salesTotal + sales;
	}	while (sales != 0);
}

void calcBonus(double &sales, const double RATE, double &bonus)
{
	
	bonus = sales * RATE;

}

void displayBonus(double &bonus)
{

	cout << fixed << setprecision(2) << "Bonus is: $" << bonus << endl;

}

Try this since you are required to use void functions!
Dereference salesTotal

void getSales(double sales, double &salesTotal)
{
     do{
              cout << "Enter the sales amount (0 to stop): ";
              cin >> sales;
               salesTotal = salesTotal + sales;
    }  while (sales != 0);
}

Why would I want to do that? I thought I had the whole value vs. reference figured out, but I guess I'm still lost.

Also changed:

void calcBonus(double salesTotal, const double RATE)
{
	double bonus = 0.0;

	bonus = salesTotal * RATE;

}

Your input loop has no method to return your salesTotal accumulation so you need to dereference salesTotal. Also I don't understand why you're passing sales in since its actually entered!
So...

void getSales( double &salesTotal)
{
    double sales;
    salesTotal = 0.0;


   do{
      cout << "Enter the sales amount (0 to stop): ";
      cin >> sales;
      salesTotal = salesTotal + sales;
   } while (sales != 0);
}

But your bonus calculation is wrong too! Your input is sales and your output is bonus! You aren't alteriing sales so it shouldn't be dereferenced!

void calcBonus(double sales, const double RATE, double &bonus)
{
   bonus = sales * RATE;
}

Think of dereferencing as passing a pointer but it looks like its a full data type.

pointer method

double f;
func( &f );

void func( double *fltptr )
{
    *fltptr = 123.45;
}

dereference method

double f;

func( f );   // not really passing the value of f! is really passing the address of f!

void func( double &f )
{
     f = 123.45;
}

So I pass by reference when a value is going to be modified?

And if it stays the same I leave it alone right?

Hmm! if changing your post please indicate so in reason for editting!

Yes,

void displayBonus( double bonus );

It was the data entry and bonus calculation functions that were wrong.

Ok, updated code. It runs and returns a valid bonus answer. Everything looks good right? I'm pretty sure all my references are right this time.

//Ch10AppE03.cpp
//Calculates and displays a bonus amount
//Created/revised by Greg Schader on 7/6/2009

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
void getSales(double &);
void calcBonus(double, double, double &);
void displayBonus(double);

int main()
{	
  	//declare constant and variables
	const double RATE = .1;
	double sales = 0.0;
	double bonus = 0.0;
	double salesTotal = 0.0;
	
	//get input item
	getSales(salesTotal);

	//calculate bonus
	calcBonus(salesTotal, RATE, bonus);

	//display output item
	displayBonus(bonus);

    return 0;
}   //end of main function

//*****function definitions*****
void getSales(double &salesTotal)
{
    double sales;
    salesTotal = 0.0;
 
 
   do{
      cout << "Enter the sales amount (0 to stop): ";
      cin >> sales;
      salesTotal = salesTotal + sales;
   } while (sales != 0);
}

void calcBonus(double salesTotal, const double RATE, double &bonus)
{

	bonus = salesTotal * RATE;

}

void displayBonus(double bonus)
{

	cout << fixed << setprecision(2) << "Bonus is: $" << bonus << endl;

}

Looks good. But in actual development you would return a value. Needing to return 2nd to Nth values would involve passing pointers or dereferencing.

Something to keep in mind about dereferencing. From the calling function's viewpoint you don't realize that you're actually passing the pointer and not the value, unless you remember that's the way it works. Always a good idea to put a comment referring to passing the reference on the function call!

commented: Thanks for your help! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.