【发布时间】:2021-05-15 13:12:55
【问题描述】:
我是在线编码和学习 C++ 的新手,但是在运行此代码时,它不会获取字符的值。如果使用 cin 或 cout 它工作正常,但 scanf 给我带来了问题。有人可以用最基本的方式解释我做错了什么
#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;
}
【问题讨论】:
-
PS D:\C, C++ Projects\C++> cd "d:\C, C++ Projects\C++\" ; if ($?) { g++ sharma.cpp -o sharma } ; if ($?) { .\sharma } 第一个数字是:54 第二个数字是:45 你要执行的操作是:6422280 这是这个的输出
-
#int main(){你打错字了 -
您期待什么?附:这看起来更像是 C 而不是 C++
-
两个注意事项:
#include <iostream>在这里什么都不做,因为代码不使用该标头中的任何内容。而#include <cstdio>将名称(printf和scanf是这里使用的名称)放在命名空间std中,它可能也将它们放在全局命名空间中。对于直接 C像这样的代码,使用#include <stdio.h>。或者,用<cstdio>,写std::printf和std::scanf。
标签: c++