Re: (C) missing semi-colon

From: Jack Klein (jackklein_at_spamcop.net)
Date: 11/05/04


Date: Thu, 04 Nov 2004 21:23:11 -0600

On Thu, 4 Nov 2004 21:18:40 -0500, "Paul Fedorenko"
<pfedorenko@look.ca> wrote in alt.comp.lang.learn.c-c++:

> Actually... if you don't mind me asking another question... if you look at
> main(), there's a line where my function game_menu() is called. The program
> basically skips that part, and just executes the next line. am I maybe
> calling the function wrong?
>
> is writing: void game_menu(player1); alright as a function prototype?
> Or do I have to use something like
> void game_menu(player1[20]);
> or possibly void game_menu(&player1); instead when passing a string?
>
> I know the first one is fine for a single character char variable.

What do you mean, "skips that part"? It looks to me like it would
print the prompts but not accept any input. Let's look at it...

> void game_menu(char player1[20])
> {
> int year;
> int choice;
>
> for (year = 1; year >= 3; year++)
> {
> // system("cls");
> printf("Year %d", year);
> printf("1) Agressive\t2) Withdraw\t3) Cooperate\n\n");
> printf("Enter your strategy for year %d\n\n", year);
> switch ( choice )

Notice that you define 'choice' as a local automatic variable at the
top of the function and since you don't initialize it it has some
random value. Using that value causes undefined behavior.

Then you output three lines of text to the user, informing him/her
about the input they should provide, AND DON'T HAVE ANY INPUT FUNCTION
CALL. So the code just switches on the uninitialized value of
'choice'. Perhaps a call to let the user provide the input would
improve things.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html