0

i tried to write a program that prints out all the cards in a deck but it crashes i tried removing the for loop in the main function and manually asigning the value to one card for the deck and then printing it and then it worked, but with the whole code compiler doesnt see any errors and runs just fine only to crash, whats the problem? im using DevC++ as the compiler

#include <iostream>
#include <string>
using namespace std;


struct card
{
        
    int num;
    string type;
    
    
    void printcard()
    {
        cout<<num<<" "<<type;
    }
};

card deck[52];

void printdeck()
{
    for (int f=0; f<=52; f++)
    {
        deck[f].printcard();
    }
}

int main()
{
    
   
   int J,Q,K,A;
    J=10;
    Q=10;
    K=10;
    A=11;
   int num[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A};
   string type[] ={"Jvari", "Yvavi", "Guli", "Aguri"};
    
    
    int k=0;
    for(int i=0; i<=4; i++)
    {
        for(int j=0; j<=13; j++)
        {
            deck[k].num = num[j];
            deck[k].type = type[j];
            k++;
        }
    }
    
    printdeck();
    
   return 0;
}
Programvarg
  • 31
  • 1
  • 6
  • 3
    Indices are zero-based. So your `for` loops terminating conditions need to be `<` 52, 4 and 13 respectively, not `<=` – acraig5075 Jun 08 '21 at 06:34
  • tried doing that still crashes when running – Programvarg Jun 08 '21 at 06:35
  • 1
    How is a very good time to learn how to use a *debugger* to catch crashes as they happen. That will allow you to locate exactly when and where the crash happens, and also lets you examine the values of all involved variables at the time of the crash. – Some programmer dude Jun 08 '21 at 06:43

1 Answers1

4

Indices are zero-based. So, your array int num[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A}; which has 13 items will be indexed from 0 -> 12. As such, the loop

for(int j=0; j<=13; j++)

will accessed num[13] which isn't in the range. The same goes for

for(int i=0; i<=4; i++)

and

for (int f=0; f<=52; f++)

To solve this, change <= to <. Another problem is that you mis-assigned the type of the card:

deck[k].type = type[j];

should be:

deck[k].type = type[i];

as i ran from 0->3.

Full code (after some clean-up):

#include <iostream>
#include <string>
using namespace std;

struct card
{
    int num; string type;
    void printcard() { cout<<num<<" "<<type << endl; }
};

card deck[52];

void printdeck()
{
    for (int f=0; f<52; f++){ deck[f].printcard(); }
}

int main()
{
    int J,Q,K,A;
    J = Q = K = 10; A = 11;
    int num[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A};
    string type[] ={"Jvari", "Yvavi", "Guli", "Aguri"};

    int k=0;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<13; j++)
        {
            deck[k].num = num[j];
            deck[k].type = type[i];
            k++;
        }
    }
    printdeck();
    return 0;
}

Result:

2 Jvari
3 Jvari
4 Jvari
5 Jvari
6 Jvari
7 Jvari
8 Jvari
9 Jvari
10 Jvari
10 Jvari
10 Jvari
10 Jvari
11 Jvari
...

Also see Why is "using namespace std;" considered bad practice?

silverfox
  • 1,568
  • 10
  • 27