So the code is supposed to be basically like a survey. And it would be soooo easy if we could write our own code how we wanted... but our teacher wants it his way. You pick a number, which is a drink and then the value increments on that certain drink that you picked.. but the drink is in an array and you have to keep it that way. Then it outputs the results of it, as in "This many people choose that drink". The program is telling me a bunch of things are wrong such as:
1 2 3 4 5 6 7
- error C2065: 'number': undeclared identifier
- error C2228: left of '.index' must have class/struct/union
- note: type is 'unknown-type'
- error C2109: subscript requires array or pointer type
- error C2676: binary '[': 'Drinks' does not define thisoperator or a
conversion to a type acceptable to the predefined operator
- error C2109: subscript requires array or pointer type
I get the errors.. kind of... but how do I fix them? Also is there any tiny small possibility that the code might work?
// Program Favorit surveys users to determine the favorite soft drink.
#include <iostream>
#include <array>
usingnamespace std;
enum Drinks: int{ COKE = 0, PEPSI = 1, SPRITE = 2, DR_PEPPER = 3 };
void Prompt();
struct Selection
{
Drinks index;
int uSelection{};
};
int main() {
Selection select;
number.index = Drinks::COKE, Drinks::SPRITE, Drinks::PEPSI, Drinks::DR_PEPPER;
constint favorites = 4; //holds sum of users who favor each drink
int number;
Drinks index;
for (index = COKE; index <= DR_PEPPER; index = Drinks(index + 1))
//Set to 0
std::array<int, favorites> index{ 0 };
Prompt();
std::cout << "\nYOUR CHOICE: ";
std::cin >> select.uSelection;
std::cout << std::endl;
select.uSelection = 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.
favorites[select.uSelection - 1]++;
Prompt();
cin >> number;
}
// FILL IN THE Code to write out the totals in the format:
// Drink Number of favorites
std::cout << "These are the results of our survey:\n";
for (int i = 0; i < favorites; i++)
{
cout << index[i] << ": " << favorites[i] << "\n";
}
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;
}
1) Is there any reason you chose to edit out the line numbers from your error messages? It would be much easier for us to find the problems, if you'd left them in.
2) number.index = Drinks::COKE, Drinks::SPRITE, Drinks::PEPSI, Drinks::DR_PEPPER;
There is a lot wrong with this line.
a) Firstly, you haven't yet defined anything called number, so there's no way the compiler could understand what it is
b) When you do define it later on, you define it as an int, so there's no way number.index could mean anything
c) You use the comma operator on this line. Do you understand what that comma operator actually does in C++? I'm pretty sure that what you think it's doing here isn't what it actually does.
What is it you're actually trying to achieve here?
3) favorites[select.uSelection - 1]++;
You're trying to use favorites as if it were an array, but you've defined it on line 18 as an int. Did you mean to define it as an array?
4) At line 23, you're declaring an array called index. This only exists within the scope of the for loop; when the loop exits that array no longer exists. So, at line 44, you're trying to use an array that no longer exists. Also...
5) ...at line 20 you've defined, in a different scope, a different variable called index. It#s a really bad idea to have multiple variables with the same name and use different scopes to differentiate them - that will only lead to confusion on your part, and make it harder to understand why yuou're getting errors.
@BGA6444 -- when did the teacher code stop and yours begin? It's not entirely clear what is "allowed" to be changed. You don't want to "Prompt" all the time in the loop, as that would spam too much. These are instructions that occur once at the start. Perhaps add a "Press 's' to see current stats", which would spit out a brief status message.