Re: A89: C problem...


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

Re: A89: C problem...




oh crap.. read below...

Robin Kirkman wrote:
> 
> Here, I changed your code and fixed it. It -should- work now.
> --robin
> 
> JHill8075@aol.com wrote:
> >
> > 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,char *initials)
> This has to be a char*, not an int*. If you make it int, it really sucks :/
> 
> > {
> >
> > int count,key=0,x=0;
> 
> >         clrscr();
> >         printf("Game Over");
> >         printf("\nYour score is %d.",score);
> >         if(score<=*highscore)
> >             printf("\nHighscore is %d by %s",*highscore,initials);
> 
> You need to use %s to display a string. %c displays only a character.
> Also, i changed *initials to just initials. THis is because %s expects a
> character pointer, not a character.
> 
> >
> >         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);
> >                     }
> >                     *initials=key;
> >
> >                     initials++;
> >                     key=0;
> >             }
> >             }
> >         GKeyFlush();
> >         GKeyIn(NULL,0);
> >         clrscr();
> >         return;
> > }
> > //...more code here...
> > //in _main
> > static int initials[3]={0,0,0,NULL};

i forgot... the above line should have been:
static char initials[4]={0,0,0,0};

and it should be global, not local only to main.
it doesn't -have- to be global, but that makes it easier to understand. ;)


> > //...rest of program
> 
> there you go, that should work ;)



References: