正在C++中提取char数组的int

Extracting int for char array in C++

本文关键字:数组 int char 提取 C++ 正在      更新时间:2023-10-16

我正在尝试从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::stringstd::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;
}

您可以使用这两个函数在数字(intdouble…)之间转换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;

}