首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在c中为if语句提供数组输入?

如何在c中为if语句提供数组输入?
EN

Stack Overflow用户
提问于 2020-05-19 23:56:20
回答 3查看 69关注 0票数 1
代码语言:javascript
复制
# 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;
}

我有一个问题,当我输入一个YouTubers名称(完整名称)作为输入,我得到一个问题,常量太长,没有给出正确的结果,而且它还说常量字符对于它的类型来说太长

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-20 00:10:58

您需要为输入分配更多的空间。类型char只能包含一个字符。您需要一个由char组成的数组。可以这样声明:

代码语言:javascript
复制
char b[30];

这将包含30个字符。

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

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

请阅读相关文档:strcmp

由于您是初学者,我还建议您查看一些关于C. String Functions中字符串的教程。

票数 1
EN

Stack Overflow用户

发布于 2020-05-20 00:14:58

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

代码语言:javascript
复制
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的字符串库函数。

票数 0
EN

Stack Overflow用户

发布于 2020-05-20 00:18:06

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

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

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61895374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档