简体   繁体   English

如何将用户输入存储为c中的变量?

[英]How do you store user input as a variable in c?

I am a very newbie programmer, and have no clue whatsoever what I'm doing. 我是一个非常新手的程序员,对我正在做的事情一无所知。 besides reading documentation. 除了阅读文档。

My program isn't giving the user time to input the unwires right away, its just saying the answer for no. 我的程序没有给用户时间立即输入断开连接,只是说出了答案。 What did I do wrong? 我做错了什么?

I made this program to be a funny joke for my friends (like an AI gone wrong) 我把这个程序当成对我朋友们的有趣笑话(就像AI出了问题)

Here is my code: 这是我的代码:

#include <stdio.h>

int main() {

    int yorn;

    printf("do you have friends? please awnser yes or no.");
    scanf("%d", &yorn );

    if (yorn = "yes") {
        printf("no, you dont. please reload the program if you want to change your awnser.");
    }
    else if (yorn = "no") {
        printf("i can be your friend. your BEST friend.");
    }
    return 0;
}

For comparison, you have two use strcmp not = . 为了进行比较,您有两个使用strcmp not = Also, you are taking int type for yorn and comparing with the string. 另外,您将int类型yorn并与字符串进行比较。 Change yorn type to char[] and read as %s in scanf . 将yorn类型更改为char[]并在scanf读为%s

Change your code to following the code. 将您的代码更改为遵循代码。 look closely to understand it: 仔细观察以了解它:

int main() {

    char yorn[20]; //set max size according to your need

    printf("do you have friends? please awnser yes or no.");
    scanf("%19s", yorn); // Use %s here to read string.

    if (strcmp(yorn, "yes") == 0) { //Use ==
        printf("no, you dont. please reload the program if you want to change your awnser.");
    }
    else if (strcmp(yorn,"no") == 0) { // Use ==
        printf("i can be your friend. your BEST friend.");
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM