String.at() 返回时髦的数字
String.at() returning funky numbers
当我尝试运行这段代码时,我遇到了一些故障,我无法查明原因。前两个"cout"行显示数字 7 和 3,但是,最后一个"cout"行通常显示 50-60 范围内的数字(在我运行它的那一刻,我得到 55 和 51,这似乎以某种方式与我试图读取的数字相关联)。据我从一些谷歌搜索和我手头的书中可以看出,这应该是有效的,但显然我错过了一些东西。谢谢你的时间。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string text = "73";
int one=0, two=0;
cout << text.at(0) << endl;
cout << text.at(1) << endl;
one = text.at(0);
two = text.at(1);
cout << one << endl << two << endl;
return 0;
}
程序运行正常:text.at()
返回一个char
,你隐式地将其转换为int
。然后打印该 int 的值,分别为 "7" 的 55 和"3" 的 51(看这里)。
相关文章:
- 函数如何使用引用返回所需的数字?
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- 返回 int 中可被 2 c++ 整除的所有数字的总和
- C++ python 中的函数返回数字而不是字符串
- 使用非托管导出将字符串从 C# 返回到C++将返回数字
- 我的代码应该接受一个数字,并返回字母等级或"Grade is not valid"但 else 语句不起作用
- 如何返回一串字母数字字符中的最大值(例如-1A003B3)?
- 将 MPFR 数字转换为字符串并返回
- C++ 递归:返回数字的最小偶数
- typeid.name 返回派生类类型之前的数字
- Fibbonaci 递归代码返回错误值,始终返回下一个数字
- 为什么我的阶乘数查找器返回以C++输入的数字?(已编辑)
- 尝试编写函数尝试从字符串中提取数字,但它返回不相关的数字
- 随机函数在循环中调用时返回相同的数字!C++
- 将字符转换为ASCII返回数字太大
- 为什么此范围基于语句返回一个数字太低的范围
- 根据另一个数字的可整除性返回子数组中的索引
- 如果数组中有重复的数字,如何返回'false'?
- 递归计算满足条件的值数并返回该数字
- C++余数以较高的数字返回 7