Functions

Here is the scenerio:
Part A.
Modifying Lab #6 to use a void function ( i.e. it will not return anything.)

First, fix anything you did wrong in Lab#6 to be done correctly.
E.g. using the wrong kind of loops
put in blank lines for readability
break up long "cout" statements into multiple lines

Write a function named "showResults" that will print this message first:

Inside showResults

Then do all of the calculations and printing of output. I.e. put all of that code into the function body of "showResults" and take it all out of "main".

The input values will need to be passed in from "main" into "showResults".
The function "showResults" returns no value.
The input values should NOT be changed inside the function "showResults".

Change "main" to get valid input from a user, call the function "showResults", then after the function executes, "main" asks the user about running again for another set of data.

Run your program with the same values that you used for Lab#6 Part B.
No global variables, must pass parameters!
This is what i had from part b:

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

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

int main()
{
	std::cout <<"Rebecca Carolina Katz, lab 6 part b\n" ; // endl;;

	// Variable declarations
	/* float */ double wholesale_cost ; // cost; // *** favour double as the default floating point type
	/* float */ double Mark_up_percentage;
	/* float */ double Sales_tax_rate;

	char run_the_program_again ; // *** added 

	do
	{
	   do
       { 
	    cout << "Enter Wholesale Cost:";
        cin >> wholesale_cost;
        if (wholesale_cost <= 0) 
        cout << "Invalid.Wholesale cost must be > 100 and a positive number" << endl;
       } while (wholesale_cost <=0) ;
       
  
       do
      {  
	     cout << "Enter Mark up percentage:";
         cin >> Mark_up_percentage;
         if ((Mark_up_percentage  > 1) || (Mark_up_percentage < 0.0))
         cout << "Invalid.Markup percentage must be < or equal to 100 and must be positive" << endl;
      } while ((Mark_up_percentage > 1) || (Mark_up_percentage < 0.0)) ;
  
       do
      {
	     cout << "Enter sales tax rate:";
        cin >> Sales_tax_rate;
          if ((Sales_tax_rate > 0.15) || ( Sales_tax_rate <= 0.00)) 
        cout << "Invalid.Sales tax rate must be > 0.15 and must be positive" << endl;
      } while ((Sales_tax_rate > 0.15) || (Sales_tax_rate <= 0.00)) ;
Last edited on
T
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
his is what i did so far:

[include <iostream>
#include <cmath>>
using namespace std;

int main ()
{
	cout << "Rebecca Carolina Katz, Lab 7 part A";
	
	// Variable declarations
	double wholesale_cost;
	double Mark_up_percentage;
	double Sales_tax_rate;
	void showResults;
	char run_the_program_again; // *** added
}]
[/code]
Last edited on
It looks like part of your code in your original message was truncated. Can you repost it?

I think they are saying that if your original program looks like this:
1
2
3
4
5
stuff
int main()
{
   code for main
}


then you should start by changing it to this:
1
2
3
4
5
6
7
8
9
10
stuff
void showResult()
{
    code for main
}

int main()
{
    showResult();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>>
using namespace std;

int main ()
{
	cout << "Rebecca Carolina Katz, Lab 7 part A";
	
	// Variable declarations
	double wholesale_cost;
	double Mark_up_percentage;
	double Sales_tax_rate;
	void showResults;
	char run_the_program_again; // *** added
	
	void showResults()
	{
		
	}
}


I do not understand what is being asked..
Get rid of the "void showResults" within main(), int first place too, as that says you're trying to create an instance of "void" which is not-allowed. Secondly, when you write a function, it needs to be outside the body of all other functions, including main() so embellished a little, it would look more like this:

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
//Your directions indicate this func will need parameters, prolly will need all 3 pieces obtained in main.
void ShowResults(double wholesale, double markup, double salesTax)
{
   //TO DO: Implement more content...
   cout << "Inside ShowResults function\n";

}

int main()
{
	cout << "Rebecca Carolina Katz, Lab 7 part A\n";
	
	// Variable declarations
	double wholesale_cost;
	double Mark_up_percentage;
	double Sales_tax_rate;
	char run_the_program_again; // *** added

       do {
         //Read in the doubles...
	 //TODO: Other content here...
         ShowResults( wholesale_cost, Mark_up_percentage, Sales_tax_rate );

         cout << "Do you want to run this program again? y or n\n";
         cin >> run_the_program_again;
       } while(run_the_program_again == 'y');
}
I do not understand what is being asked..

In lab #6, you wrote a program that does a bunch of stuff. That program runs ONCE and finishes.

In this new assignment, you are taking that code and putting it into a function that can be called. You will then call it MULTIPLE TIMES to get different results with different input. In other words, the code from lab #6 will become a building block for the new program.
Okay.The values from lab#6 were:
Cost: $50.00 Mark-up percentage: 10% Sales tax rate: 5%
Cost: $250.00 Mark-up percentage: 45% Sales tax rate: 12%
Cost: $25.00 Mark-up percentage: 5% Sales tax rate: 10%
Cost: $100.00 Mark-up percentage: 8% Sales tax rate: 3%


how would i do this for the output? I have to have calculations and i always got that wrong..
The way to approach this is to look at what you're doing and see how you can "factor out" what's in common. In this case, the cost, mark up and sales tax change, and the rest remains the same, so showResults probably looks like this:
1
2
3
4
5
void showResults(double cost, double markUp, double salesTaxRate)
{
   // calculate the values and print them out
}
Now your main program can just call this:
1
2
3
showResults(50, 0.10, 0.05);
showResults(250, 0.45, 0.12);
showResults(100, 0.08, 0.03);


The code that you posted for prompting can also be factored out. Write a function that prompts the user for a number and checks if the number is within a range:

1
2
3
4
5
6
double promptForDouble(const char *prompt, double lowerBound, double upperBound)
{
    // print the prompt and then get a double from the user, if
    // lowerBound <= value <= upperBound then return it. Otherwise loop back and
    // prompt again.
}

Now you can get the values like this:
1
2
3
wholesale_cost = promptForDouble("Enter Wholesale Cost: ", 0.00001, 1E100);
mark_up_percentage = promptForDouble("Enter Mark up percentage:", 0, 1);
sales_tax_rate = promptForDouble("Enter sales tax rate:", 0,1);


Any time you find yourself writing the same sort of code several times, you should think about how you could write a function to factor out the common code.
Topic archived. No new replies allowed.