Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include <iostream>
#include <cstddef>
using namespace std;

struct cards
{
    int num;
    string suit;
    cards * next_card;
};

cards get_card(int num, string suit, cards *previous_card)
{
    cards card;
    card.num = num;
    card.suit = suit;
    card.next_card = previous_card;
    return card;
}

int main()
{
    cards *temp_card = NULL;
    string suit;

    for (int i = 0; i<4; i++)
    {
        if (i == 0){suit = "hearts";}
        if (i == 1){suit = "diamonds";}
        if (i == 2){suit = "clubs";}
        if (i == 3){suit = "spades";}
        for (int j = 0; j<13; j++)
        {
            cards sum_card = get_card(j+1, suit, temp_card);
            temp_card = &sum_card;
        }
    }
    while ((*temp_card).next_card != NULL)
    {
        cout <<(*temp_card).num <<" of ";
        cout <<(*temp_card).suit <<"\n";
        temp_card = (*temp_card).next_card;
    }
    cout <<"yis it work";
}


This is meant to print all the cards in a deck of cards but it just prints the same card over and over again infinitely. Can anyone spot the problem?

What I have tried:

sum stuf.......................................................................
Posted
Updated 19-Mar-18 11:55am
v2
Comments
Kornfeld Eliyahu Peter 19-Mar-18 15:17pm    
Have you used the debugger to execute the code step-by-step?
BerthaDusStuf 19-Mar-18 15:43pm    
No I dont really know how to use the debugger
Kornfeld Eliyahu Peter 19-Mar-18 16:04pm    
As a developer it is a must have knowledge - you can not start too early...
BerthaDusStuf 19-Mar-18 16:06pm    
ok i will try to learn it

Because only one card exists: and you keep overwriting it.
It doesn't matter if you declare a variable inside or outside a loop for this: only one variable is created, you do not get a "brand new one" each tine you go round the loop.
To prove this, look at the address you take each time and store in temp_card with the debugger - it will always be the same address.

To use anew card each time, you need to use either an array (probably the best for a deck of cards) or malloc to give you a new area of memory to store your card info in.
 
Share this answer
 
Comments
BerthaDusStuf 19-Mar-18 16:49pm    
Oh I think I saw other examples of using a loop to create multiple items in a linked list using the same variable name each loop but I think that example was using new to get memory from the free store. Is it the fact that I am not utilizing the free store that means I can not name the variable the same thing each time?
OriginalGriff 19-Mar-18 17:07pm    
"new" will work fine - GB o back and follow that example.
BerthaDusStuf 19-Mar-18 18:02pm    
So is what your saying is using new allows you to name multiple variables the same thing? Also what example?
OriginalGriff 20-Mar-18 5:28am    
No. It's complicated, and you need to learn about pointers, references, and memory before you are going to understand it properly - you can't "change the name" of a variable, but you can change what memory it refers to.
BerthaDusStuf 8-Jun-18 19:11pm    
ok thanks
cards is a linked list, you must allocated each node manually using malloc().
C++
cards get_card(int num, string suit, cards *previous_card)
{
    cards card; // here is the memory problem
    card.num = num;
    card.suit = suit;
    card.next_card = previous_card;
    return card;
}

This code dynamically allocate a node of cards, the problem is that the node is freed the code return from the function.
Said otherwise, the code is just messing with the memory.
malloc - C++ Reference[^]
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
BerthaDusStuf 19-Mar-18 18:12pm    
Ok thanks I don't know how to use the debugger but Ill look into it
If that is meant as an exercise on linked list then you, as others already note, have to exercise more on linekd list.
On the other hand, if you really need to handle a deck of cards in a C++ program, then a linked list is possibly not the better choice. Try for instance
C++
#include <vector>
#include <array>
#include <iostream>
using namespace std;

constexpr int VALUES = 13;
constexpr int SEEDS = 4;
constexpr int CARDS = VALUES * SEEDS;


class Card
{
  int v;

public:
  Card(int v):v(v){}
  int getValue() const { return (v % VALUES);}
  int getSeed() const { return (v / VALUES);}
  string getSeedDesc() const;
};

ostream & operator << ( ostream & os, const Card & card);


int main()
{
  vector <Card > card;

  for ( int n = 0; n < CARDS; ++n) card.emplace_back(Card(n));

  for (const auto & c : card) cout << c << endl;
}


ostream & operator << ( ostream & os, const Card & card)
{
  os << card.getValue() << " of " << card.getSeedDesc();
  return os;
}

string Card::getSeedDesc() const
{
  static array<string, SEEDS> desc = { "hearts", "diamonds", "clubs", "spades" };
  return desc[getSeed()];
}
 
Share this answer
 
Comments
BerthaDusStuf 19-Mar-18 18:15pm    
Oh thanks for the solution but I dont really know how to use classes very well and a few other things that you have used in you code, I am only making this for the purpose of practicing using linked lists but thanks, sorry I should have made it clear that I needed to do it using linked lists
CPallini 20-Mar-18 4:51am    
If you don't know how to use classes, the it is time to learn.
Trying to implement yourself a linked list is a good exercise, anyway.
BerthaDusStuf 23-Mar-18 10:50am    
Yes I am going through a book in order of chapters and I have got an idea of what classes are aswell but I dont have much knoledge on the syntax of them and how to implement them.
Try this:
C++
#include <iostream>
#include <string>
using namespace std;

struct cards
{
    int num;
    string suit;
    cards * next_card;
};

cards* get_card(int num, string suit, cards *previous_card)
{
    cards* card = new cards;;
    card->num = num;
    card->suit = suit;
    card->next_card = previous_card;
    return card;
}

int main()
{
    cards *temp_card = NULL;
    string suit;

    for (int i = 0; i<4; i++)
    {
        if (i == 0){suit = "hearts";}
        if (i == 1){suit = "diamonds";}
        if (i == 2){suit = "clubs";}
        if (i == 3){suit = "spades";}
        for (int j = 0; j<13; j++)
        {
            cards* sum_card = get_card(j+1, suit, temp_card);
//            cout << sum_card->num << " of " << sum_card->suit << ", next: " << sum_card->next_card << endl;
            temp_card = sum_card;
        }
    }
    while (temp_card != NULL)
    {
        cout <<temp_card->num <<" of ";
        cout <<temp_card->suit <<"\n";
//        cout <<temp_card->next_card <<"\n";
        temp_card = temp_card->next_card;
    }
//    cout <<"yis it work";
}

It could do with more tidying up but it fixes the problem of using the same address of a temporary structure for each link. It also tidies up the dereferencing of structure members, and correctly lists all the cards.
 
Share this answer
 
Comments
BerthaDusStuf 19-Mar-18 18:17pm    
Ok thanks someone suggested earlier that I should use new and now I see how.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900