简体   繁体   English

我正在尝试在 Vscode 上执行此代码,但每当我运行此代码时,它不会获取字符的值,而只是显示一个随机数

[英]I'm trying to execute this code on Vscode but whenever I'm running this it's not taking the value of character but just showing a random number

I'm new to coding and learning C++ online but while running this code it just doesn't takes the value of the character.我是在线编码和学习 C++ 的新手,但是在运行此代码时,它只是不采用字符的值。 If using cin or cout it's working fine but scanf is giving me problems.如果使用cin或 cout 它工作正常,但scanf给我带来了问题。 Can someone explain me what am I doing wrong in the most basic way possible有人可以以最基本的方式向我解释我做错了什么吗

    #include<iostream>
    #include<cstdio>
    #int main(){

    int num1, num2;
    char oper;

    printf("The first number is: ");
    scanf("%d", &num1);
    printf("The second number is: ");
    scanf("%d", &num2);
    printf("The operation you want to perform is: ");
    scanf("%c", &oper);

    if(oper == '+'){
        int total= num1+num2;
        printf("%i", &total);
    }

    else if(oper == '*'){
        int mul= num1 * num2;
        printf("%d", &mul);
    }

    else{
        int diff = num1-num2;
        printf("%d", &diff);
    }

    return 0;
}
  • You have an extra # before int main() .你有一个额外的#int main()之前。
  • %d leaves newline character after the number read and %c will read it if it is there. %d在读取的数字后留下换行符,如果存在, %c将读取它。 You should add a space character befoer %c to have scanf() whitespace characters (including newline character) to have it ignore space characters.您应该在%c之前添加一个空格字符以使其具有scanf()空白字符(包括换行符)以使其忽略空格字符。
  • %i and %d in printf() expects int . printf()中的%i%d需要int Passing int* there invokes undefined behavior .在那里传递int*会调用未定义的行为

Fixed code:固定代码:

#include<iostream>
#include<cstdio>
int main(){ // remove extra #

    int num1, num2;
    char oper;

    printf("The first number is: ");
    scanf("%d", &num1);
    printf("The second number is: ");
    scanf("%d", &num2);
    printf("The operation you want to perform is: ");
    scanf(" %c", &oper); // add a space

    if(oper == '+'){
        int total= num1+num2;
        printf("%i", total); // pass the number itself, not a pointer
    }

    else if(oper == '*'){
        int mul= num1 * num2;
        printf("%d", mul); // pass the number itself, not a pointer
    }

    else{
        int diff = num1-num2;
        printf("%d", diff); // pass the number itself, not a pointer
    }

    return 0;
}

Also your code will be better if you add some code to check return values of scanf() to check if they successfully read expected things.此外,如果您添加一些代码来检查scanf()的返回值以检查它们是否成功读取预期内容,您的代码也会更好。

暂无
暂无

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

相关问题 当我尝试运行代码时,在 vscode 中出现奇怪的错误 - Getting an weird error in vscode when i'm trying to run a code 这段代码有什么问题?。 几分钟前我刚刚学习了编程,我正在努力做到这一点 - What's wrong with this code?. I just learned programming a few minutes ago and I'm trying to make this 我正在尝试将s1内的所有字符更改为“ x”。 但是,当我运行代码时,编译器仅打印了11次“ hello world” - I'm trying to change all character within s1 to 'x'. When I run the code, however, the compiler just printed out 'hello world' 11 times c++。 我试图通过在开关内使用数组来获取用户输入,但是当我运行代码时它显示分段错误? - c++ . I'm trying to get user input by using array inside switch but when i run the code it's showing segmentation fault? 我试图在这一部分找到一个质数 - I'm trying to find a prime number at this section 我试图在数组中得到一个随机元素,为什么我的简单代码不起作用? - I'm trying to get a random element in an array, why wont my simple code work? 我无法在 VSCode 中调试我的 C++ 代码 - I'm Unable to debug my C++ code in VSCode 关于字符数组,我试图将数组中的每个字符分配给另一个值 - Regarding char arrays, I'm trying to assign each character in my array to another value 我正在尝试从 LIS3DH 加速度计获取读数,但我正在获取随机数 - I'm trying to get readings from the LIS3DH accelerometer and I'm getting random numbers 为什么每当我尝试运行此 LinkedList delete function 时都会出现分段错误错误? - Why am I getting a segmentation fault error whenever I'm trying to run this LinkedList delete function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM