Help on Passing by Value/Reference

My class partner and I are having trouble with passing by reference and/ or passing by values. We decided to put our variables in global and the code works perfectly, but the professor didn't like the way we did that. He said we had to use pass by reference and/ or pass by value. My partner and I are having trouble using it. Can you guys please help?

#include <iostream>
#include <iomanip>

using namespace std;

void menu();
void greeting();
void results();
double calc();
const double MildSalsa=1.99, MediumSalsa=2.99, SweetSalsa=4.99, HotSalsa=3.99, ZestySalsa=7.99;

double calcMild, calcMedium, calcSweet, calcHot, calcZesty, finalCalc; //Professor said to use Passing by value/ Passing by reference
int plusMild, plusMedium, plusSweet, plusHot, plusZesty, userChoice, howmanyJars;

int main()
{
greeting();
menu();
while(userChoice!=6)
{
calc();
menu();
}
results();
}

void greeting()
{
cout << "Welcome to Miami Salsa Factory" << endl;
cout << "Please choose from the following options: " <<endl;
}

void menu()
{
cout<< "1. Mild Salsa" << setw(15)<< "$"<< MildSalsa <<endl;
cout<< "2. Medium Salsa"<< setw(13)<< "$"<< MediumSalsa<<endl;
cout<< "3. Sweet Salsa" << setw(14)<< "$"<< SweetSalsa <<endl;
cout<< "4. Hot Salsa" << setw(16)<< "$"<< HotSalsa <<endl;
cout<< "5. Zesty Salsa" << setw(14)<< "$"<< ZestySalsa <<endl;
cout<< "6. Exit" <<endl;
cout<< "Your Choice: ";
cin >> userChoice;
while(userChoice<1 || userChoice>6 || cin.fail())
{
cout << " Wrong Input please use only integers between 1 and 6 " << endl;
cin.clear();
cin.sync();
cout<< "Your Choice: ";
cin >> userChoice;
}
}

double calc()
{
cout << "How many jars would you like to purchase: ";
howmanyJars=0;
cin >> howmanyJars;
while(howmanyJars<0 || cin.fail())
{
cout << " Wrong Input please use only integers, and do not enter negative numbers " << endl;
cin.clear();
cin.sync();
cout<< "How many jars would you like to purchase: ";
cin >> howmanyJars;
}
switch(userChoice)
{
case 1:
calcMild += howmanyJars * MildSalsa;
plusMild += howmanyJars;
break;
case 2:
calcMedium += howmanyJars * MediumSalsa;
plusMedium += howmanyJars;
break;
case 3:
calcSweet += howmanyJars * SweetSalsa;
plusSweet += howmanyJars;
break;
case 4:
calcHot += howmanyJars * HotSalsa;
plusHot += howmanyJars;
break;
case 5:
calcZesty += howmanyJars * ZestySalsa;
plusZesty += howmanyJars;
break;
case 6:
break;
}
}
void results()
{
finalCalc = calcMild + calcMedium + calcSweet + calcHot + calcZesty;
cout << "\nThank you for using Salsa Factory Application" << endl;
cout << "# Jars SalsaType Unit $ SubTotal\n";
cout << setw(8) << left << plusMild ; cout << " Mild Salsa Jars @ $1.99 ..... $ " << calcMild <<endl;
cout << setw(8) << left << plusMedium ; cout << " Medium Salsa Jars @ $2.99 ..... $ " << calcMedium<<endl;
cout << setw(8) << left << plusSweet ; cout << " Sweet Salsa Jars @ $4.99 ..... $ " << calcSweet <<endl;
cout << setw(8) << left << plusHot ; cout << " Hot Salsa Jars @ $3.99 ..... $ " << calcHot <<endl;
cout << setw(8) << left << plusZesty ; cout << " Zesty Salsa Jars @ $7.99 ..... $ " << calcZesty <<endl;
cout << setw(8) << left << "Your total is $: " << finalCalc << endl;
}
Last edited on
Please edit your post and put code in code block. It is unreadable thus way
Do you know how to use functions and send it arguments?
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

#include <iostream>
#include <iomanip>

using namespace std;

void menu();
void greeting();
void results();
double calc();
const double MildSalsa=1.99, MediumSalsa=2.99, SweetSalsa=4.99, HotSalsa=3.99, ZestySalsa=7.99;

double calcMild, calcMedium, calcSweet, calcHot, calcZesty, finalCalc; //Professor said to use Passing by value/ Passing by reference 
int plusMild, plusMedium, plusSweet, plusHot, plusZesty, userChoice, howmanyJars;

int main()
{
greeting();
menu();
while(userChoice!=6)
{
calc();
menu();
}
results();
}

void greeting()
{
cout << "Welcome to Miami Salsa Factory" << endl;
cout << "Please choose from the following options: " <<endl;
}

void menu()
{
cout<< "1. Mild Salsa" << setw(15)<< "$"<< MildSalsa <<endl;
cout<< "2. Medium Salsa"<< setw(13)<< "$"<< MediumSalsa<<endl;
cout<< "3. Sweet Salsa" << setw(14)<< "$"<< SweetSalsa <<endl;
cout<< "4. Hot Salsa" << setw(16)<< "$"<< HotSalsa <<endl;
cout<< "5. Zesty Salsa" << setw(14)<< "$"<< ZestySalsa <<endl;
cout<< "6. Exit" <<endl;
cout<< "Your Choice: ";
cin >> userChoice;
while(userChoice<1 || userChoice>6 || cin.fail())
{
cout << " Wrong Input please use only integers between 1 and 6 " << endl;
cin.clear();
cin.sync();
cout<< "Your Choice: ";
cin >> userChoice;
}
}

double calc()
{
cout << "How many jars would you like to purchase: ";
howmanyJars=0;
cin >> howmanyJars;
while(howmanyJars<0 || cin.fail())
{
cout << " Wrong Input please use only integers, and do not enter negative numbers " << endl;
cin.clear();
cin.sync();
cout<< "How many jars would you like to purchase: ";
cin >> howmanyJars;
}
switch(userChoice)
{
case 1:
calcMild += howmanyJars * MildSalsa;
plusMild += howmanyJars;
break;
case 2:
calcMedium += howmanyJars * MediumSalsa;
plusMedium += howmanyJars;
break;
case 3:
calcSweet += howmanyJars * SweetSalsa;
plusSweet += howmanyJars;
break;
case 4:
calcHot += howmanyJars * HotSalsa;
plusHot += howmanyJars;
break;
case 5:
calcZesty += howmanyJars * ZestySalsa;
plusZesty += howmanyJars;
break;
case 6:
break;
}
}
void results()
{
finalCalc = calcMild + calcMedium + calcSweet + calcHot + calcZesty;
cout << "\nThank you for using Salsa Factory Application" << endl;
cout << "# Jars SalsaType Unit $ SubTotal\n";
cout << setw(8) << left << plusMild ; cout << " Mild Salsa Jars @ $1.99 ..... $ " << calcMild <<endl;
cout << setw(8) << left << plusMedium ; cout << " Medium Salsa Jars @ $2.99 ..... $ " << calcMedium<<endl;
cout << setw(8) << left << plusSweet ; cout << " Sweet Salsa Jars @ $4.99 ..... $ " << calcSweet <<endl;
cout << setw(8) << left << plusHot ; cout << " Hot Salsa Jars @ $3.99 ..... $ " << calcHot <<endl;
cout << setw(8) << left << plusZesty ; cout << " Zesty Salsa Jars @ $7.99 ..... $ " << calcZesty <<endl;
cout << setw(8) << left << "Your total is $: " << finalCalc << endl;
}
I'm way too tired to go into detail right now but there is plenty of great resources explaining this,

https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

I think it would be better in this situation to pass by reference rather than pass by value.
Topic archived. No new replies allowed.