将字符数组转换为 int
Converting char array to int
#include <QtCore/QCoreApplication>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cctype>
using namespace std;
void test()
{
int arr[10];
int size = 0;
int i = 0;
char str[] = "12 45 1666";
for(;;)
{
while(str[i]!=' ' && str[i]==' ')i++;
if(str[i]==' ')return;
arr[size] = 0;
while(str[i]!=' ' && str[i]!=' ')
{
if(!isdigit(str[i]))
{
cout <<str[i]<<" - Not a number!"<<endl;
return;
}
arr[size]=arr[size]*10+(str[i]-48);
i++;
}
size++;
}
for(int k = 0;i<size;k++)
{
cout <<arr[k]<<endl;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
test();
return a.exec();
}
在这里,我正在尝试编写一个将数字和空格字符串转换为数字数组的程序,但是它没有输出一个问题。什么可能导致此问题。我是 c++ 的乞丐,所以欢迎批评者。
return
离开当前函数。摆脱无限循环的唯一方法是离开整个函数,这意味着您总是跳过输出。请改用break
或为for(;;)
循环提供适当的条件。
尽管 c++ 已经提供了执行此操作std::stringstream
。
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::stringstream stream("12 45 1666");
int result;
// Reads from the stream until it's exhausted or fails
while (stream >> result)
{
std::cout << result << 'n';
}
return 0;
}
for(int k = 0;k<size;k++)
{
cout <<arr[k]<<endl;
}
问题在这里只是在"k"上更改了"i"。
相关文章:
- 是否可以从int转换为enum类类型
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- 将字符串中的 int 转换为字母;
- 主.cpp:18:20:错误:从"int*"转换为"int"会失去精度 [-fa
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- 如何将矢量<int>转换为字符数组?
- C++ 无法从 const int* 转换为 const_iterator
- 我不断收到错误 C2440'=':无法从"int *(__cdecl *)(int *,int *,int,int)"转换为"int *
- 如何将 int[4] 转换为 std::array<int,4>?
- 如何将data[i].int转换为vaible
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 将私有矩阵形式 int 转换为双 C++
- 无法将参数 1 从 'int' 转换为 'int &'
- 将 int 转换为字符串后始终得到 0
- isspace 函数的性能警告,从 int 转换为布尔值
- 将大 int 转换为浮点数,而不舍入 c++
- 错误:无法将'uint8* {aka unsigned int*}'转换为"常量emxArray_uint8_T*"?
- 将未签名的INT转换为BCD
- 在 C++ 中将 int 转换为双精度