473,703 Members | 3,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Don't understand why my code isn't working

I've got a simple and repetitive bit of code for a function in a C
implementation of the card game 31s I'm working on. BTW, I am a bit of
a novice at C; for the past couple of years I was using Visual Basic,
but I wanted portability, so I chose C.

I am developing this in XCode 2 (on Mac OS 10.4.7) as a normal C
command line utility; so this is basically just GCC. This code compiles
fine, without any errors or warnings but when I run it in XCode it
says: "31s has exited due to signal 11 (SIGSEGV)." and in Terminal it
says "Segmentati on Fault". Here is the code of most of the function,
and what I'm pretty sure is the faulty bit. I can post all the code up
if needed; it's all in one .c file.

void print_stats() {
int
location_of_pla yer_card_1,loca tion_of_player_ card_2,location _of_player_card _3;
int
location_of_CPU 1_card_1,locati on_of_CPU1_card _2,location_of_ CPU1_card_3;
int
location_of_CPU 2_card_1,locati on_of_CPU2_card _2,location_of_ CPU2_card_3;
int
location_of_mid dle_card_1,loca tion_of_middle_ card_2,location _of_middle_card _3;

int x,y;
while (x<52) {
if (where_are_card s[x] = PLAYER_HAND) {
switch (y) {
case 0:
location_of_pla yer_card_1 = x;
y++;
break;
case 1:
location_of_pla yer_card_2 = x;
y++;
break;
case 2:
location_of_pla yer_card_3 = x;
y++;
break;
}
}
x++;
}

x,y = 0;

while (x<52) {
if (where_are_card s[x] = CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU 1_card_1 = x;
y++;
break;
case 1:
location_of_CPU 1_card_2 = x;
y++;
break;
case 2:
location_of_CPU 1_card_3 = x;
y++;
break;
}
}
x++;
}

x,y = 0;

while (x<52) {
if (where_are_card s[x] = CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU 2_card_1 = x;
y++;
break;
case 1:
location_of_CPU 2_card_2 = x;
y++;
break;
case 2:
location_of_CPU 2_card_3 = x;
y++;
break;
}
}
x++;
}

x,y = 0;

while (x<52) {
if (where_are_card s[x] = MIDDLE) {
switch (y) {
case 0:
location_of_mid dle_card_1 = x;
y++;
break;
case 1:
location_of_mid dle_card_2 = x;
y++;
break;
case 2:
location_of_mid dle_card_3 = x;
y++;
break;
}
}
x++;
}

printf("Table:\ n");
printf(" Your hand: ");
print_card(pack[location_of_pla yer_card_1]);
print_card(pack[location_of_pla yer_card_2]);
print_card(pack[location_of_pla yer_card_3]);

}

Jul 18 '06 #1
9 1863
Simon wrote:
int x,y;
while (x<52) {
The value of `x` is indeterminate. Undefined behaviour. It
might, for example, be -1066 (assuming it's not a trap
value).
if (where_are_card s[x] = PLAYER_HAND) {
I'm /sure/ this isn't what you meant to say. You've assigned
`PLAYER_HAND` to `where_are_card s[x]`, and if PLAYER_HAND isn't
zero, you do the then-part (the switch below). I think you
meant to write `==`.
switch (y) {
case 0:
location_of_pla yer_card_1 = x;
y++;
break;
case 1:
location_of_pla yer_card_2 = x;
y++;
break;
case 2:
location_of_pla yer_card_3 = x;
y++;
break;
}
}
x++;
}
Wouldn't it be better to have location_of_pla yer_card being an
array so you could just write

location_of_pla yer_card[y] = x;

(if necessary, checking y to be in range)?
x,y = 0;
I don't think that does what you think.

What it does is evaluate `x`, throw the value away, then assign 0
to `y`.

I suspect you need to write

x = y = 0;
while (x<52) {
if (where_are_card s[x] = CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU 1_card_1 = x;
y++;
break;
case 1:
location_of_CPU 1_card_2 = x;
y++;
break;
case 2:
location_of_CPU 1_card_3 = x;
y++;
break;
}
}
x++;
}
Almost exactly the same as the code above. Seek to remove duplication.
x,y = 0;
As above.
>
while (x<52) {
if (where_are_card s[x] = CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU 2_card_1 = x;
y++;
break;
case 1:
location_of_CPU 2_card_2 = x;
y++;
break;
case 2:
location_of_CPU 2_card_3 = x;
y++;
break;
}
}
x++;
}
As other above.
x,y = 0;
Ditto.
while (x<52) {
if (where_are_card s[x] = MIDDLE) {
switch (y) {
case 0:
location_of_mid dle_card_1 = x;
y++;
break;
case 1:
location_of_mid dle_card_2 = x;
y++;
break;
case 2:
location_of_mid dle_card_3 = x;
y++;
break;
}
}
x++;
}
Ditto ditto.
--
Chris "sekr" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Jul 18 '06 #2
Thanks, Chris, this is really helpful. I've cleared up stuff you said
in long-hand just to make sure but it's still not quite working - my
program, that is. I've commented out a bit in shuffle_cards which
randomizes but it's still very wierd with all these "signal" errors.
Here's the complete code:

#include <stdio.h>
#include <stdlib.h // for srand and rand
#include <time.h // for time

typedef struct {
int suit; /* The suit of the playing card */
int value; /* The value of the playing card */
} CARD;

/* Set up an integer to represent each suit */
enum {CLUBS = 4, DIAMONDS = 3, HEARTS = 2, SPADES= 1};

/* Set up integers to represent non numeric cards */
enum {ACE = 14, JACK = 11, QUEEN = 12, KING = 13};
CARD pack[51];
int where_are_cards[51];
enum {PACK = 0, PLAYER_HAND = 1, CPU1_HAND = 2, CPU2_HAND = 3, MIDDLE =
4};
void print_card(CARD card_to_print) {
if ( card_to_print.v alue < 11 ) { printf("%d",car d_to_print.valu e); }
if ( card_to_print.v alue == 11 ) { printf("Jack"); }
if ( card_to_print.v alue == 12 ) { printf("Queen") ; }
if ( card_to_print.v alue == 13 ) { printf("King"); }
if ( card_to_print.v alue == 14 ) { printf("Ace"); }

printf(" of ");

if ( card_to_print.s uit == 1 ) { printf("Spades" ); }
if ( card_to_print.s uit == 2 ) { printf("Hearts" ); }
if ( card_to_print.s uit == 3 ) { printf("Diamond s"); }
if ( card_to_print.s uit == 4 ) { printf("Clubs") ; }
printf("\n");
}

void shuffle_pack() {
srand(time(0)); // initialize seed "randomly"
int i,x,y,z = 0;

while (x < 4) {
while (y < 13) {
pack[x*y].value = y;
pack[x*y].suit = x;
y++;
}
y = 0;
x++;
}
/*
while (z<52) {
int r = rand() % 52; // generate a random position
CARD temp = pack[i]; pack[i] = pack[r]; pack[r] = temp;
z++;
}*/
}

void print_stats() {
int
location_of_pla yer_card_1,loca tion_of_player_ card_2,location _of_player_card _3;
int
location_of_CPU 1_card_1,locati on_of_CPU1_card _2,location_of_ CPU1_card_3;
int
location_of_CPU 2_card_1,locati on_of_CPU2_card _2,location_of_ CPU2_card_3;
int
location_of_mid dle_card_1,loca tion_of_middle_ card_2,location _of_middle_card _3;

int x,y = 0;
while (x<52) {
if (where_are_card s[x] == PLAYER_HAND) {
switch (y) {
case 0:
location_of_pla yer_card_1 = x;
y++;
break;
case 1:
location_of_pla yer_card_2 = x;
y++;
break;
case 2:
location_of_pla yer_card_3 = x;
y++;
break;
}
}
x++;
}

x = 0;
x = 0;

while (x<52) {
if (where_are_card s[x] == CPU1_HAND) {
switch (y) {
case 0:
location_of_CPU 1_card_1 = x;
y++;
break;
case 1:
location_of_CPU 1_card_2 = x;
y++;
break;
case 2:
location_of_CPU 1_card_3 = x;
y++;
break;
}
}
x++;
}

x = 0;
x = 0;

while (x<52) {
if (where_are_card s[x] == CPU2_HAND) {
switch (y) {
case 0:
location_of_CPU 2_card_1 = x;
y++;
break;
case 1:
location_of_CPU 2_card_2 = x;
y++;
break;
case 2:
location_of_CPU 2_card_3 = x;
y++;
break;
}
}
x++;
}

x = 0;
x = 0;

while (x<52) {
if (where_are_card s[x] == MIDDLE) {
switch (y) {
case 0:
location_of_mid dle_card_1 = x;
y++;
break;
case 1:
location_of_mid dle_card_2 = x;
y++;
break;
case 2:
location_of_mid dle_card_3 = x;
y++;
break;
}
}
x++;
}

printf("Table:\ n");
printf(" Your hand: "); print_card(pack[location_of_pla yer_card_1]);
printf(" "); print_card(pack[location_of_pla yer_card_2]);
printf(" "); print_card(pack[location_of_pla yer_card_3]);

}

void play_game() {
printf("\nYou will be playing against two computer players.\n");
printf("Shuffli ng pack... "); shuffle_pack(); printf("Done!\n ");

int i=0; while (i<52) { where_are_cards[i] = PACK; i++; } //
Initialize where_are_cards[51] array
i=0; while (i<3) { where_are_cards[i] = PLAYER_HAND; i++; } // Give
player 3 cards
i=3; while (i<3) { where_are_cards[i] = CPU1_HAND; i++; } // Give CPU1
3 cards
i=6; while (i<3) { where_are_cards[i] = CPU2_HAND; i++; } // Give CPU2
3 cards
i=9; while (i<3) { where_are_cards[i] = MIDDLE; i++; } // Put 3 cards
in the middle

print_card(pack[5]);

print_stats();
}

int main (int argc, const char * argv[]) {
printf("Welcome to the game of 31s!\n");
printf("This was created by Simon Goldring in 2006.\n");

int menu;
while ( menu != 1 ) {
printf("\nMAIN MENU:\n");
printf(" 0 - Play a game of 31s with the computer\n");
printf(" 1 - Quit 31s\n");
printf("? ");
scanf("%d", &menu);
if ( menu == 0 ) {
play_game();
}
}

printf("\nThank s for playing!");

return 0;
}

Jul 18 '06 #3
Simon wrote:
Thanks, Chris, this is really helpful. I've cleared up stuff you said
in long-hand just to make sure but it's still not quite working - my
program, that is. I've commented out a bit in shuffle_cards which
randomizes but it's still very wierd with all these "signal" errors.
Those are likely array-indexing errors. (You don't get a nice message
when that happens in C - things will go loudly wrong if you're lucky,
quietly wrong if you're unlucky.)

You should read a decent introductory C book - your previous language
is interfering and you need a bit of reprogramming!

Just a few quick notes at days end:
void shuffle_pack() {
srand(time(0)); // initialize seed "randomly"
int i,x,y,z = 0;
Only initialises `z`. `i`, `x`, and `y` are all junk.

int i = 0, x = 0, y = 0, z = 0;
or
int i = 0;
int x = 0;
int y = 0;
int z = 0;

int x,y = 0;
Ditto.
>
x = 0;
x = 0;
One of those should be `y = 0`?

x = y = 0;
x = 0;
x = 0;
Ditto.
x = 0;
x = 0;
Ditto.

--
Chris "seeker" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Jul 18 '06 #4
On 18 Jul 2006 07:57:15 -0700, in comp.lang.c , "Simon"
<Si***********@ gmail.comwrote:
>Thanks, Chris, this is really helpful. I've cleared up stuff you said
in long-hand just to make sure but it's still not quite working - my
program, that is. I've commented out a bit in shuffle_cards which
randomizes but it's still very wierd with all these "signal" errors.
Here's the complete code:
int i,x,y,z = 0;
This only initialises z to zero. Same problem appears several other
times.

Top tip: do not declare more than one variable on a line;
int i = 0;
int x = 0;
that way, you cannot make this sort of mistake.
(or this one:
FILE *a, b;
)
By the way, while some C compilers now allow you to declare variables
where used, most do not - you must declare all variables at the start
of a block. I worry that you are accidentally compiling this code as
C++.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 18 '06 #5
Simon wrote:
[...]
CARD pack[51];
int where_are_cards[51];
[...]

Last I checked, there are 52 cards in the deck.

CARD pack[52];
int where_are_cards[52];
int i,x,y,z = 0;
i, x, and y are not initialized here.

int i=0, x=0, y=0, z=0;
>
while (x < 4) {
while (y < 13) {
pack[x*y].value = y;
pack[x*y].suit = x;
y++;
}
y = 0;
x++;
}
You probably want "pack[x*13+y]" here.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jul 18 '06 #6
I'm changing all the initializing stuff, but I don't understand one
thing; doesn't an array tart at 0, so 52 items would be 0-51?

Thanks for all the help.

Jul 18 '06 #7
Simon said:
I'm changing all the initializing stuff, but I don't understand one
thing; doesn't an array tart at 0, so 52 items would be 0-51?
Sure, 52 items would be 0-51, but if you define it as being CARD pack[51],
that's 51 items, 0-50. In an array definition, the number between [ and ]
is the number of objects you want the array to contain, not the number of
the highest index in the array.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 18 '06 #8
aha! Cheers!

Jul 18 '06 #9
Simon wrote:
aha! Cheers!

Please quote enough of the previous message for context. Google does
this automatically now, so you have no excuse.


Brian
Jul 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
17657
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
4
47629
by: Danny | last post by:
Hi I don't understand why I keep getting "Error: Object expected" I tried different things and haven't been able to solve it. I'm a newbie so I'm not sure what I'm doing wrong. The debugger breaks at the <BODY> Tag of my code so I'm thinking the problem is with my InitPage() function. I appreciate the advice. Here is my code. <SCRIPT LANGUAGE="Javascript"> <!-- var flag = false;
7
3252
by: Robert Nicholson | last post by:
So I've got one page where I have an image inside a DIV with text-align: center and the image is correctly centered in that block. Making me think that text-align will center the contents of a block. Another page I have a series of thumbnails which are themselves a DIV block wrapping both an image and a caption (float: left) inside a DIV block with a fixed width which is smaller than the width of the browser and that DIV block is in...
3
1275
by: Lars | last post by:
Hello I write all my applications in VB6-- and I don't understand the reason to move to .NET (other than MS might break VB6 apps in future updates). I'm taking the position that I don't understand something, so I'm hoping some experienced people can clarify my position I write shareware apps that I only distribute through my own website. Download bandwidth is a major concern because that will cost me money Here are my main concerns *...
19
3875
by: LP | last post by:
I am using (trying to) CR version XI, cascading parameters feature works it asks user to enter params. But if page is resubmitted. It prompts for params again. I did set ReuseParameterValuesOnRefresh="True" in a viewer, but it still doesn't work. Did anyone run into this problem. What's the solution? Please help. Thank you
20
3763
by: johnnyboy7 | last post by:
I don't know a lot about ASP and I know even less about SQL. I have been trying to search the web for information to be able to get done what I need. I've tried piecing things together from many different sites, adding my own info in where needed, but it just isn't working. What I want to do is, using an existing database setup for a calendar application, pull information out of the database and display a title of an entry that corresponds...
21
2057
by: jehugaleahsa | last post by:
Hello: I had an hour-long discussion with my boss today. Last night, right before I dozed off, I realized some of his code resulted in duplicate processing. I tried to explain it to him and he kept saying, "I'm afraid to say it's 'OK' to change it because everything I did was for a reason." Well, I know better. My boss' old code is written in some cryptic C file with Oracle precompiler macros barfed all over it. Once I figured out how...
0
320
by: Stef Mientki | last post by:
Terry Reedy wrote: sorry, don't know how this happened, as I always copy/paste ? AFAIK locals() == sys._getframe(0).f_locals AFAIK, again one level up weird, I use it in 2.5 and if I remember well it already worked in 2.4. but exchanging the function with the statement yields exactly the same results Yes ;-) done.
7
1849
by: sara | last post by:
I have a friend doing some pro-bono work for a non-profit that does job training for distressed kids under DCSS care. He asked me for code to do the following (he's using A2003). I can't find code in searches or on the MVP and tool sites I visited - though I might not understand that there is code that just needs adaptation. ALL help is appreciated. He wants a routine (that I'd put into a module) to do the following:
0
8749
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9243
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7853
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2057
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.