- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 9 个字符的字符串读入 9 个整数值,并存储在一个数组中(现在我将它们存储在 9 个单独的整数中,一旦它们读入 OK 就会将它们放入数组中)。我采用的一般方法:con 字符串,将其拆分为 9 个字符值,将 (atoi) 每个转换为一个整数并存储为 9 个整数,将整数放入数组中。奇怪的是,虽然单个值毫无问题地拆分为单个字符,但以某种方式“看到”其他相邻值(根本不包含在该字符中!)并将它们向后转换。
示例代码:
countrows = 1;
countcols = 1;
cout << endl << "Enter values for boxes in row " << countrows << ", enter 0 for open boxes (enter 9 numbers, with no spaces or delimiters): ";
string inputline;
cin >> inputline;
char col1, col2, col3, col4, col5, col6, col7, col8, col9;
int int1, int2, int3, int4, int5, int6, int7, int8, int9;
col1 = inputline[0];
col2 = inputline[1];
col3 = inputline[2];
col4 = inputline[3];
col5 = inputline[4];
col6 = inputline[5];
col7 = inputline[6];
col8 = inputline[7];
col9 = inputline[8];
int1 = atoi(&col1);
int2 = atoi(&col2);
int3 = atoi(&col3);
int4 = atoi(&col4);
int5 = atoi(&col5);
int6 = atoi(&col6);
int7 = atoi(&col7);
int8 = atoi(&col8);
int9 = atoi(&col9);
cout << "inputline: " << inputline << endl;
cout << "col1: " << col1 << " col2: " << col2 << " col3: " << col3 << endl; //debug line
cout << "int1: " << int1 << " int2: " << int2 << " int3: " << int3 << endl; //debug line
结果是:
为第 1 行的框输入值,为空框输入 0(输入 9 个数字,没有空格或分隔符):456123789输入线:456123789col1: 4 col2: 5 col3: 6int1: 4 int2: 54 int3: 654
为什么int包含5和int3 65(应该是int1:4 int2:5 int3:6)
最佳答案
atoi
用于将以 NULL 结尾的字符串 (char*) 而非字符转换为整数。您的 col1
、col2
不是以 NULL 结尾的字符串,并且 atoi
将读取内存直到它到达 NULL 值(这将结束字符串)。
如果您想将 ASCII 数字转换为数字,可以使用简单的数学运算:
int1 = col1 - '0';
col1: 4 col2: 5 col3: 6 int1: 4 int2: 54 int3: 654
所有这些值都存储在堆栈中。让我们假设堆栈当前为空(它不是,还有其他局部变量、返回地址等),并且顶部元素为 0:
STACK
----------
0 <- top
现在,您的col1
、col2
、col3
被放置在堆栈上,正如您声明的那样:
STACK
----------
0
col1
col2
col3
并且,一旦您对它们赋值,您将得到下图:
STACK
----------
0
'4'
'5'
'6'
当您调用 atoi(col1)
时,它将读取 '4'
,然后是 0
,这将终止字符串,并且它将仅解析 ASCII '4'
。当您调用 atoi(col2)
时,它将读取 '5'
、'4'
,然后是 0
, 所以输入字符串将是 "54"
所以它会准确地解析。所有其他 col
变量都会发生类似情况。
请注意,以相反顺序读取堆栈元素并没有什么神奇之处——实际上,您正在以正序读取内存——因为在我(可能还有你)的机器上,堆栈是向下增长的。在某些机器上,情况并非如此(查看 this link 了解更多详细信息),您会得到 456...
for col1
或者可能只是零(atoi
如果您传递非数字字符串作为参数,将返回零。
关于C++ atoi 获取在程序同一部分创建的其他字符的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286502/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
为什么在 C# 中添加两个 char 结果是 int 类型? 例如,当我这样做时: var pr = 'R' + 'G' + 'B' + 'Y' + 'P'; pr 变量变为 int 类型。我希望它是
下面的代码可以编译,但 char 类型的行为与 int 类型的行为不同。 特别是 cout ::ikIsX >() ::ikIsX >() ::ikIsX >() using names
我正在寻找一个正则表达式,它可以匹配长度为 1 个或多个字符但不匹配 500 的内容。这将在 Rails 路由文件中使用,特别是用于处理异常。 路线.rb match '/500', to: 'err
对于 C 编程作业,我正在尝试编写几个头文件来检查所谓的“X 编程语言”的语法。我最近才开始,正在编写第一个头文件。这是我编写的代码: #ifndef _DeclarationsChecker_h_
为什么扩展的 ascii 字符(â、é 等)被替换为 字符? 我附上了一张图片...但我正在使用 PHP 从 MySQL 中提取数据,其中一些位置有扩展字符...我使用的是 Arial 字体。 您可以
我有一个与 R 中的断线相关的简单问题。 我正在尝试粘贴,但在获取(字符/数字)之间的断线时遇到问题。请注意,这些值包含在向量中(V1=81,V2=55,V3=25)我已经尝试过这段代码: cat(p
如何将 ANSI 字符 (char) 转换为 Unicode 字符 (wchar_t),反之亦然? 是否有用于此目的的任何跨平台源代码? 最佳答案 是的,在 中你有mbstowcs()和 wcsto
函数 fromCharCode 不适用于国际 ANSI 字符。例如,对于 ID 为 192 到 223 的俄语 ANSI (cp-1251) 字符,它返回特殊字符。如何解决这个问题? 我认为,需要将A
如果不喜欢,我想隐藏 id,但不起作用 SELECT * FROM character, character_actor WHERE character.id NOT LIKE character_a
现在这个程序成功地反转了键盘输入的单词。但是我想在我反转它之前“保存”指针中的单词,所以我可以比较两者,反转的和“原始的”,并检查它们是否是回文。我还没有太多经验,可能会出现比我知道的更多的错误,但我
Memcpy 和 memcmp 函数可以接受指针变量吗? char *p; char* q; memcpy(p,q,10); //will this work? memcmp(p,q,10); //w
恐怕我对一个相当过饱和的主题的细节有疑问,我搜索了很多,但找不到一个明确的答案来解决这个特定的明显-imho-重要的问题: 使用UTF-8将byte[]转换为String时,每个字节(8bit)都变成
我有一个奇怪的问题。我需要从 stat 命令打印输出字符串。 我已经编写了获取一些信息的代码。 import glob import os for file in glob.glob('system1
我正在使用 Java 并具有其值如下所示的字符串, String data = "vale-cx"; data = data.replaceAll("\\-", "\\-\\"); 我正在替换其中的“
String urlParameters = "login=test&password=te&ff"; 我有一个String urlParams,& - 是密码的一部分,如何使其转义,从而不被识别为分
大家好,我只想从此字符串中提取第一个字母: String str = "使 徒 行 傳 16:31 ERV-ZH"; 我只想获取这些字符: 使 徒 行 傳 并且不包括 ERV-ZH 仅数
这个问题已经有答案了: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized point
所以, 我有一个字符**;它本质上是一个句子,带有指向该句子中每个单词的指针;即 'h''i''\0''w''o''r''l''d''\0''y''a''y''!''\0' 在这种情况下,我希望使用可
这个问题在这里已经有了答案: Using quotation marks inside quotation marks (12 个答案) 关闭 7 年前。 如何打印 " 字符? 我知道打印 % 符号
我是一名优秀的程序员,十分优秀!