arcade style name input function

This is a discussion on arcade style name input function within the C Programming forums, part of the General Programming Boards category; ok, i'm making a game and i want it to have an arcade style name input function if you get ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    Question arcade style name input function

    ok, i'm making a game and i want it to have an arcade style name input function if you get a high score.
    it needs to use the up and down keys to change the letter and then enter to accept and go to the next letter and it needs to be 3 characters long.
    i have everything working fine but when i try to print the string after the name is input it also prints a couple of odd symbols after it.

    i have no idea why it's doing this or how to fix it, any help would be greatly appreciated.

    Heres the code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    char change(char name,char a);
    int main()
    {
    	_setcursortype(_NOCURSOR);
    	char init[3],a;
       int i;
       init[0]='A';
       init[1]='A';
       init[2]='A';
       textcolor(2);
       cprintf("Please input your initials.\n\r");
       for (i=0;i<3;i++)
       {
       	a='a';
       	printf("%c",init[i]);
       	while (1)
       	{
       		if(kbhit())
          	{
                a=getch();
    	   		init[i]=change(init[i],a);
                gotoxy(i+1,2);
             	printf("%c",init[i]);
          	}
          if (a=='\r')
          	break;
       	}
       }
       fflush(stdin);
       printf("\n\n\n%s",init);
       getch();
    }
    char change(char init,char a)
    {
       if (a=='H')
       	init++;
       if (a=='P')
       	init--;
       if (init=='@')
       	init='Z';
       if (init=='[')
       	init='A';
       return init;
    }
    Thanx in advance.
    -----------------------
    Windows XP.
    borland c++ 5.00.
    Today is the tommorow we feared yesterday.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't leaving room for the null character. Now, you don't have to if you don't want to, but you can't treat it as a string:
    Code:
    printf("%s"...
    You'd have to print the three characters individually.

    Or, just expand your array by one, so you can have the last spot be a null. ( '\0' )
    Oh, and don't "fflush(stdin)", as it's undefined behavior, and is frowned upon by people with great big frowns.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21