Question about compiler warnings

So I'm still new to C++ (don't know a lot outside of what is here) and this is the first time I've ever gotten a warning at all. I'm just curious what it is and how and why I should fix it.

I get:

Line 57 - "name lookup of 'i' changed"
Line 8 - "matches this 'i' under ISO standard rules"
Line 18 - "matches this 'i' under old rules"

I have no idea what any of this means and would be grateful if someone could explain it.

ANY tips/help about my question or about anything in my code is always appreciated

By the way I made this to simulate the first roll in the game of Monopoly and the chances of where you would land each time. I'm going to have it display the percentages of the simulation next.

Sorry if anything is unclear. I know the code is pretty messy.



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int choice;
    int i, roll_1, roll_2, roll_end; //defining the index for my for loops
    int CountJail, Count2, Count3;
    int Count[40];
    
    while(1)
    {
        system("CLS");
        
        CountJail = Count2 = Count3 = 0;
        
        for(int i = 0; i<=39; i++)//initializing all my array things(word?) to 0
        Count[i] = 0;
    
        unsigned int seed = static_cast<int>(time(0));
        srand(seed);
        
        cout << "1 - Simulate once" << endl;
        cout << "2 - Simulate ten times" << endl;
        cout << "3 - Simulate one hundred times" << endl;
        cout << "4 - Simulate one thousand times" << endl;
        cout << "5 - Simulate ten thousand times" << endl;
        cout << "6 - Simulate a custom number of times" << endl;
        cout << "7 - Quit" << endl;
        
        cin >> choice;
        
        switch(choice){
            case 1:
                choice = 1;
                break;
            case 2:
                choice = 10;
                break;
            case 3:
                choice = 100;
                break;
            case 4:
                choice = 1000;
                break;
            case 5:
                choice = 10000;
                break;
            case 6:
                cin >> choice;
                break;
            case 7:
                exit(1);
            }
            
        for(i = 0; i < choice; i++) //rolling the dice the input amount of times
        {
            roll_1 = rand() % 6 + 1;
            roll_2 = rand() % 6 + 1;
            roll_end = roll_1 + roll_2;
            
            if(roll_1 != roll_2)
            {
                Count[roll_end]++;
                
            }else{
                roll_1 = rand() % 6 + 1;
                roll_2 = rand() % 6 + 1;
                roll_end = roll_end + roll_1 + roll_2;
                
                if(roll_1 != roll_2)
                {
                    Count[roll_end]++;
                    Count2++;
                    
                }else{
                    roll_1 = rand() % 6 + 1;
                    roll_2 = rand() % 6 + 1;
                    roll_end = roll_end + roll_1 + roll_2;
                    
                    if(roll_1 != roll_2)
                    {
                        Count[roll_end]++;
                        Count3++;
                        
                    }else{
                        Count[10]++;
                        CountJail++;
                    }
                }
            }
        }
        
        for(i = 0; i <= 39; i++) //printing the times each square was landed on                                
        {
            if(Count[i] != 0)
                cout << endl << i << " was rolled " << Count[i] << " times." << endl;
        }
        if(Count2 != 0)
        cout << "There was 1 double " << Count2 << " time(s)." << endl;
        if(Count3 != 0)
        cout << "There were 2 doubles " << Count3 << " time(s)." << endl;
        if(CountJail != 0)
        cout << "You went to jail " << CountJail << " time(s)." << endl;
        
        system("pause");
    }
    return 0;
}
Last edited on
I found an answer somewhere else but it seems a little round-about way to do it.

1
2
for(int i = 0; i<40; i++)
Count[i] = 0;


Is there another way to do it or is this the simplest way?
Last edited on
That is the simplest, but you can also use the memset() function from the <cstring> header. I personally would use the one you already have, but it is up to you.

For details on the memset() function, check out http://www.cplusplus.com/reference/clibrary/cstring/memset.html
Okay, Thanks a lot, I'll check it out. I have another question though. Check first post ^^^^
Last edited on
another thing is that you wrote:
int i, roll_1, roll_2, roll_end; //defining the index for my for loops
but after you wrote
for(int i = 0; i<40; i++)
you said int i twice. You can only initiazize i onece.
what you can do is change the first to
int roll_1,rool_end, roll_end;
or change the second to
for( i = 0; i<40; i++)

both will fix that problem
oh and the answer to your first post is yes, there is a way too make it "neater" for other peaple to look at. you can make rolling a dice a function.
by doing function.....roll dice the loop is choice times.
it. that way you can actually input ANY number of time to simulate it. Ya thats it good luk!
Remove i from line 8, and declare it in all for statements like this:
 
for (int i = ...
Last edited on
Topic archived. No new replies allowed.