This is homework that my professor assigned. What he wants us to do is make a program that finds the highest sales among four burger joints in my area. Thats simple enough trying to find the highest sales, but my problem lies when he asked us to make one of the function parameters a variable of type String.
Heres an example code, cause I don't want to let everyone know where I live in this post:
#include <iostream>
usingnamespace std;
double getBurgerSales(string);
void findHighest(double, double, double, double);
int main()
{
string joint;
getBurgerSales(joint);
}
//Somehow this function will get burger sales with use of string as an argument
double getBurgerSales(string joint){
double joint1, joint2, joint3, joint4;
//getting sales for each burger join below
cout << "Enter sales for Joint 1 last month: ";
cin >> joint1;
cout << joint1;
cout << "\nEnter sales for Joint 2 last month: ";
cin >> joint2;
cout << joint2;
cout << "\nEnter sales for Joint 3 last month: ";
cin >> joint3;
cout << joint3;
cout << "\nEnter sales for Joint 4 last month: ";
cin >> joint4;
cout << joint4;
findHighest(joint1, joint2, joint3, joint4);
return 0;
}
void findHighest(double joint1, double joint2, double joint3, double joint4){
//checking whether joint1 is the highest with nested if statements
if(joint1 > joint2 && joint1 > joint3 && joint1 > joint4){
cout << endl << "Joint 1 has the most sales!";
}
//checking whether joint2 is the highest with nested if statements
elseif (joint2 > joint1 && joint2 > joint3 && joint2 > joint4){
cout << endl << "Joint 2 has the most sales!";
}
//checking whether joint3 is the highest with nested if statements
elseif(joint3 > joint1 && joint3 > joint2 && joint3 > joint4){
cout << endl << "Joint 3 has the most sales!";
}
//checking whether joint4 is the highest with nested if statements
elseif(joint4 > joint1 && joint4 > joint2 && joint4 > joint3){
cout << endl << "Joint 4 has the most sales!";
}
}
After this code, I definitely get the highest sales amount. I just don't know how my professor wants me to implement the string parameter. He gave us an example that looked something like this when calling the functions:
double getBurgerSales(string joint){
double joint1, joint2, joint3, joint4;
//getting sales for each burger join below
cout << "Enter sales for Joint 1 last month: ";
cin >> joint1;
cout << joint1;
cout << "\nEnter sales for Joint 2 last month: ";
cin >> joint2;
cout << joint2;
cout << "\nEnter sales for Joint 3 last month: ";
cin >> joint3;
cout << joint3;
cout << "\nEnter sales for Joint 4 last month: ";
cin >> joint4;
cout << joint4;
//Here is what im talking about
findHighest(joint1"Joint1", joint2"Joint2", joint3"Joint3", joint4"Joint4");
return 0;
}
Here is my output without putting in string into the function call for findHighest():
1 2 3 4 5 6 7 8 9 10 11 12
Enter sales for Joint 1 last month: 6521
6521
Enter sales for Joint 2 last month: 33
33
Enter sales for Joint 3 last month: 1656
1656
Enter sales for Joint 4 last month: 755
755
Joint 1 has the most sales!
Process returned 0 (0x0) execution time : 5.414 s
Press any key to continue.
Help would be appreciated! I just want to understand what he wants with the string parameter! Thanks!
//Somehow this function will get burger sales with use of string as an argument
double getBurgerSales(string joint){
double sales;
std::cout << "\nEnter sales for " << joint << " last month: " ;
std::cin >> sales ;
return sales;
}
I don't think I understand how that would work, cire. I would only be able to input one sales number? Would I input the joint name in the function call? I want to be able to enter in four sales numbers, but can't seem to see how to do that with your edit.
I also want to be able to find the highest sales number, so having only one number entered would not allow me to do that? I appreciate the help!
getBurgerSales gets the sales for one joint per call. If you want to get more than one joint's sales, you call it more than one time. If you want to compare the results of those calls, you store the return values in the calling code and compare them there.
Thanks, cire! I finally got it! It took a while to try to understand it, but I got it. My professor wanted one function call in the main. I didn't realize calling a function within a function also worked. Here is my finished code:
#include <iostream>
usingnamespace std;
double getBurgerSales(string);
void findHighest(double, double, double, double);
int main()
{
//Calling findHighest() once also calls getBurgerSales() four times
findHighest(getBurgerSales("Joint 1"), getBurgerSales("Joint 2"),
getBurgerSales("Joint 3"), getBurgerSales("Joint 4"));
return 0;
}
//Somehow this function will get burger sales with use of string as an argument
double getBurgerSales(string joint){
double sales;
//getting sales for each city below
cout << "\nEnter sales for " << joint << " last month: ";
cin >> sales;
cout << sales;
return sales;
}
void findHighest(double joint1, double joint2, double joint3, double joint4){
//checking whether joint1 has the highest sales
if(joint1 > joint2 && joint1 > joint3 && joint1 > joint4){
cout << endl << "Joint 1 has the most sales!";
}
////checking whether joint2 has the highest sales
elseif (joint2 > joint1 && joint2 > joint3 && joint2 > joint4){
cout << endl << "Joint 2 has the most sales!";
}
//checking whether joint3 has the highest sales
elseif(joint3 > joint1 && joint3 > joint2 && joint3 > joint4){
cout << endl << "Joint 3 has the most sales!";
}
//checking whether joint4 has the highest sales
elseif(joint4 > joint1 && joint4 > joint2 && joint4 > joint3){
cout << endl << "Joint 4 has the most sales!";
}
}
This works perfectly! Well, as far as I'm concerned.
Also, thanks Diedes! I did something similar to that at first, but instead used counters in a while loop. I made it so that the while loop would stop once the counter got to 4. That isn't what my professor wanted. He only wanted one function call in the main. I really appreciate the help, though! You guys are awesome!
Oh, well I think he meant what I have on there. So, having those function calls within a function call is considered calling 5 functions? Is there a name for placing functions inside a function argument?
auto a = getBurgerSales("Joint 1");
auto b = getBurgerSales("Joint 2");
auto c = getBurgerSales("Joint 3");
auto d = getBurgerSales("Joint 4");
findHighest(a, b, c, d);
except that in the first snippet, the getBurgerSales function invocations can occur in any order, whereas in the second the order is guaranteed.
Oh, wow. That actually makes a lot of sense! I was wondering why the orders of the joints were not occurring the way I wanted them to. I would get them in a random order, or as random as they can get. This is good information to know for later.