我没有得到一个数字作为输出,而是一个表情符号
I am not getting a number as output but instead a emoji
在此处输入图像描述我编写了C++程序,该程序要求用户输入两个数字和运算符,并根据输入给出输出。我认为一切都是正确的,但输出不是所需的输出。
#include <iostream>
using namespace std;
int main()
{
int num1;
string op;
int num2;
string result;
cout << "Enter a number: ";
cin >> num1;
cout << "Enter a operator: ";
cin >> op;
cout << "Enter another number: ";
cin >> num2;
if (op == "+"){ //if user types '+' the result is num1 + num2
result = num1 + num2;
//cout << result;
}else if (op == "-"){ //if user types '-' the result is num1 - num2
result = num1 - num2;
// cout << result;
}else if (op == "*"){ //if user types '*' the result is num1 * num2
result = num1 * num2;
//cout << result;
}else if (op == "/"){ //if user types '/' the result is num1 / num2
result = num1 / num2;
//cout << result;
}else{
cout << "Invalid operator...";
}
cout << result;
return 0;
}
输出应为整数。但输出只是一颗钻石。
将变量结果声明为具有 int 类型
int result = 0;
或者也许最好将其声明为
long long int result = 0;
并像使用它一样使用
result = static_cast<long long int>( num1 ) * num2;
在您的程序中,它具有类型std::string
string result;
您正在为字符串分配一个整数值,所以我认为它是将该整数值转换为字符值并提供该表情符号。我的情况和你的一样,我 正在为字符串分配整数值。但是我随后将该整数值声明为字符值,然后它工作正常。
此外,最好的方法是使用 to_string(( 函数将整数值更改为字符值。
相关文章:
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 为什么不建议使用宏符号常量定义一个固定长度的数组呢
- 类模板上一个特定函数的未解析外部符号
- 使用无符号int作为二进制来在c++中实现一个集
- 编译了一个recent-C++.因此,如果没有暴露std符号,请在旧系统上运行
- 我没有得到一个数字作为输出,而是一个表情符号
- 为无符号字符* 分配一个数字
- C ++读取用户输入的字符一个一个符号
- 'char' c++ 类型的数组中的最后一个符号
- 如何在字符串C++中添加任何符号而不是另一个符号
- 定义一个符号,该符号可能是boost精神中文字功能的一部分
- 为什么我有一个"="符号输出和 2 个笑脸而不是正确的输出?C++
- C++标准是否要求有符号整数正好有一个符号位
- QT和未解决的下一个符号
- c++删除数组中的最后一个符号
- 解析一个符号是什么意思
- 函数与函数名之间有一个&符号
- 当一个符号替换几个符号时,是否存在从UTF-8到CP1251的音译?