Having trouble with passby value

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//*******************************************************************************************************
double getMarkupPercentage();
double getWholeSale();
double calculateRetail(double wholeSaleCost, double markUpPercentage);
void displayRetail(double retailPrice);
//*******************************************************************************************************
int main()
{
	
	string line(40, '*');
	double wholeSaleCost,
		markUpPercentage,
		totalPrice,
		retailPrice;
	
	do
	{
		cout << line << endl;
		getWholeSale();
		getMarkupPercentage();
		calculateRetail(); // Asking for argument
		displayRetail(); // Asking for argument
		
	} while (wholeSaleCost = -1);

	
	return 0;
}
double getWholeSale()
{
	double wholeSaleCost;

	do
	{
		cout << "Please enter a whole sale price:" << endl;
		cout << "(-1 to quit the program): ";
		cin >> wholeSaleCost;
		if (wholeSaleCost < -1)
		{
			cout << "INCORRECT: whole sale cannot be less than -1" << endl;
			cout << "Please enter a whole sale price:" << endl;
			cout << "(-1 to quit the program): ";
			cin >> wholeSaleCost;
		}
	} while (wholeSaleCost < -1);

	return wholeSaleCost;
}
double getMarkupPercentage()
{
	double markUpPercentage;
	
	do 
	{
		cout << "Please enter a markup percentage: ";
		cin >> markUpPercentage;
		if (markUpPercentage < 0 || markUpPercentage > 100)
		{
			cout << "INCORRECT: percentage cannot  be less than 0 or more than 100" << endl;
			cout << "Please enter a markup percentage: ";
			cin >> markUpPercentage;
		}
	} while (markUpPercentage <= 0);

	return markUpPercentage;
}
double calculateRetail(double wholeSaleCost, double markUpPercentage)
{
	const double RETAIL_PRICE = wholeSaleCost * (100 + markUpPercentage) / 100.0;

	return RETAIL_PRICE;
}
void displayRetail(double retailPrice)
{
	double wholeSaleCost,
		markUpPercentage;

	cout << "Retial price will be: " << '$' << fixed << setprecision(2) << calculateRetail() //asking for argument << endl;
}


//I don't really understand what I should be putting within these parameter so this program can work, I tried putting some of the variables within them, but that doesn't work
Last edited on
amputate your left hand, then knock at your neighbour's door and see his
¿does he still have it?
awesome, that's how local variables work

¿what do you expect this to do?
1
2
3
4
getWholeSale();
getMarkupPercentage();
calculateRetail();
displayRetail();
Last edited on
Those are function calls into the main function, I'm just confused what I should put in the parameter since I need arguments within them.
Hello BruhImSkill,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Rather than talking about the whole program check the comments I made:
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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;  // <--- Best not to use.

//****************************** Prototypes *************************************************************
double getMarkupPercentage();
double getWholeSale();
double calculateRetail(double wholeSaleCost, double markUpPercentage);
void displayRetail(double retailPrice);
//*******************************************************************************************************

int main()
{

	string line(40, '*');
	double wholeSaleCost,
		markUpPercentage,
		totalPrice,
		retailPrice;

	do
	{
		cout << line << endl;
		wholeSaleCost = getWholeSale();  // <--- These three need to deal with a return value.
		getMarkupPercentage();
		calculateRetail(wholeSaleCost, markUpPercentage); // Asking for argument. This is the way it should be.
		displayRetail(retailPrice); // Asking for argument. This is the way it should be.

	} while (wholeSaleCost = -1);  // <--- This assigns "-1" to wholeSaleCost. What you want is "==" to compare.


	return 0;
}

double getWholeSale()
{
	double wholeSaleCost;

	do
	{
		cout << "Please enter a whole sale price:" << endl;
		cout << "(-1 to quit the program): ";
		cin >> wholeSaleCost;

		// <--- Check here for "-1" and return it to main if it is.

		if (wholeSaleCost < -1)  // <--- This will not work. Even a decimal nmber is greater than zero. This would work better as a while loop. "while (wholeSaleCost <= 0)".
		{
			cout << "INCORRECT: whole sale cannot be less than -1" << endl;
			cout << "Please enter a whole sale price:" << endl;
			cout << "(-1 to quit the program): ";
			cin >> wholeSaleCost;

			// <--- Check here for "-1" and return it to main if it is.
		}
	} while (wholeSaleCost < -1);  // <--- Would work better as less than zero.

	return wholeSaleCost;
}

double getMarkupPercentage()
{
	double markUpPercentage;

	do
	{
		cout << "Please enter a markup percentage: ";
		cin >> markUpPercentage;

		if (markUpPercentage < 0 || markUpPercentage > 100) // <--- Again would work better as a while loop.
		{
			cout << "INCORRECT: percentage cannot be less than 0 or more than 100" << endl;
			cout << "Please enter a markup percentage: ";
			cin >> markUpPercentage;
		}
	} while (markUpPercentage <= 0);

	return markUpPercentage;
}

double calculateRetail(double wholeSaleCost, double markUpPercentage)
{
	const double RETAIL_PRICE = wholeSaleCost * (100 + markUpPercentage) / 100.0;  // <--- Done well, but this does not need to be a constant.

	return RETAIL_PRICE;
}

void displayRetail(double retailPrice)
{
	double wholeSaleCost,
		markUpPercentage;

	//std::cout << std::fixed << std::showpoint << std::setprecision(2);

	cout << "Retial price will be: " << '$' << fixed << setprecision(2) << calculateRetail() /*asking for argument*/ << endl;  // <--- See comments in main.
}

The first two prototypes need to look like the last two. Not the exact parameters, but the concept. and the function definitions need to match the prototypes. Now in the prototypes only the type of variable needs to be there. As you did in the last two prototypes I like to add the variable names. In my VS IDE this can be a big help when writing the function calls.

The actual function calls only need the variable name that is to be passed to the function. See lines 28 and 29.

In the "calculateRetail" function the variable "RETAIL_PRICE" does not need to be a constant. However it is defined it will be lost when the function ends and goes out of scope.

In the "displayRetail" function the commented line (line 95) I added will work better for you (I have this set up to just click and paste). This line only needs done once, and it could be done in main, and will affect all "cout" statements that follow until something is changed. The "std::showpoint" tells the "cout" to print ".00" should it happen. This helps when aligning the numbers on the screen.

How I will finish the fixes to your program and see if I find anything else.

Hope that helps,

Andy
Topic archived. No new replies allowed.