名称comparisson为文字为基础的游戏写在C [重复]

rggaifut  于 2023-08-03  发布在  其他
关注(0)|答案(1)|浏览(90)

此问题在此处已有答案

How do I properly compare strings in C?(10个答案)
28天前关闭。
所以,我试图使一个基于文本的游戏,以帮助人们学习linux,我试图使它这样,如果你的名字是Linus Torvalds而不是你的指南是Linus Torvalds它将是理查德·斯托曼,问题是,与任何名称输入打印屏幕总是说,该指南是理查德·斯托曼.(使用C btw)
代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Global variables
char name[15];
char yesno[1];

//Main
int main(){
    system("clear");
    printf("???: Hello adventurer, are you here to learn Linux?\n");
    printf("y/n\n");

    scanf("%c", yesno);

    if (strcmp("n", yesno)){
        printf("???: Great!, What is your name?\n");
        
        scanf("%s", name);
        
        if("Linus Torvalds\n"){
            printf("Achievment complete: In God We Trust\n");
            printf("Nice to meet you!, my name is Richard Stallman.\n");
        }else {
            printf("Nice to meet you!, my name is Linus Torvalds.\n");
        }
    }
    else if (strcmp("y", yesno)){
        printf("Ok!, bye!");
    }else {
        printf("That's not an answer.\n");
    }
}

字符串
我试过strcmp,mallocs,平原条件,但它不工作,如果有人有一个答案,这是非常apreciated。
P.S.
请尝试包括代码在您的答案和/或使他们尽可能详细,谢谢。

xqkwcwgp

xqkwcwgp1#

scanf%s说明符一起使用将只读取单个字。如果要阅读由几个单词组成的整行(例如,"Linus Torvalds\n"),那么我建议您改用fgets。但是,要注意this problemfgetsscanf的混合使用。另外,有关如何从fgets输入中删除换行符的信息,请参见this question。我建议您完全不要使用scanf进行用户输入,而应始终使用fgets
条件if("Linus Torvalds\n")没有意义,因为该条件将始终为真。如果要将name的内容与"Linus Torvalds\n"进行比较,则需要使用strcmp函数。
还有,行

if (strcmp("n", yesno)){

字符串
是错误的,因为函数strcmp要求它的两个参数都是指向字符串的指针,即转换为以空字符结尾的字符序列。但是,yesno不是指针。&yesno也是错误的,因为这样的指针只指向单个字符,而不是指向以空字符结尾的字符序列。
如果两个字符串匹配,则函数strcmp将返回零,如果不匹配,则返回非零。因此,您可能希望将strcmp的返回值与零进行比较。
我建议你这样重写你的程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void get_line_from_user( const char prompt[], char buffer[], int buffer_size );

int main( void )
{
    char input[200];

    get_line_from_user(
        "Hello adventurer, are you here to learn Linux? (y/n) ",
        input, sizeof input
    );

    if ( strcmp( input, "y" ) == 0 )
    {
        get_line_from_user(
            "Great! What is your name? ",
            input, sizeof input
        );
        
        if ( strcmp( input, "Linus Torvalds" ) == 0 )
        {
            printf( "Achievement complete: In God We Trust\n" );
            printf( "Nice to meet you! My name is Richard Stallman.\n" );
        }
        else
        {
            printf( "Nice to meet you! My name is Linus Torvalds.\n" );
        }
    }
    else if ( strcmp( input, "n" ) == 0 )
    {
        printf( "Ok! Bye!" );
    }
    else 
    {
        printf( "That's not an answer.\n" );
    }
}

//This function will read exactly one line of input from the
//user. It will remove the newline character, if it exists. If
//the line is too long to fit in the buffer, then the function
//will automatically reprompt the user for input. On failure,
//the function will never return, but will print an error
//message and call "exit" instead.
void get_line_from_user( const char prompt[], char buffer[], int buffer_size )
{
    for (;;) //infinite loop, equivalent to while(1)
    {
        char *p;

        //prompt user for input
        fputs( prompt, stdout );

        //attempt to read one line of input
        if ( fgets( buffer, buffer_size, stdin ) == NULL )
        {
            printf( "Error reading from input!\n" );
            exit( EXIT_FAILURE );
        }

        //attempt to find newline character
        p = strchr( buffer, '\n' );

        //make sure that entire line was read in (i.e. that
        //the buffer was not too small to store the entire line)
        if ( p == NULL )
        {
            int c;

            //a missing newline character is ok if the next
            //character is a newline character or if we have
            //reached end-of-file (for example if the input is
            //being piped from a file or if the user enters
            //end-of-file in the terminal itself)
            if ( (c=getchar()) != '\n' && !feof(stdin) )
            {
                if ( c == EOF )
                {
                    printf( "Error reading from input!\n" );
                    exit( EXIT_FAILURE );
                }

                printf( "Input was too long to fit in buffer!\n" );

                //discard remainder of line
                do
                {
                    c = getchar();

                    if ( c == EOF )
                    {
                        //this error message will be printed if either
                        //a stream error or an unexpected end-of-file
                        //is encountered
                        printf( "Error reading from input!\n" );
                        exit( EXIT_FAILURE );
                    }

                } while ( c != '\n' );

                //reprompt user for input by restarting loop
                continue;
            }
        }
        else
        {
            //remove newline character by overwriting it with
            //null character
            *p = '\0';
        }

        //input was ok, so break out of loop
        break;
    }
}


此程序具有以下行为:

Hello adventurer, are you here to learn Linux? (y/n) y
Great! What is your name? Jimmy
Nice to meet you! My name is Linus Torvalds.

x

Hello adventurer, are you here to learn Linux? (y/n) y
Great! What is your name? Linus Torvalds
Achievement complete: In God We Trust
Nice to meet you! My name is Richard Stallman.
Hello adventurer, are you here to learn Linux? (y/n) n
Ok! Bye!
Hello adventurer, are you here to learn Linux? (y/n) sdfsdgf
That's not an answer.

的一个或多个字符

相关问题