More on functions.

Here is the scenario:
Write a function named "getInput" that takes one parameter identifying whether the value the user is entering is the wholesale price OR if it is getting a percentage ( i.e. it will be checking the upper bound. )

There should be:
only ONE loop validating a real number with no upper bound
and only ONE loop validating a percentage.

The function returns a real number: the valid value that the user entered. This same function will be called 3 times from "main": one for each input value needed.

The function body should be prompting the user appropriately ( by using the parameter value ), get a value from the user, validate it and if necessary, loop until a good value is entered. Return the good value.

Please include comments preceding each function definition explaining what it does.



Run with the same sets of data that I gave in Lab#6 Part A, but all of them should be done within one run because you are still using the code that you had for Lab#6 Part B ( looping for multiple sets of input data.) Your output will be similar to what you had previously.



You are NOT using pass by reference parameters. The function should NOT be changing the values of the arguments. The function "getInput" only returns ONE value – a real number.
( Remember, NO global variables. )
Based on my understanding, here is what i did so far:

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
49
50
51
52
53
54
55
56
57
58
59
60
  // Rebecca Carolina Katz
// Filename lab_8
//Lab 8

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

double getInput (char variablename);

int main()
{
	cout<<"Rebecca Carolina Katz, Lab8\n";
	
	//variable declarations
	double wholesale_price;
	double sales_tax_rate;
	double mark_up_rate;
	
	//get input
	wholesale_price=getInput('W');
	cout<<wholesale_price;
	
	sales_tax_rate=getInput('S');
	cout<<sales_tax_rate;
	
	mark_up_rate=getInput('M');
	cout<<mark_up_rate;	
}
// This function is taking one parameter that identifies whether the value
// the user is entering is the wholesale price or a percentage to return a real number

double getInput( char variablename)
{
	double varvalue;
	if( variablename=='W')
	{
		case 'W':
			cout<<"Enter Wholesale Cost:";
			cin>> varvalue;
			while (varvalue<=0)
			{
				cout<<"Invalid. Wholesale Cost must be a positive number"<<endl;
				cout<<"Enter Wholesale Cost:";
				cin>>varvalue;
			}
	}
	else
		case 'S':
			cout<< "Enter Percentage:";
			cin>>varvalue;
			while( varvalue<=o || varvalue >100)
			{
				cout<<"Invalid. Percentage must be a positive number and < 100" <<endl;
				cout<< "Enter Sales Tax Rate:";
				cin>> varvalue
			}
	
}  


what do i do with case 'M'?
What you want to do is one or the other and this example demonstrates functional equivalency.

1
2
3
4
5
6
7
8
9
10
11
	// Using switch statements.
	switch ( type ) {
		case 'W':
			break;
		case 'S':
			break;
		case 'M':
			break
		default:
			// Some sort of error code here.
	}

OR

1
2
3
4
5
6
7
8
9
10
11
	if ( type == 'W') {
		
	}
	else if ( type == 'S' ) {
		
	}
	else if ( type == 'M' ) {
		
	}
	else
		// Some sort of error code here. 

It is good practice to trap by some means if operator didn't select W, S or M.

Last edited on
my professor already stated not to use switch. Originally for case 'M' i had the following but she crossed it out:
case 'M':
1
2
3
4
5
6
7
8
9
cout<<" Enter Mark-up rate:";
cin>> varvalue;
 while( varvalue <=0 || varvalue >100)
{
 cout<<"invalid"<<endl;
cout<<"Enter Mark-up Rate:";
cin>> varvalue;
return varvalue:
} break


i do not understand what was wrong with this?
i do not understand what was wrong with this?

Perhaps:
and only ONE loop validating a percentage.

Both your sales_tax rate, and your mark_up rate are percentages.

so how i would i go about this assignment?
Since you state you can't use a switch statement then an if/else would seem appropriate.

Do you know how to do multiple conditions in your control statements?


Is this okay?

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
49
50
51
52
53
54
55
56
57
58
59
60
// Rebecca Carolina Katz
// Filename lab_8
//Lab 8

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

double getInput (char variablename);

int main()
{
	cout<<"Rebecca Carolina Katz, Lab8\n";
	
	//variable declarations
	double wholesale_price;
	double sales_tax_rate;
	double mark_up_rate;
	
	//get input
	wholesale_price=getInput('W');
	cout<<wholesale_price;
	
	sales_tax_rate=getInput('S');
	cout<<sales_tax_rate;
	
	mark_up_rate=getInput('M');
	cout<<mark_up_rate;	
}
// This function is taking one parameter that identifies whether the value
// the user is entering is the wholesale price or a percentage to return a real number

double getInput( char variablename)
{
	double varvalue;
	if( variablename=='W')
	{
		case 'W':
			cout<<"Enter Wholesale Cost:";
			cin>> varvalue;
			while (varvalue<=0)
			{
				cout<<"Invalid. Wholesale Cost must be a positive number"<<endl;
				cout<<"Enter Wholesale Cost:";
				cin>>varvalue;
			}
	}
	else if(variablename=='S' || variablename=='M')
		case 'S':
			cout<< "Enter Percentage:";
			cin>>varvalue;
			while( varvalue<=o || varvalue >100)
			{
				cout<<"Invalid. Percentage must be a positive number and < 100" <<endl;
				cout<< "Enter Sales Tax Rate:";
				cin>> varvalue
			}
  
	
}  
I would just simply remove the cases. As it stands now it seems like the case statements would break the program.

the if and else if statements do the same thing as the case so both is not necessary, in fact having both will not make the program work properly if your lucky or it might even have a compiler error.

However it looks like you are on the right track. Keep it up! :D

- Hirokachi
so how would you write the program then? to me it doesnt make any sense to remove the cases...for example she wants something like if(variablename=='W). I think she also wants the cases because she did not say anything about removing them
You need to remove the case statements because:
my professor already stated not to use switch.

And case statements are part of switch statements. All you need to do is remove the line with the case statement.

ok so how is 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <cmath>
using namespace std;

// Declaring functions outside of int main
double getInput (char);
void showResults(double ,  double ,  double,double,double,double );
double calcMarkUp (double , double );
double calcSalesTax (double , double , double );
double calcRetailPrice(double , double ,
					double );




int main()
{
	cout<<"Rebecca Carolina Katz, Lab8\n";
	
	//variable declarations
	double wholesale_price;
	double sales_tax_rate;
	double mark_up_rate;
	double Mark_Up_Amount;
  	double Sales_Tax_Amount;
  	double Retail_Price;
  	char run_the_program_again;
	
	do 
	{ 
		
		//get input
		wholesale_price=getInput('W');
		//cout<<wholesale_price;
		
		sales_tax_rate=getInput('S');
		//cout<<sales_tax_rate;
		
		mark_up_rate=getInput('M');
		//cout<<mark_up_rate;	
		
		Mark_Up_Amount = calcMarkUp(wholesale_price, mark_up_rate ) ;
	  	
	  	Sales_Tax_Amount = calcSalesTax(wholesale_price, Mark_Up_Amount, sales_tax_rate);
		
	    Retail_Price = calcRetailPrice(wholesale_price, Mark_Up_Amount, Sales_Tax_Amount);
	  	
	   	showResults( wholesale_price,   mark_up_rate,   sales_tax_rate, Mark_Up_Amount, Sales_Tax_Amount, Retail_Price);
	   	
	   	cout << "Do you want to run this program again? y or n\n";
	    cin >> run_the_program_again;
	         
   } while (run_the_program_again =='y' || run_the_program_again =='Y' );
       
       cout << "\nGOODBYE\n";
}

void showResults(double wholesale_price,  double mark_up_rate,  double sales_tax_rate,double Mark_Up_Amount,double Sales_Tax_Amount, double Retail_Price)
  {  	
  	
  	cout << "Inside Show Results\n\n";
  	
  	
  	cout << "WholeSale Price : " ;
	cout <<  wholesale_price << endl; 
	
	cout << "\n\nMark Up Rate : ";
	cout << mark_up_rate << endl;
	
	cout << "\n\nSales Tax Rate : ";
	cout << sales_tax_rate << endl;
	
  	cout << "\n\nMark Up Amount\n" ;
	cout << Mark_Up_Amount << endl;

	
	cout << "\n\nSales Tax Amount\n" ;
	cout << Sales_Tax_Amount << endl;
	
  	
  	cout << "\n\nRetail Price Amount\n" ;
	cout << Retail_Price << endl;



}// end of show result

// This function is taking one parameter that identifies whether the value
// the user is entering is the wholesale price or a percentage to return a real number

double getInput( char variablename)
{
	double varvalue;
	if( variablename=='W')
	{
		cout<<"Enter Wholesale Price:";
		cin>> varvalue;
		while (varvalue<=0)
		{
			cout<<"Invalid. Wholesale Cost must be a positive number"<<endl;
			cout<<"Enter Wholesale Price:";
			cin>>varvalue;
		}
	}
	else 
		{
			cout<< "Enter Percentage:";
			cin>>varvalue;
			while( varvalue<=0|| varvalue >100)
			{
				cout<<"Invalid. Percentage must be a positive number and < or = 100" <<endl;
				cout<< "Enter Percentage:";
				cin>> varvalue;
			}
		}
		
	return varvalue;
} 
// This function is calculating the mark up amount.
double calcMarkUp (double wholesale_price, double mark_up_rate)
{
	//Variable declarations	
	double Mark_Up_Amount;
	
	cout << "\n\nInside calcMarkUp\n\n";


	Mark_Up_Amount=wholesale_price*(mark_up_rate/100);


	return Mark_Up_Amount;
}
  
// This function is calculating the sales tax amount
double calcSalesTax (double wholesale_price, double Mark_Up_Amount, double sales_tax_rate)
{
	//Variable declarations	
	double Sales_Tax_Amount;
	
	cout << "\n\nInside calcSalesTax\n\n";


	Sales_Tax_Amount=(wholesale_price+Mark_Up_Amount)*(sales_tax_rate/100);


	return Sales_Tax_Amount;
}


// This function is calculating the retail price  
double calcRetailPrice(double wholesale_price, double Mark_Up_Amount,
					double Sales_Tax_Amount)
{
	//Variable declarations
	double Retail_Price;
	
	cout << "\n\nInside calcRetailPrice\n\n";


	Retail_Price=(wholesale_price+Mark_Up_Amount+Sales_Tax_Amount);


	return Retail_Price;
} 
I guess the question for you would be, if this is the result your looking for then your app is spot on.

Rebecca Carolina Katz, Lab8
Enter Wholesale Price:Enter Percentage:Enter Percentage:

Inside calcMarkUp



Inside calcSalesTax



Inside calcRetailPrice

Inside Show Results

WholeSale Price : 391.72


Mark Up Rate : 35


Sales Tax Rate : 5


Mark Up Amount
137.102


Sales Tax Amount
26.4411


Retail Price Amount
555.263
Do you want to run this program again? y or n

GOODBYE
Topic archived. No new replies allowed.