开发者

Let a string grow by input

following problem:

I want to make a kind of hangman game (everything in the console). so i made a loop which turns 13 times after it runs out, the player loose (it only count down if the player inserts a wrong letter). now, i want to show the user which letters he allready used. so the output should look like this: "you allready used: a, b, c, g..." and so on. So after every try, the line grow by one letter (the input letter of course). i tried strcpy, but it only makes random letters which i never put in, and it doesn't grow, so how can i handle this?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <ctype.h>

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main() 

{
char word[81], used[14];
int wrong=0, laenge, _, i;
char input;


SetConsoleTitle(" Guess me if u Can! ");



//printf("\n\n spielst du mit einem Freund oder alleine?"); /*for later
//printf(" \n\n [1] alleine"
//       " \n\n [2] mit einem Freund");                                    */


printf("\n\n please insert a word (max. 80 characters): \n\n");

gets(word开发者_StackOverflow);

    laenge=strlen(word);

    printf("\n\n this word has %i characters.\n\n",laenge);

    for(i=0; i<13; i++)
    {
//                    for(_=0; _<laenge; _++) /*ignore this this is also for later 
//                    printf(" _");
//                    printf("\n");                                           */

    gotoxy(10,10);
    printf("\n\n please insert a letter now:  ");
    input=getch();
    strcpy(used, &input);
    printf("\n\n The following characters are allready used: %c ", used);

    if(strchr(word, input)){
                      printf("\n\n %c is in the word\t\t\t\t\t\t\n\n");
                      i--;

    }
                  else{
                      printf("\n\n the letter %c is wrong!\n");
                      wrong++;
                      printf(" you have %i try",13-wrong);
    }

  }
    system("cls");
    printf("\n\n to many tries.\n\n");





system("Pause");


}


First, you should fill up used with 0 characters to ensure it is always properly terminated:

memset(used, 0, 14);

Then, add a new character to it like this:

used[i] = input;

Also, as @Fred noted, you should use the proper format specifier %s in the printf call.


As already said here you should fill used with zeros, something like used[14] = {0};

Then I think the line printf("\n\n The following characters are allready used: %c ", used); should be printf("\n\n The following characters are allready used: %s ", used);, notice the "%s" you're printing a string.


If you know the maximum size, you can create a buffer with that maximum size, and then append to it. In this case you do know the maximum size, as there are only 26 letters in the alphabet. So the maximum length of the string is the length of whatever text you put at the beginning, plus 26 times the number of characters you'll use for each letter. I count 18 in the initial string. Remember to add one for the null byte terminator at the end. For each letter you have the letter, a comma, and a space, so the max length is 18 + 26*3 + 1 = 97 if I did the arithmetic right.

So you could write something like:

char used[96];
strcpy(used,"You already used: ");
int first=TRUE;
... whatever other work ...
... let's say we get the character in variable "c" ...
// Add comma after previous entry, but only if not first
if (!first)
{
  first=FALSE;
  strcat(used,", ");
}
// turn character into a string
char usedchar[2];
usedchar[0]=c;
usedchar[1]='\0';
// Append to working string
strcat(used,usedchar);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜