Can anyone tell me how I'm supposed to increment in variables that are in arrays? I have tried looking on the internet but they aren't very clear. Also, how am I suppose to cout the values of those variables? Thanks.
I tried douing what you all sayd, but its saying syntax error: unexpected token 'constant', expected 'id-expression' and that I need a ;. But where? I actually don't know what I'm doing anymore. The program is basically suppose to be a poll on which drink is a persons favorite, its then suppose to cout the results and ask again. It sounds easy but the teacher wants us to use his code, so I dont even know anymore.
// Program Favorit surveys users to determine the favorite soft drink.
#include <iostream>
usingnamespace std;
enum Drinks { COKE, PEPSI, SPRITE, DR_PEPPER };
void Prompt();
int main() {
int favorites[4]; //holds sum of users who favor each drink
int number;
Drinks index;
for (index = COKE; index <= DR_PEPPER; index = Drinks(index + 1))
{
// FILL IN Code to initialize array favorites to all zeros
int favorites[4] = { 1 }; //
//
Prompt();
cin >> number;
while (number != 4)
{
// FILL IN Code to increment the proper drink fvorites based on user selections
// e.g. if user enter 0 increment favorites[COKE] etc.
if (number == 0)
{
enum Drinks[1]++;
}
cout << favorites[COKE];
Prompt();
cin >> number;
}
}
// FILL IN THE Code to write out the totals in the format:
// Drink Number of favorites
cout << index;
system("PAUSE");
return 0;
}
/*******************************************************/
void Prompt() {
cout << "Enter a 0 if your favorite is a Coke." << endl;
cout << "Enter a 1 if your favorite is a Pepsi." << endl;
cout << "Enter a 2 if your favorite is a Sprite." << endl;
cout << "Enter a 3 if your favorite is a DrPepper." << endl;
cout << "Enter a 4 if you wish to quit the survey." << endl;
}
#include <array>
#include <iostream>
#include <string>
enumclass Drinks : int { COKE = 1, PEPSI = 2, SPRITE = 3, DR_PEPPER = 4 };
struct Selection
{
Drinks drink;
int uSelection{ };
};
auto main() -> int
{
constint NUMELS = 4;
// We instantiate a struct object here and assign it the value of 1 in the second line
Selection select;
select.drink = Drinks::COKE;
// Two arrays - one whose values is initalized to 0 and the other holds the drink names.
std::array<int, NUMELS> favorites{ 0 };
std::array<std::string, NUMELS> drinkNames{ "1 - COKE ", "2 - PEPSI ", "3 - SPRITE", "4 - DR_PEPPER" };
std::cout << "Please select your favorite drink [1-4] or [0] to quit:\n\n";
do
{
for (autoconst &drinkList : drinkNames)
{
std::cout << drinkList << "\n";
}
std::cout << "\nYOUR CHOICE: ";
std::cin >> select.uSelection;
std::cout << std::endl;
if (select.uSelection != 0)
{
// Here we increment the value of each of the array elements by one
// depending on the user choice made. The -1 is only there to allow
// the user to input 1-4 instead of having to input array indices.
favorites[select.uSelection - 1]++;
}
} while (select.uSelection != 0);
std::cout << "These are the results of our survey:\n";
for (auto i = 0; i < NUMELS; i++)
{
std::cout << drinkNames[i] << ": " << favorites[i] << "\n";
}
return 0;
}