所以,我正在try 制作一个基于文本的游戏来帮助人们学习Linux,我正在努力做到这一点,如果你的名字是Linus Torvalds,而不是你的向导是Linus Torvalds,那么它将是Richard Stallman,问题是,无论输入任何名字,打印屏幕总是会显示向导是Richard Stallman. (使用C btw)

代码如下:

!--语言:C-->


#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,普通的条件,但它不起作用,如果有人有答案,它是非常感激的.

附注:

请try 在您的答案中包括代码和/或使他们尽可能详细,塔克斯.

推荐答案

使用带有%s格式说明符的scanf将仅读取单个单词.如果你想读一整行由几个单词组成的单词(例如"Linus Torvalds\n"),那么我建议你用fgets代替.然而,关于混合fgetsscanf,要注意this problem.此外,有关如何从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.
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.

C++相关问答推荐

C sscanf没有捕获第二个参数

带双指针的2D数组

Zig将std.os.argv转换为C类型argv

丑陋的三重间接:可扩展的缓冲区管理 struct

如何将长字符串转换为较小的缩写,该缩写由第一个字符、最后一个字符和中间的字符数组成?

C++中矢量类型定义和数据保护的高效解决方案

致命:ThreadSaniizer:在Linux内核6.6+上运行时意外的内存映射

是否可以通过调用两个函数来初始化2D数组?示例:ARRAY[STARTING_ROWS()][STARTING_COLUMNS()]

在移动数组元素时获得意外输出

如何将C中的两个字符串与从文件接收的字符串中的字符数进行比较

初始成员、公共初始序列、匿名联合和严格别名如何在C中交互?

为什么我的旧式&q;函数在传递浮点数时会打印2?

在下面的C程序中,.Ap0是如何解释的?

共享目标代码似乎不能在Linux上的进程之间共享

%g浮点表示的最大字符串长度是多少?

将char*铸造为空**

使用 strtok 多次分割一个字符串会导致意外行为

cs50拼写器分配中的无限循环

我该如何处理这个 C 90 代码中的内存泄漏?

如何确定 C 程序中的可用堆内存