SOS... a problem in a program I wrote

1student

Member
May 30, 2004
42
0
0
hi, I have to write a program for a player in bridge.

I have to:
1. recieve 13 cards, and enters them into a linked list
2. print them in the order I got them
3. orginize them in a sort order:
-> the order in the seried: clubs-> diamonds-> hearts-> spades
-> each series has to b sorted also
4. print it out

The main parts of the program are: main & hand.

from 'main' I call to:
* read - gets the details of the player's cards
* write - prints the lists out
* hand - which cares to orginize the cards in a sort order

from 'hand' I call to:
* copy - which copies each card to the suitable series
* sort - which sorts each series
* merge - which merges all the series to one list

I did it, but have some problem in it...

enclosed the program.

plsssss, help me!!! :-(
 

1student

Member
May 30, 2004
42
0
0
I can compile it, but when I run I get the message:
Unhandled Exception General Protection Exception...

The bold line the program point to, as a guilty one, is that:
void read (card *&list)
{
card *c=new card;

for(int i=1; i<=NUM_CARD; i++)
{
cin>>c->suit>>c->num;
c=list;
list=c;
list->next=new card;
}
c->next=NULL;
}
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
So read() simply tacks a new card on the end? I'm not down with all the c++ but you're algorithm looks just a little bit odd. First you create a new card and enter it's suit and number. Then you abandon that card by assigning list to c. Then you assign c to list (which will have no effect). Then you tack on a new card. When you repeat you go to work on the same card again (I don't see any c = c->next). Then at the end you cut the list off. So you've effectively lost a reference to NUM_CARD-1 cards.

I could be reading this all wrong, like I said, I've never done c++.

I'd suggest an algorithm for you but I don't understand the semantics of read(). Are you passing in a pointer to a card pointer and after the call that pointer should point to an entire list?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
You don't need so many lists to do this.

number your cards 1-52. card 1-13 are 2-A of clubs, 14-26 are 2-A of diamonds, 27-39 are 2-A of hearts, 40-52 are 2-A of spades.

Then you just select the cards, drop them in the list, print out the list, sort the list like an array of integers, and print out the list again.
 

1student

Member
May 30, 2004
42
0
0
'read' gets the all 13 cards, in the order the user enters it - it keeps it in the 'list'.
Than I split it to maximum 4 groups: c, d, h and s (it can be also 1 group, if all the cards the player got belong to one series), I sort each of the groups, and than merge the series to 1 list, according to the order: c-> d-> h-> s.
That's what I meant to do, any way...

Then you just select the cards, drop them in the list, print out the list, sort the list like an array of integers, and print out the list again
amm.. that're the requirements in my HW..
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
by the way, your parameter to read has a datatype of card*&. that's kind of weird. if you're passing in a pointer into your function, you don't need to pass that pointer as a reference. just pass the pointer in by value as card*.

the general protection fault refers to the fact that you declared card* list in main(), never created a new card through something like "list = new card()", and ultimately try to access a member of this card* variable that points to invalid memory. allocate a new card and assign it to list first.
 

1student

Member
May 30, 2004
42
0
0
Originally posted by: oog
by the way, your parameter to read has a datatype of card*&. that's kind of weird. if you're passing in a pointer into your function, you don't need to pass that pointer as a reference. just pass the pointer in by value as card*.
Ary u sure? Because I learnt that when I move a pointer of a list, for ex., and I want to change the parameter it points to - I have to get it as *&...

I think I understood what u wrote afterwards.. I'm not sure.
I tried to correct my program according to it, and initialized the list in 'main'.
Now the ptoblem is in another place...
Now I get something printed (in that step:cout<<"The cards, befor organizing:\n";, in main), but it doesn't do it well (I entered 'c' from 1 to 13, and got: "c 13");
The program stucked just after that step..
In another check, the problem is where it was before...

:eek: :(
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: 1student
'read' gets the all 13 cards, in the order the user enters it - it keeps it in the 'list'.
Than I split it to maximum 4 groups: c, d, h and s (it can be also 1 group, if all the cards the player got belong to one series), I sort each of the groups, and than merge the series to 1 list, according to the order: c-> d-> h-> s.
That's what I meant to do, any way...

Then you just select the cards, drop them in the list, print out the list, sort the list like an array of integers, and print out the list again
amm.. that're the requirements in my HW..

You completely missed my point. I'm saying you don't need to split it into four lists or do four sorts. You can just use one list and one sort.