Can you please help me with this quesstion and this is the exam problem. I am wr
ID: 3779612 • Letter: C
Question
Can you please help me with this quesstion and this is the exam problem.
I am writing a c++ program and i need help with sorts the structure array by number of each drink in the machine.
.
Here is my code:
#include
#include
#include
#include
using namespace std;
//stroing data into struct and const value
const int SIZE = 5;
struct drinkInfo
{
string drinkName;
double DrinkCost;
int numOfDrinks;
};
drinkInfo machine[SIZE] = { { "Cola", .75, 20 }, { "RootBeer", .75, 20 },
{ "LemonLime", .75, 20 }, { "GradeSoda", .80, 20 },
{ "CreamSoda", .80, 20 } };
//functions protypate and creating each soda type
void Cola(double &payments);
void RootBeer(double &);
void lemon(double &);
void grape(double &);
void cream(double &);
void menu(int &);
void sortString(drinkInfo machine[], int size);
int mian()
{
//delcaring variable
int choice = 6;
int userPurchase;
double totalEran = 0;
string drink;
bool drinkSoldOut = true;
//uisng while loop for the amount of drinks slodout
while (drinkSoldOut)
{
//using while loop to display the drink list
cout << " ---------------------------------------------------------------" << endl;
cout << " Number " << "DrinkName " << " Cost " << "Quantity left" << endl;
cout << " ---------------------------------------------------------------" << endl;
//using for loop to dislpay the tabel to user
for (int i = 0; i < SIZE; i++)
{
cout << " " << (i + 1) << setw(20) << machine[i].drinkName << " "
<< machine[i].DrinkCost << " " << machine[i].numOfDrinks << endl;
}
cout << choice + 1 << ". Quit" << endl;
menu(userPurchase);
//using switch case for nothing left in machine
switch (userPurchase)
{
case 1:
if (machine[0].numOfDrinks > 0)
Cola(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 2:
if (machine[1].numOfDrinks > 0)
lemon(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 3:
if (machine[1].numOfDrinks > 0)
lemon(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 4:
if (machine[1].numOfDrinks > 0)
grape(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 5:
if (machine[1].numOfDrinks > 0)
cream(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 6:
sortString(machine, SIZE);
case 7:
break;
};
}
cout << showpoint << fixed << setprecision(2);
cout << "Total amount of money that machine earned for the day: $" << totalEran << endl;
return 0;
}
void menu(int &drink)
{
bool userChoice;
//asking user for drink choice
cout << "What kind of soda that you would like to purchase (Select number 1 to 6): ";
cin >> drink;
//using switch case for user choice
switch (drink)
{
case 1:
userChoice = true;
break;
case 2:
userChoice = true;
break;
case 3:
userChoice = true;
break;
case 4:
userChoice = true;
break;
case 5:
userChoice = true;
break;
case 6:
userChoice = true;
break;
case 7:
userChoice = true;
break;
default:
userChoice = true;
};
//using while loop to check if user enter vaild number
while (userChoice != true)
{
cout << "!!!Invild number that you select. Try again, rembember you are allow number 1-7" << endl
<< "Re-enter soda that you would like to purchase (Select number 1 to 7): ";
cin >> drink;
//inside while loop using switch case if user enter vaild number
switch (drink)
{
case 1:
userChoice = true;
break;
case 2:
userChoice = true;
break;
case 3:
userChoice = true;
break;
case 4:
userChoice = true;
break;
case 5:
userChoice = true;
break;
case 6:
userChoice = true;
case 7:
userChoice = true;
break;
default:
userChoice = true;
};
}
}
void Cola(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected Cola and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop for calculotr the price
while (billInstill < machine[0].DrinkCost)
machine[0].numOfDrinks;
payments += machine[0].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[0].DrinkCost << endl;
}
void RootBeer(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected root-Beer and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
machine[1].numOfDrinks--;
billInstill += machine[1].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[1].DrinkCost << endl;
}
void lemon(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected lemon and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
machine[2].numOfDrinks--;
billInstill += machine[2].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[2].DrinkCost << endl;
}
void grape(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected grape and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
machine[3].numOfDrinks--;
billInstill += machine[3].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[3].DrinkCost << endl;
}
void cream(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected grape and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
machine[4].numOfDrinks--;
billInstill += machine[4].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[4].DrinkCost << endl;
}
void sortString(drinkInfo machine[], int size)
{
//delcaring the varibale in function proytype
int minCount;
drinkInfo minValue;
//using for loop
for (int i = 0; i < (size - 1); i++)
{
minCount = i;
//starting value from minvalue
minValue = machine[i];
//using for loop to find the minValue
for (int j = i + 1; j < size; j++)
{
if (machine[j].numOfDrinks < minValue.numOfDrinks)
{
minCount = j;
minValue = machine[j];
}
}
machine[minCount] = machine[i];
machine[i] = minValue;
}
}
/* Sample output
*/
Explanation / Answer
// Corrected it.
#include<iostream>
#include<iomanip>
using namespace std;
//stroing data into struct and const value
const int SIZE = 5;
struct drinkInfo
{
string drinkName;
double DrinkCost;
int numOfDrinks;
};
drinkInfo machine[SIZE] = { { "Cola", .75, 20 }, { "RootBeer", .75, 20 },
{ "LemonLime", .75, 20 }, { "GradeSoda", .80, 20 },
{ "CreamSoda", .80, 20 } };
//functions protypate and creating each soda type
void Cola(double &payments);
void RootBeer(double &);
void lemon(double &);
void grape(double &);
void cream(double &);
void menu(int &);
void sortString(drinkInfo machine[], int size);
int main()
{
//delcaring variable
int choice = 6;
int userPurchase;
double totalEran = 0;
string drink;
bool drinkSoldOut = true;
//uisng while loop for the amount of drinks slodout
while (drinkSoldOut)
{
//using while loop to display the drink list
cout << " ---------------------------------------------------------------" << endl;
cout << " Number " << "DrinkName " << " Cost " << "Quantity left" << endl;
cout << " ---------------------------------------------------------------" << endl;
//using for loop to dislpay the tabel to user
for (int i = 0; i < SIZE; i++)
{
cout << " " << (i + 1) << setw(20) << machine[i].drinkName << " "
<< machine[i].DrinkCost << " " << machine[i].numOfDrinks << endl;
}
cout << choice + 1 << ". Quit" << endl;
menu(userPurchase);
//using switch case for nothing left in machine
switch (userPurchase)
{
case 1:
if (machine[0].numOfDrinks > 0)
Cola(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 2:
if (machine[1].numOfDrinks > 0)
lemon(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 3:
if (machine[1].numOfDrinks > 0)
lemon(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 4:
if (machine[1].numOfDrinks > 0)
grape(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 5:
if (machine[1].numOfDrinks > 0)
cream(totalEran);
else
cout << "Sorry, we are currently outStock for this drink. Please select another soda. " << endl;
break;
case 6:
sortString(machine, SIZE);
break;
case 7:
exit(1);
};
}
cout << showpoint << fixed << setprecision(2);
cout << "Total amount of money that machine earned for the day: $" << totalEran << endl;
return 0;
}
void menu(int &drink)
{
bool userChoice;
//asking user for drink choice
cout << "What kind of soda that you would like to purchase (Select number 1 to 5): ";
cin >> drink;
//using switch case for user choice
switch (drink)
{
case 1:
userChoice = true;
break;
case 2:
userChoice = true;
break;
case 3:
userChoice = true;
break;
case 4:
userChoice = true;
break;
case 5:
userChoice = true;
break;
case 6:
userChoice = true;
break;
case 7:
userChoice = true;
break;
default:
userChoice = true;
};
//using while loop to check if user enter vaild number
while (userChoice != true)
{
cout << "!!!Invild number that you select. Try again, rembember you are allow number 1-7" << endl
<< "Re-enter soda that you would like to purchase (Select number 1 to 7): ";
cin >> drink;
//inside while loop using switch case if user enter vaild number
switch (drink)
{
case 1:
userChoice = true;
break;
case 2:
userChoice = true;
break;
case 3:
userChoice = true;
break;
case 4:
userChoice = true;
break;
case 5:
userChoice = true;
break;
case 6:
userChoice = true;
case 7:
userChoice = true;
break;
default:
userChoice = true;
};
}
}
void Cola(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected Cola and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop for calculotr the price
while (billInstill < machine[0].DrinkCost)
machine[0].numOfDrinks--;
payments += machine[0].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[0].DrinkCost << endl;
}
void RootBeer(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected root-Beer and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
machine[1].numOfDrinks--;
billInstill += machine[1].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[1].DrinkCost << endl;
}
void lemon(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected lemon and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
machine[2].numOfDrinks--;
billInstill += machine[2].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[2].DrinkCost << endl;
}
void grape(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected grape and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
machine[3].numOfDrinks--;
billInstill += machine[3].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[3].DrinkCost << endl;
}
void cream(double &payments)
{
//delcaring variable for bill
double billInstill;
//displying meesage to user for uer choice and anking for the payament
cout << " You selected grape and total price for would be: $";
cin >> billInstill;
//using while loop for vaild inout
while (billInstill < 0)
{
cout << "Insuffcient bill that your enter. (Hints, machine does not accepted negative)" << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
//using while loop again for 1
while (billInstill > 1)
{
cout << "Machine does not accpet money greater than 1." << endl
<< "Re-enter your bills: ";
cin >> billInstill;
}
machine[4].numOfDrinks--;
billInstill += machine[4].DrinkCost;
//giving refurnd to user
cout << "Your change will be: $" << payments - machine[4].DrinkCost << endl;
}
void sortString(drinkInfo machine[], int size)
{
//delcaring the varibale in function proytype
int minCount;
drinkInfo minValue, temp;
//using for loop
for (int i = 0; i < (size - 1); i++)
{
minCount = i;
//starting value from minvalue
minValue = machine[i];
//using for loop to find the minValue
for (int j = i + 1; j < size; j++)
{
if (machine[j].numOfDrinks < minValue.numOfDrinks)
{
minCount = j;
minValue = machine[j];
}
}
if(minCount != i){
temp = machine[minCount];
machine[minCount] = machine[i];
machine[i] = temp;
}
}
}
/* Sample output
*/