【问题标题】:how to give a array input for if statement in c?如何在c中为if语句提供数组输入?
【发布时间】:2023-04-05 11:58:01
【问题描述】:
# include <stdio.h>


void danidev(void){
 printf("Dani is a YouTuber and an indie game developer and an fps game developer having his game published in play store he is 22 years old and goes to a university");
}

void brackeys(void){
    printf("brackeys is a YouTuber and an indie game developer and also an fps game developer having most of his games published in itch.io and has a team which works on game development");
}

void blackthornprod(void){
    printf("  they are two brothers who create video games and teach others how to do the same over on Youtube and Udemy ! they are passionate in sharing their knowledge and game creation journey with other aspiring game developers.");
}

void jabrils(void){
 printf("jabrils is a ai programmer and also a machine learning pro coder and also a game developer he has made a lot of ai and has saved millions of people from their tough times");
}

void codingbullet(void){
 printf("coding bullet is a multi intelligent ai developer and also a master in machine learning also he owns a youtube channel with 2.06 million subscribers");
}

int main(){

 printf("HERE IS THE INFORMATION OF FAMOUS CODING YOUTUBERS(PLS TYPE THE FOLLWOING YOUTUBERS NAME): ");
 char b;
 scanf("%c",&b);
 if(b=='danidev'){
 danidev(); 
 }
 else if(b=='brackeys'){
 brackeys();
 }
 else if(b=='blackthornprod'){
 blackthornprod();
 }
 else if(b=='jabrils'){
 jabrils();
 }
 else if(b=='codingbullet'){
 codingbullet();
 }
 else{
 printf(" i dont know what you are taking about");
 }

return 0;
}

当我输入 YouTube 用户的姓名(全名)作为输入时遇到问题,我遇到了一个问题,即常量太长并且无法给出正确的结果,而且它说常量字符对于它的类型来说太长了

【问题讨论】:

  • 使用双引号 "" 创建字符串文字,而不是单引号。创建一个char 数组来存储scanf 的结果,并使用%s 读取字符串而不是%c 读取单个字符。
  • 感谢克里斯蒂安·吉本斯的帮助
  • 我建议您按照自己的方式完成初级 C 编程课程。网上有很多免费的。
  • 嗨 bob Jarvis,我刚开始学习 c,我是一个 12 岁的孩子(有多聪明)我在 education.io 中自学 c @Bob Jarvis - Reinstate Monica

标签: c arrays if-statement input scanf


【解决方案1】:

您需要为输入分配更多空间。 char 类型只能容纳一个字符。你需要一个chars 的数组。这可以声明如下:

char b[30];

这将包含 30 个字符。

对于字符串文字,您应该使用双引号而不是单引号。例如"danidev" 而不是 'danidev'

比较字符串(字符数组)时,您应该使用strcmp() 函数。

请阅读相关文档:strcmp

既然你刚开始,我还建议你看一些 C 字符串的教程。String Functions

【讨论】:

    【解决方案2】:

    有几个错误,来自字符串比较、字符串定义和 char/char 数组变量。有时它看起来像 JS,但要注意很大的差异。 除此之外,我看到在写这篇文章时添加了另一个答案,我想补充一下,注意 scanf 中数组的长度,这样你就不会在运行时引发缓冲区溢出错误。

    您可以在这里查看:https://onlinegdb.com/HyrgvK-sL

    #include <stdio.h>
    #include <string.h>
    
    void danidev (void)
    {
      printf ("Dani is a YouTuber and an indie game developer and an fps game developer having his game published in play store he is 22 years old and goes to a university");
    }
    
    int main ()
    {
    
        printf("HERE IS THE INFORMATION OF FAMOUS CODING YOUTUBERS(PLS TYPE THE FOLLWOING YOUTUBERS NAME): ");
        char b[32];
        scanf("%31s", b);
    
        if (strncmp(b,"danidev", 32)== 0) 
        {
            danidev ();
        }
        else
        {
            printf (" i dont know what you are taking about");
        }
        return 0;
    }
    

    【讨论】:

    • 嗨,请您解释一下 if (strncmp(b,"danidev", 32)== 0) 它的作用和方式
    • 当然,strncmp 会进行字符串比较。在 C 中,字符串是一个以空字符结尾的字符数组。额外的 n 是字符串的最大长度,因此您永远不会进入禁止的位置。在这种特殊情况下,您也可以使用 strcmp,因为第二个字符串小于 32,它将首先终止,但我认为使用 strncmp 是一个好习惯。
    【解决方案3】:

    您不能使用相等运算符 (==) 直接比较字符串。相反,使用 strcmp() 比较两个字符串。

    char str[20];
    gets(str);
    
    if(!strcmp(str, "danidev") danidev();  //Use double quotes for strings, instead of single quotes (used for character literal).
    //Same as strcmp(str, "danidev") == 0
    //rest of the code
    

    另外,如果你是初学者,我建议你检查一些 C 的 String 库函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-22
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多