Segmentation fault and "War" card game

Hi,

I'm receiving the following error after trying to debug my project.
Signal received: SIGSEGV (Segmentation fault)
For program final_project, pid 8,900
I'm in a beginning C++ class, so I'm not sure what it means.

I'm trying to do a project for aforementioned class, by trying to create the card game "war"
For the sake of space, I'll post the latter part of the program.
before this I was able to successfully set 2 different arrays (each holding 52 values)to hold random 'card numbers' from values 1-26 in those arrays. And for values 26-52 in both arrays, I set equal to 0.
so, for example
uhand[52] = {1,2,3.....26, 0,0,0.....0}
ohand[52] = {1,2,3.....26, 0,0,0.....0}

I have been working on this for a long while now, and I cannot pinpoint why this isn't working.

The program works correctly until you get to the array[26] value, where the first 0's are initialized in the arrays.

Thank you for any help.

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
    int pos1=0, pos2=0;
    do{
        
            while (uhand[pos1]==0){
                pos1++;
                if (pos1==51)
                    pos1==0;
            }
            while (ohand[pos2]==0){
                pos2++;
                if (pos2==51)
                    pos2==0;
            }
        if (pos1==51)
                pos1=0;
        if (pos2==51)
            pos2=0;
        if (uhand[pos1]>ohand[pos2]){
            cout<<setw(5)<<"Your card"<<setw(5)<<"Their card"<<endl;
            cout<<setw(5)<<uhand[pos1]<<setw(5)<<ohand[pos2]<<endl;
            cout<<"You won!"<<endl;
                for (int i=0; flag!=1; i++){
                    if (uhand[i]==0){
                        uhand[i]=ohand[pos2];
                        flag=1;
                        
                    }if (i==52)
                            i=0;
            }
            ohand[pos2]=0;
            pos1=pos1++;
            pos2=pos2++;
            ucount++;
        }
       else if (uhand[pos1]<ohand[pos2]){
            cout<<setw(5)<<"Your card"<<setw(5)<<"Their card"<<endl;
            cout<<setw(5)<<uhand[pos1]<<setw(5)<<ohand[pos2]<<endl;
            cout<<"You lost."<<endl;
            for (int i=0; flag!=1; i++){
                if (ohand[i]==0){
                    ohand[i]=uhand[pos1];
                    flag=1;
                    
                }if (i==51)
                        i=0;
            }
            uhand[pos1]=0;
            pos1=pos1+1;
            pos2=pos2+1;
            ucount--;
        }
        else if (uhand[pos1]==ohand[pos2]){
            cout<<setw(5)<<"Your card"<<setw(5)<<"Their card"<<endl;
            cout<<setw(5)<<uhand[pos1]<<setw(5)<<ohand[pos2]<<endl;
            cout<<"You and your opponent tied!"<<endl;
              for (int i=0; flag!=1; i++){
                if (uhand[i]==0){
                    uhand[i]=uhand[pos1];
                    flag=1;
                }
                  for (int i=0; flag!=1; i++){
                if (ohand[i]==0){
                    ohand[i]=ohand[pos1];
                    flag=1;
                    
                }if (i==51)
                        i=0;}}
                 pos1++;
                pos2++;}}
    while (ucount<52 || ucount>0);
    cout<<ucount<<endl;
    
Last edited on
Your code is so hard to read due to the inconsistent spacing. But I believe you're going out of bounds with your array. Something doesn't seem right with pos1 and pos2. I'd double check the values each time and make sure they never make it larger than 51.
Topic archived. No new replies allowed.