A89: Re: C problem...


[Prev][Next][Index][Thread]

A89: Re: C problem...




> I'm gaving a problem with my C program (problamatic code is below). I'm
> working on the highscore and I decided to allow the user to put his/her
> initials in. The program displays the initials while the user is typing them
> in, but then when you play it the second time it's supposed to show you who
> has the highscore and what their initials are, but it only displays their
> first initial. I consulted another C programmer for the 89 ,and we
> re-arranged and re-wrote the code in different ways (including changing it
> from a char variable to an int variable which it is now), but the bug(?) is
> still there. Can anyone figure this out? Thanks a lot.
>
>         Josh
>         <A HREF="http://pa.ticalc.org">Programmers Anonymous</A> Member
>
> void end(int score,int *highscore,int *initials)
> {
>
> int count,key=0,x=0;
>         clrscr();
>         printf("Game Over");
>         printf("\nYour score is %d.",score);
>         if(score<=*highscore)
>             printf("\nHighscore is %d by %c",*highscore,*initials);

*highscore is correct, but *initials is not. It means "the value that <initials>
points to", but you want "pointer to initials" to print a string, i.e. without
the "*".
Also, to want to print a string (in C; array of char:s) the correct printf
directive is "%s".

>
>         if(score>*highscore)
>             {
>             printf("\nOld High Score was %d",*highscore);
>             *highscore=score;
>             printf("\nNew High Score of %d!",score);
>
>             printf("\nEnter your initials.\n");
>             for(count=0;count<=2;count++)
>             {
>                     GKeyFlush();
>                     while(key==0)
>                     {
>                         key=getchar();
>                         ST_busy(ST_IDLE);
>                     }

Just curious: why not ngetchx()?

>                     *initials=key;
>
>                     initials++;
>                     key=0;
>             }
>             }
>         GKeyFlush();
>         GKeyIn(NULL,0);
>         clrscr();


>         return;

Not strictly necessary since the function doesn't return any value and since the
function is already done anyway.

> }
> file://...more code here...
> file://in _main
> static int initials[3]={0,0,0,NULL};

Char was right from the beginning, plus it holds 4 characters: 3+zero
termination.
Change to:
   static char initials[4] = {0,0,0,0};

> file://...rest of program



 / Niklas Brunlid
Check out Prosit for the TI-89 / TI-92+ at http://prosit.ticalc.org
Random PQF Quote follows:

- "Outside! What's it like?"
- "Well -- It's sort of big"
        -- (Terry Pratchett, Truckers)





Follow-Ups: References: