How to tell user which number to vote for?

I am working on a dice game for a C++ class in college. I am trying to make the program tell the user which number to vote for depending on whichever dice number rolled the most. However I only get the first if statement to execute with ones > twos, threes, fours, etc. Your help would be very appreciated, as I am a beginner and need all the help I can get.

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
57
58
59
60
#include<iostream>
#include<cstdlib>//rand and srand
#include<ctime>
using namespace std;
int main()
{srand(time(0));//using current time
	int ones,twos,threes,fours,fives,sixes;//declare each possible number
	int roll, r;
	char goagain;
	do{//loop, do this while goagain = y
	ones=twos=threes=fours=fives=sixes=0; //all set to 0
	cout<<"\nHow many rolls: ";
	cin>> r;
	for(int i=1; i<= r; i++) //up to the number of rolls user inputs (r)
	{
	roll = rand()%6+1;//between 1 and 6, rand() randomizes a number then puts into % equation
	cout<< "\nYou rolled a "<< roll;
	if(roll==1)ones++;//if roll = 1, counter adds +1
	if(roll==2)twos++;
	if(roll==3)threes++;
	if(roll==4)fours++;
	if(roll==5)fives++;
	if(roll==6)sixes++;
	}
	
	cout<<"\n\n1 - "<<ones//prints out how many times each number was outputted
		<<"\n2 - "<<twos
		<<"\n3 - "<<threes
		<<"\n4 - "<<fours
		<<"\n5 - "<<fives
		<<"\n6 - "<<sixes;	
		
	if(ones > twos, threes, fours, fives, sixes){
		cout << "\nYou should vote for 1, because it rolled " << ones << " times." << endl;	
	} else if(twos > ones, threes, fours, fives, sixes){
		cout << "You should vote for 2, because it rolled " << twos << " times." << endl;	
	} else if(threes > ones, twos, fours, fives, sixes){
		cout << "You should vote for 3, because it rolled " << threes << " times." << endl;	
	} else if(fours > ones, twos, threes, fives, sixes){
		cout << "You should vote for 4, because it rolled " << fours << " times." << endl;
	} else if(fives > ones, twos, threes, fours, sixes){
		cout << "You should vote for 5, because it rolled " << fives << " times." << endl;	
	} else if(sixes > ones, twos, threes, fours, fives){
		cout << "You should vote for 6, because it rolled " << sixes << " times." << endl;	
	} else {
		cout << "\n I am not sure what you should vote for.";	
	}
	cout<<"\nGo again? y/n ";//prompt user at the end
	cin>>goagain;
	}
	while(goagain=='y');//otherwise program ends
	
	
	
	
	
	
cout<< "\n\n";
return 0;
}
Last edited on
Use an array to hold the counts.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(time(0));
    char goagain;
    do {
        int nums[6] {}; // the empty braces init the array to zeros.

        int num_rolls;
        cout << "How many rolls: ";
        cin >> num_rolls;

        // Roll the dice and collect the counts.
        for (int i = 0; i < num_rolls; ++i)
        {
            int roll = rand() % 6 + 1;
            cout << "You rolled a " << roll << '\n';
            ++nums[roll - 1];
        }

        // Print the results.
        for (int i = 0; i < 6; ++i)
            cout << i + 1 << " - " << nums[i] << '\n';

        // Determine the highest count and whether it occurs more than once.
        int high = 0;
        bool multiple = false;
        for (int i = 1; i < 6; ++i)
        {
            if (nums[i] >= nums[high])
            {
                if (nums[i] > nums[high])
                {
                    high = i;
                    multiple = false;
                }
                else
                    multiple = true;
            }
        }

        if (multiple)
            cout << "I am not sure what you should vote for.\n";
        else
            cout << "You should vote for " << high + 1
                 << ", because it rolled " << nums[high] << " times.\n";

        cout << "\nGo again? y/n ";
        cin >> goagain;
    }
    while (goagain == 'y');
}

Last edited on
Topic archived. No new replies allowed.