getting odd answers

so i am just writing a program to organize a two dimentional array as a deck of cards then cout a random card 5 times. for whatever reason it is giving me odd answers.please let me now where the problem is. thanks...

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
#include<iostream>

using namespace std;
int main()
{
    int cards[52][1],hand[5][1],i = 3,j = 1,m = 0,k;
    srand(time(NULL));
    // make deck with values
    for(i = 3;i < 7;i++){
      for(j=1;j<=13;j++){
                         cards[m][0] = j;
                         cards[m][1] = i;
                         cout << m << ": " << cards[m][0] << char(cards[m][1]) << endl;
                         m++;
      }
    }
    cout << endl;
    i = 0;
    while(i < 5){
            k = rand()% 52;
            cout << k << ": ";
            cout << char(cards[k][1]) << endl;
            i++;
    }
    
    system("pause");
    return 0;
}
your card array doesn't have enough elements for the suit. you only asked for one 1.


int a[1] has 1 element,  a[0].             there is no a[1];
int a[2] has 2 elements, a[0], a[1].       there is no a[2];
int a[3] has 3 elements, a[0], a[1], a[2]. see where this is going?


the compiler didn't say anything! scary! i guess we are free to read and write to memory outside our program.
Topic archived. No new replies allowed.