Chips and Salsa using header file - c++

Revise Programming Challenge 3 to use an array of Product objects instad of two parallel arrays. The Product class will need member variables to hold a product name and a quantity.
Challenge 3: Write a program that lets a maker of chips and salsa keep track of their sales for five different types of salsa they produce: mild, medium, sweet, hot, and zesty. It should use two parallel five-element arrays: an array of strings that holds five salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using an initialization list at the time the name array is created. The program should prompt the user to enter the number of jars sold for each type. One this sales data has been entered, the program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products.
I did Challenge 3, code here.
After much trying, I can't seem to get said program to work. I've commented the errors, and what they are. Here's what I've got so far:
#ifndef SALSA_H
#define SALSA_H
class Salsa
{
private:
void getTotal();
void getHigh();
void getLow();
int count;
int total;
int high;
int low;
int flavor;
public:
void getSold();
};
#endif
#include "Salsa.h"
#include <iostream>
#include <string>
using namespace std;
void Salsa::getSold()
{
for (count = 0; count < 5; count++)
{
cout << "Jar sold last month of ";
cout << count + 1;
cin >> flavor[count]; //Get error saying subscript array or pointer type
while (flavor[count] <= 0) // Get error saying subscript array or pointer type
cout << "Jars sold must be greater than or equal to 0.";
cout << "Re-enter jars sold for last month ";
cin >> flavor[count];
cout << endl;
}
}
Salsa::getTotal();
Salsa::getHigh();
Salsa::getLow();
}
void Salsa::getTotal()
total = 0;
for (count = 0; count < 5; count++)
total += flavor[count];
cout << "Total Sales: " << total << endl;
}
void Salsa::getHigh()
{
highest = flavor[0];
int index = 1;
for (count = 0; count < 5; count++)
if (flavor[count] > highest)
{
highest = flavor[count];
index = count + 1;
}
cout << "High Seller: " << flavor << endl;
}
void Salsa::getLow()
{
lowest = flavor[0];
int index = 1;
for (count = 0; count < 5; count++)
if (flavor[count] < lowest)
{
lowest = flavor[count];
index = count + 1;
}
cout << "Low Seller: " << flavor << endl;
}
#include "Salsa.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int SALS_FLAV = 5;
string flavor[SALS_FLAV] = { "mild", "medium", "sweet", "hot", "zesty" };
Salsa sold;
for (int index = 0; index < SALS_FLAV; index++)
getSold(flavor[SALS_FLAV]); // Get error saying 'getSold' identifier not found
sold.getSold();
return 0;
}

There are many issues with your code. The following code may do what you want.
You should study it and try to improve it. One such improvement could be the use of std::vector as opposed to arrays. This will mean you can avoid manual memory management (new/delete) and therefore achieve the ideal of not having to define a destructor.
#include <iostream>
#include <string>
using namespace std;
class Salsa
{
public:
Salsa(string* flavours, int num_flavours);
~Salsa();
void getSold();
private:
void getTotal();
void getHigh();
void getLow();
string* flavours_;
int num_flavours_;
int* sold_count_;
};
Salsa::Salsa(string* flavours, int num_flavours)
{
num_flavours_ = num_flavours;
flavours_ = new string[num_flavours_];
sold_count_ = new int[num_flavours_];
int i;
for(i = 0; i < num_flavours_; i++)
{
flavours_[i] = flavours[i];
}
}
Salsa::~Salsa()
{
delete[] flavours_;
delete[] sold_count_;
}
void Salsa::getSold()
{
int count;
int num;
for (count = 0; count < num_flavours_; count++)
{
cout << "Jar sold last month of " << flavours_[count] << " ";
cin >> num;
while(num <= 0)
{
cout << "Jars sold must be greater than or equal to 0." << endl;
cout << "Re-enter jars sold for last month " << endl;
cin >> num;
}
sold_count_[count] = num;
}
getTotal();
getHigh();
getLow();
}
void Salsa::getTotal()
{
int count;
int total = 0;
for (count = 0; count < num_flavours_; count++)
total += sold_count_[count];
cout << "Total Sales: " << total << endl;
}
void Salsa::getHigh()
{
int count;
int highest = sold_count_[0];
int index = 0;
for (count = 0; count < num_flavours_; count++)
{
if (sold_count_[count] > highest)
{
highest = sold_count_[count];
index = count;
}
}
cout << "High Seller: " << flavours_[index] << endl;
}
void Salsa::getLow()
{
int count;
int lowest = sold_count_[0];
int index = 0;
for (count = 0; count < num_flavours_; count++)
{
if (sold_count_[count] < lowest)
{
lowest = sold_count_[count];
index = count;
}
}
cout << "Low Seller: " << flavours_[index] << endl;
}
int main()
{
const int SALS_FLAV = 5;
string flavor[SALS_FLAV] = { "mild", "medium", "sweet", "hot", "zesty" };
Salsa sold(flavor, SALS_FLAV);
sold.getSold();
return 0;
}

Related

Your C++ program must use functions to input the scores, compute the average, and find the index of the highest score

I've been working on this for hours now and I'm almost done. I can't get the program to display the correct student ID.
Also the "highIndex" function is suppose to be an "int" but I started with a double. When I try to change it, everything else seems to fall apart.
How do I get the high score to be associated with the student ID so I can output it correctly? I also need to highIndex to be an int which means some other things needs to be changed.
Any help would be much appreciated :)
Here's what I have so far
void inputAnswers(int studentIds[], double scores[]);
double getAverage(double scores[])
{
double total = 0;
for (int count = 0; count <= 6; count++)
{
total += scores[count];
}
return total / 7;
}
double highIndex(double score[])
{
double highScore = score[0];
double indexHigh = 1;
for (int count = 0; count <= 6; count++)
{
if (score[count] > highScore)
{
highScore = score[count];
indexHigh = count;
}
}
return highScore;
}
int main()
{
const int ids = 7;
int student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 };
double scores[7];
double highScore[7];
// Gets the test score from user
inputAnswers(student, scores);
// Calculates the average
cout << "The average score is " << getAverage(scores) << endl;
// Calculates highest score
cout << "The high score was student " << highIndex(highScore) << " with a score of " << highIndex(scores);
return 0;
}
// Function gets student scores
void inputAnswers(int student[], double scores[])
{
for (int count = 0; count <= 6; count++)
{
cout << "Enter the score for student "<< student[count] << ": ";
cin >> scores[count];
}
}
As per my observation you haven't supplied any values to the Highscore array and it is not required as well.
If all you need is to find average score, highscore and id of student with highscore this slight change will do the trick just adjusted 3 values from your code and is documented at corresponding lines.
#include<iostream>
using namespace std;
double getAverage(double scores[])
{
double total = 0;
for (int count = 0; count <= 6; count++)
{
total += scores[count];
}
return total / 7;
}
void inputAnswers(int student[], double scores[])
{
for (int count = 0; count <= 6; count++)
{
cout << "Enter the score for student "<< student[count] << ": ";
cin >> scores[count];
}
}
int highIndex(double score[])
{
double highScore = score[0];
double indexHigh = 0; //as we are assigning from position 0
for (int count = 1; count <= 6; count++)
{
if (score[count] > highScore)
{
highScore = score[count];
indexHigh = count;
}
}
return indexHigh; //returns index of highscore
}
int main()
{
const int ids = 7;
int student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 };
double scores[7];
//double highScore[7]; no need
// Gets the test score from user
inputAnswers(student, scores);
// Calculates the average
cout << "The average score is " << getAverage(scores) << endl;
// Calculates highest score
cout << "The high score was student " << student[highIndex(scores)] << " with a score of " << scores[highIndex(scores)]; //uses returned index to find values from array
return 0;
}
Although i strongly recommend using class or structures for such data collection of any entitiy. Happy Coding :-)

Largest / Smallest from an array?

currently stuck on a homework problem and hoping for some hints as to why my code isn't working / what I'm missing. The homework question asks you to make an array that holds a string of salsa flavors for a store, gathers data from the user input on how many were sold (jars) and then display the total sales from each flavor sold. Ive got all that down, but the question goes on to ask you to retrieve the best seller and the worst seller. I've attempted and can get my code to pull two flavors, but they're completely incorrect. Any help is appreciated! Here is my code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// variables
const int SIZE = 5;
string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty"};
//names of salsas
int jars[SIZE]; //holds number of jars user enters
int sales[SIZE]; //holds sales numbers
int count; //counts for a loop
int largest; //largest sale
int smallest; //smallest sale
cout << "Enter the monthly number of jars sold for each type of
salsa.\n\n";
// Display the salsas in the array
for (int count = 0; count < SIZE; count++)
{
cout << salsas[count] << ": ";
cin >> jars[count];
}
// Display salsa sales
cout << "\nEach jar of salsa, no matter the type, costs $5.50.\n";
cout << "Here are the monthly sales numbers for each type of salsa.
\n\n";
cout << fixed << showpoint << setprecision(2);
for (int count = 0; count < SIZE; count++)
{
double sales = jars[count] * 5.50;
cout << salsas[count] << ": $";
cout << sales << endl;
}
//Gets highest sale
{
int count;
int largest = sales[0];
int index = 0;
for (count = 0; count < SIZE; count++)
{
if (sales[count] > largest)
{
largest = sales[count];
index = count;
}
}
cout << "\nBest Seller: " << salsas[index] << endl;
}
//Gets lowest sale
{
int count;
int smallest = sales[0];
int index = 0;
for (count = 0; count < SIZE; count++)
{
if (sales[count] < smallest)
{
smallest = sales[count];
index = count;
}
}
cout << "\nWorst Seller: " << salsas[index] << endl;
}
return 0;
}
One of the biggest hindrances in debugging your own code is in naming your variables. If you've declared count, largest, and smallest variables at the start of your main() function but you redeclare them in a separate block later on. It's downright redundant.
The actual bug is that you're not actually calculating and filling your sales[] array. Comparing them later on for largest and smallest wouldn't work as intended.
Look for this line in the code below
sales[count] = jars[count] * 5.50;
Lé Code
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// variables
const int SIZE = 5;
string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty"};
//names of salsas
int jars[SIZE]; //holds number of jars user enters
int sales[SIZE]; //holds sales numbers
// int count; //counts for a loop // no need
// int largest; //largest sale // no need
// int smallest; //smallest sale // no need
cout << "Enter the monthly number of jars sold for each type of salsa.\n\n";
// Display the salsas in the array
for (int count = 0; count < SIZE; count++)
{
cout << salsas[count] << ": ";
cin >> jars[count];
}
// Display salsa sales
cout << "\nEach jar of salsa, no matter the type, costs $5.50.\n";
cout << "Here are the monthly sales numbers for each type of salsa. \n\n";
cout << fixed << showpoint << setprecision(2);
for (int count = 0; count < SIZE; count++) // declare `count` where it is used
{
sales[count] = jars[count] * 5.50; // calculate sales[count] IMPORTANT
cout << salsas[count] << ": $";
cout << sales[count] << endl; // show appropriate output
}
//Gets highest sale
int largest = sales[0]; // declare largest and index closest to where they are used
int index = 0;
for (int count = 0; count < SIZE; count++)
{
if (sales[count] > largest)
{
largest = sales[count];
index = count;
}
}
cout << "\nBest Seller: " << salsas[index] << endl;
//Gets lowest sale
int smallest = sales[0]; // declare smallest closest to where it is used
index = 0; // reuse your old variable
for (int count = 0; count < SIZE; count++)
{
if (sales[count] < smallest)
{
smallest = sales[count];
index = count;
}
}
cout << "\nWorst Seller: " << salsas[index] << endl;
return 0;
}
Sample Console Interaction
Enter the monthly number of jars sold for each type of salsa.
Mild: 20
Medium: 10
Sweet: 5
Hot: 2
Zesty: 1
Each jar of salsa, no matter the type, costs $5.50.
Here are the monthly sales numbers for each type of salsa.
Mild: $110
Medium: $55
Sweet: $27
Hot: $11
Zesty: $5
Best Seller: Mild
Worst Seller: Zesty

Why isn't my array incrementing properly?

I am in a C++ class and I am having trouble with the project. The idea of the project is to create an ordering application using structs and arrays. As far as I can tell the program is working as intended except for the how many items of each item the person ordered part of my printMenu function. If I am mistaken and or you find more errors please let me know.
Here is the code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
struct dinnerItemType
{
string dinnerItem;
double dinnerPrice;
int dinnerOrdered;
};
void getFood(dinnerItemType ourMenu[], int &size);
void printMenu(dinnerItemType ourMenu[], int size);
void printCheck(dinnerItemType ourMenu[], int size);
//Defines the global tax constant of 6%
const double TAX = 0.06;
int main()
{
dinnerItemType ourMenu[150];
int size = 0;
getFood(ourMenu, size);
printMenu(ourMenu, size);
printCheck(ourMenu, size);
system("pause");
return 0;
}
void getFood(dinnerItemType ourMenu[], int &size)
{
ourMenu[0].dinnerItem = "Chicken Sandwich";
ourMenu[0].dinnerPrice = 4.45;
ourMenu[0].dinnerOrdered = 0;
ourMenu[1].dinnerItem = "Fries";
ourMenu[1].dinnerPrice = 2.47;
ourMenu[1].dinnerOrdered = 0;
ourMenu[2].dinnerItem = "Truffle Fries";
ourMenu[2].dinnerPrice = 0.97;
ourMenu[2].dinnerOrdered = 0;
ourMenu[3].dinnerItem = "Filet 8oz";
ourMenu[3].dinnerPrice = 11.99;
ourMenu[3].dinnerOrdered = 0;
ourMenu[4].dinnerItem = "Fruit Basket";
ourMenu[4].dinnerPrice = 2.44;
ourMenu[4].dinnerOrdered = 0;
ourMenu[5].dinnerItem = "Tea";
ourMenu[5].dinnerPrice = 0.69;
ourMenu[5].dinnerOrdered = 0;
ourMenu[6].dinnerItem = "Water";
ourMenu[6].dinnerPrice = 0.25;
ourMenu[6].dinnerOrdered = 0;
size = 7;
}
void printMenu(dinnerItemType ourMenu[], int size)
{
int number;
int amount;
cout << "Welcome to the restraunt here are your menu items: \n";
for (int i = 0; i < size; i++)
{
cout << (i + 1) << ")";
cout << ourMenu[i].dinnerItem
<< "$"
<< ourMenu[i].dinnerPrice
<< endl;
}
cout << "To order just type in the number associated with the menu item and hit enter.\n"
<< "Once you have completed your order just type in 0 to go to checkout.\n";
cin >> number;
while (number != 0)
{
if (number >= 1 && number <= 8)
{
ourMenu[number - 1].dinnerOrdered++;
}
else
{
cout << "The number does not coorispond with a menu item please try again.\n";
}
cout << "To order just type in the number associated with the menu item and hit enter.\n"
<< "Once you have completed your order just type in 0 to go to checkout.\n";
cin >> number;
}
}
void printCheck(dinnerItemType ourMenu[], int size)
{
double total = 0;
cout << "Your Bill: ";
for (int i = 0; i < size; i++)
{
if (ourMenu[i].dinnerOrdered > 0)
{
total += ourMenu[i].dinnerPrice;
}
}
cout << "Tax: $ " << fixed << setprecision(2) << (total * TAX);
cout << " Ammount Due: $" << (total + (total * TAX)) << endl;
cout << "Thank you come again!" << endl;
}
This line:
total += ourMenu[i].dinnerPrice;
is only adding the cost of one meal to the total, regardless of how many times that meal is ordered.
To fix it: simply multiple the price by the number of times it is ordered:
total += ourMenu[i].dinnerPrice * ourMenu[i].dinnerOrdered;

c++ output highest element in array

I am trying to have the program output the winner in a candidate race when trying to go through the array to get the highest count instead of the highest it gets the next highest from zero, and i feel like i have tried everything.
I keep trying to change things around in the find winner function but nothing seems to be working I dont see what i am doing wrong please help.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
//User inputs data for candidates and votes.
//Output Candidates, votes, and percentage.
//Names that will be used are johnson, miller, duffy, robinson, ashtony
//count of votes to use = 5000, 4000, 6000, 2500, 1800, total 19300
//Percentages that will be used for candidates= 25.91, 20.73, 31.09, 12.95, 9.33
int findWinner(int votes[]);
void Results(string candidates[], int votes[]);
double Percentage(int votes[], int vote);
int tester[5] = {};
const int NUMBER_OF_CANDIDATES = 5;
int main()
{
string candidates[NUMBER_OF_CANDIDATES];
int votes[NUMBER_OF_CANDIDATES];
cout << "Enter 5 Candidates with their votes ex: DelBosque 7000: ";
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
cin >> candidates[i] >> votes[i];
}
cout << "Candidate Votes Received % of Total Votes" << endl;
Results(candidates, votes);
cout << "The Winner of the Election is " << candidates[findWinner(votes)] <<
endl;
return 0;
}
double Percentage(int votes[], int vote){
int sumOfVotes = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
sumOfVotes += votes[i];
}
double percent = static_cast<double>(vote) / sumOfVotes;
double votePercent = 0;
votePercent = percent * 100;
std::cout << std:: fixed;
std::cout << std:: setprecision(2);
std::cout << votePercent;
return 0;
};
void Results(string candidates[], int votes[]){
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
cout << candidates[i] << setw(15) << votes[i] << setw(15);
int percent = Percentage(votes, votes[i]);
cout << percent << "%" << endl;
};
};
// You are returning the number of votes. Shouldn't you be returning the
// index referenced by the highest number of votes?
int findWinner(int votes[]){
int index = 0;
int winner = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
if (votes[i] > winner)
winner = votes[i];
index = i;
};
return index;
};
You need both the following lines under the if condition.
winner = votes[i];
index = i;
like:
if (votes[i] > winner)
{
winner = votes[i];
index = i;
}
What you have is equivalent to:
if (votes[i] > winner)
{
winner = votes[i];
}
index = i;
which is not correct.

(Pointers)Issue with calculating an average function after dropping a test score

When I enter the following value for each question it ask for the user input, the average is not correct. Values I enter are 3 for the amount of testscores, 64,90,52 for the score for each test score. My debugger shows 52 as my lowest drop test score so I do not believe the issue is the LowestTestScore function, but it's the calculate the average test score. The line below should give me the average of just only the two testscores (64,90). average =(total/size-1); // Average with the drop lowest test score is wrong If someone could guide me to the right direction I would gladly appreciated. I posted the entire source code because I was not sure if you could be able to figure out what is wrong with it with just snippets of the code. The sort function works as it should work, The main function works as it should as well. I believe the lowest function works too since it's giving me the correct output as my lowest testgrade.
#include <iostream>
void sortAscendingOrder(int*, int );
void LowestTestScore(int*, int);
void calculatesAverage(int*,int);
int main()
{
int* array = nullptr;
int input;
std::cout << "Enter the number of testscores you want to enter." <<std::endl;
std::cin >> input;
array = new int[input];
for(int count =0; count < input; count++)
{
std::cout << "Enter the test score" << (count +1) <<":" <<std::endl;
std::cin >> *(array+count);
while(*(array+count) < 0)
{
std::cout <<"You enter a negative number. Please enter a postive number." <<std::endl;
std::cin >> *(array+count);
}
}
sortAscendingOrder(array,input);
for(int count =0; count < input;count++)
{
std::cout << "\n" << *(array+count);
std::cout << std::endl;
}
LowestTestScore(array,input);
calculatesAverage(array,input);
return 0;
}
void sortAscendingOrder(int* input,int size)
{
int startScan,minIndex,minValue;
for(startScan =0; startScan < (size-1);startScan++)
{
minIndex = startScan;
minValue = *(input+startScan);
for(int index = startScan+1;index<size;index++)
{
if(*(input+index) < minValue)
{
minValue = *(input+index);
minIndex = index;
}
}
*(input+minIndex)=*(input+startScan);
*(input+startScan)=minValue;
}
}
void LowestTestScore(int* input, int size)
{
int count =0;
int* lowest = nullptr;
lowest = input;
for(count =1; count <size;count++)
{
if(*(input+count) < lowest[0])
{
lowest[0] = *(input+count);
}
}
std::cout << "Lowest score" << *lowest;
}
void calculatesAverage(int* input, int size)
{
int total = 0;
int average =0;
for(int count = 0; count < size; count++)
{
total += *(input+count);
}
average =(total/size-1); // Average with the drop lowest test score is wrong.
std::cout << "Your average is" << average;
}
To average after dropping the lowest test score, change
void LowestTestScore(int* input, int size)
{
int count =0;
int* lowest = nullptr;
lowest = input;
for(count =1; count <size;count++)
{
if(*(input+count) < lowest[0])
{
lowest[0] = *(input+count);
}
}
std::cout << "Lowest score" << *lowest;
}
To (notice '*lowest = 0;' at the bottom):
void LowestTestScore(int* input, int size)
{
int count =0;
int* lowest = nullptr;
lowest = input;
for(count =1; count <size;count++)
{
if(*(input+count) < lowest[0])
{
lowest[0] = *(input+count);
}
}
std::cout << "Lowest score" << *lowest;
*lowest = 0;
}
Then in your calculatesAverage function, make sure you calculate the average like:
average =(total/(size-1));