Arrays with incrementing values...yay! :(

Jul 20, 2018 at 11:06pm
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 this operator 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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Program Favorit surveys users to determine the favorite soft drink. 
#include <iostream> 
#include <array>

using namespace 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;
	const 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))
		//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;
}

That is my code...
Jul 21, 2018 at 12:28am
Maybe something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <map>
#include <string>

enum class drink
{
    coke, sprite, pepsi, dr_pepper
};

std::ostream& operator<<(std::ostream& os, drink d)
{
    switch(d)
    {
        case drink::coke:      return os << "Coca Cola";
        case drink::sprite:    return os << "Sprite";
        case drink::pepsi:     return os << "Pepsi";
        case drink::dr_pepper: return os << "Dr. Pepper";
        default: 
            os.setstate(std::ios_base::failbit);
            return os;
    }
}

std::istream& operator>>(std::istream& is, drink &d)
{
    std::string tok ; is >> tok; 
    if      (tok == "coke")   d = drink::coke;
    else if (tok == "sprite") d = drink::sprite;
    else if (tok == "pepsi")  d = drink::pepsi;
    else if (tok == "dp")     d = drink::dr_pepper;
    else is.setstate(std::ios::failbit);
    return is;
}
 
int main() 
{
    std::map <drink, int> drink_picks;
        
    for (drink d; std::cin >> d;)
        drink_picks[d]++;
        
    for (auto [drink, picks] : drink_picks)
        std::cout << picks << " users preferred " << drink << ".\n";
}


Live demo:
http://coliru.stacked-crooked.com/a/2deaf10b0e61f743
Last edited on Jul 21, 2018 at 12:29am
Jul 21, 2018 at 1:21am
I've thought about using case but the teacher wants us to follow his code. Thank you anyway.
Jul 21, 2018 at 1:52am
Well, maybe you'll find it helpful anyways. Good luck!
Last edited on Jul 21, 2018 at 2:30am
Jul 23, 2018 at 1:51pm
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.

Jul 24, 2018 at 3:34pm
@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.
Topic archived. No new replies allowed.