正在C++中提取char数组的int
Extracting int for char array in C++
我正在尝试从char数组中提取单个字符,并将其转换为整数。我需要从代码中提取数字,例如,如果用户输入A23B,我需要提取23并将其存储在一个变量中,这是我的代码
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
char code[5] ={' '};
cout << "Enter Your Four Digit CodenExample A23Bn";
cin.getline(code,5);
cout << "You typed:n" << code;
int a = atoi(code[1]);
int b = atoi(code[2]);
cout << endl <<a <<"t"<<b;
//Other processing related to number a and b goes here
}
但它不起作用,并产生以下错误
C:heloclanTestmain.cpp||In function 'int main()':|
C:heloclanTestmain.cpp|12|error: invalid conversion from 'char' to 'const char*'|
C:heloclanTestmain.cpp|12|error: initializing argument 1 of 'int atoi(const char*)'|
C:heloclanTestmain.cpp|13|error: invalid conversion from 'char' to 'const char*'|
C:heloclanTestmain.cpp|13|error: initializing argument 1 of 'int atoi(const char*)'|
||=== Build finished: 4 errors, 0 warnings ===|
atoi采用const char*
,而不是char
。
如果您需要从"A23B"获得"2"answers"3":
int b = atoi(code + 2);
code[2] = 0;
int a = atoi(code + 1);
如果您需要从"A23B"获得"23",则:
int a = atoi(code + 1);
对于常见情况,为什么不像这样使用std::string
和std::stringstream
:
#include <string>
#include <sstream>
template <class T>
std::string num2string (const T &in)
{
static std::stringstream out;
out.str ("");
out.clear();
out << in;
return out.str();
}
template <class T>
T string2num (const std::string &in)
{
T out;
static std::stringstream tmp;
tmp.str ("");
tmp.clear();
tmp << in;
tmp >> out;
return out;
}
您可以使用这两个函数在数字(int
、double
…)之间转换string
。
为什么不
int a = int(code[1]-'0') * 10 + int(code[2] - '0');
即,将两个ASCII字符转换为适当的整数,然后进行数学运算。
编辑
您应该检查以确保字符串长度为4个字符,并且字符为2&是数字。
您将一个char传递给一个需要char*的函数,但您不能这样做。为什么不做:
int a = code[1] & 0xff;
int b = code[2] & 0xff;
我想你想要这样的东西:
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
char code[5] ={' '};
cout << "Enter Your Four Digit CodenExample A23Bn";
cin.getline(code,5);
cout << "You typed:n" << code;
char aStr[2] = {code[1], 0};
char bStr[2] = {code[2], 0};
int a = atoi(aStr);
int b = atoi(bStr);
cout << endl <<a <<"t"<<b;
//Other processing related to number a and b goes here
}
如果你想在一个变量中使用23
,那么可能是这样的:
char aStr[3] = {code[1], code[2], 0};
int a = atoi(aStr);
您的代码非常不安全(如果用户输入的超过四位数字怎么办?如果换行占用了一个以上的字节怎么办?)
试试这样的东西:
int a, b;
std::string line;
std::cout << "Enter Your Four Digit Code (Example: A23B): ";
while (std::getline(std::cin, line))
{
if (line.length() != 4 || !isNumber(line[1]) || !isNumber(line[2]))
{
std::cout << "You typed it wrong. Try again: ";
continue;
}
a = line[1] - '0';
b = line[2] - '0';
break;
}
我们需要isNumber
助手:
inline bool isNumber(char c) { return '0' <= c && c <= '9'; }
如果有人想在不包含许多库的情况下执行此代码(有些学校只需要像我这样的基本库)此功能通过cmath和cctype完成它将接受一个像cs163这样的课程编号,并将其转换为163的整数1=49的ascii值,这就是为什么会出现-48。类似这样的东西也可以做到这一点,但out使用了很多类似字符串类的函数。我知道这不是超级优化,但它会起作用的。
int courseIndexFunction(char * name){
int courseIndex = 0;
int courseNameLength = 5;
if (name[2] && name[3] && name[4]){
//This function will take cs163 and turn
//it into int 163
for (int i = 2; i < courseNameLength; ++i){
double x = name[i] - 48;
x = x * pow(10.0 , 4-i);
courseIndex += x;
}
}
return courseIndex;
}
相关文章:
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 按字符值访问int数组
- 将 int 数组转换为 std::vector<int*>
- 比较if语句中的数组值和int值
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 在c++中获取两个大int,并将它们存储在数组中
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 有没有办法用 2D 数组 (int) 重载构造函数?
- 当大小未知时,如何迭代指向数组 (int*) 的指针?
- 接受来自函数的二维数组(int 类型)并将其返回到主函数
- 数组 + int 作为一个参数做什么
- 如何制作'bit'数组而不是数组'int'?
- 动态2d数组:int(*ptr)[4]=new int[7][4];
- 为[z][y][x]int定义数组int[x][y][z]有什么好处?
- 用Visual c++编写二维数组int[n][m]到HDF5文件
- 数组int未正确返回
- 将元素推送到动态分配的指针数组int C++
- 如何在另一个数组中插入多维数组(int而非char)
- 将数组(int*)设置为NULL是否释放其占用的内存
- C++如何将数组 int[10] push_back std::vector<int[10]>?