在C中创建虚拟助手

时间:2015-09-27 08:34:50

标签: c

我目前正在尝试在C中制作一个小型“虚拟助手”;非常基本的。但是我遇到了一些输出error: incompatible types when assigning to type 'char[550]' from type 'char *';之类的错误。

这是我的代码:

int main()
{
    char userName[35];
    char qInput[550];

    printf("Hi! What's your name?\n\a");
    scanf("%s", userName);

    printf("\nHello, %s! My name is EDI. I'm a friendly piece of AI you can talk to!\n\n\a", userName);
    printf("\nWhat would you like to ask me? Please keep in mind to use proper spelling \nor I won't understand the question.\n\a");

    scanf("%s", qInput);

    //what's your name?

    if(qInput = "What's your name"){
        printf("My name is EDI!");
        scanf("%s", qInput);
    } else if(qInput = "What's your name?"){
        printf("My name is EDI");
        scanf("%s", qInput);
    } else if(qInput = "Whats your name?"){
        printf("My name is EDI");
        scanf("%s", qInput);
    } else if(qInput = "What's your name"){
        printf("My name is EDI");
        scanf("%s", qInput);

    //age

    } else if(qInput = "How old are you?"){
        printf("I was born like eight minutes ago...");
        scanf("%s", qInput);
    } else if(qInput = "How old are you"){
        printf("I was born like eight minutes ago...");
        scanf("%s", qInput);
    } else if(qInput = "What is your age?"){
        printf("Eight or so minutes old.. Give or take");
        scanf("%s", qInput);
    } else if(qInput = "What is your age"){
        printf("Eight or so minutes old.. Give or take");
        scanf("%s", qInput);
    }
}

请帮帮我!我是C的新手,想继续学习新东西和新技术。我可以改变什么才能使它按照应有的方式工作?我对php很有经验,所以我理解if / else方面的事情..

谢谢!

1 个答案:

答案 0 :(得分:1)

首先,=是分配,==是C中的比较运算符。

也就是说,您无法使用==运算符比较字符串,就像您在

中所做的那样
 if(qInput = "What's your name")  //note: not even == here.

您必须使用strcmp()并检查返回值以检查相等性。