user1125033
user1125033

Reputation: 35

Extracting int for char array in C++

I am trying to extract single character from char array and converting it in to integer. I need to extract number from code for example if user enters A23B,I need to extract 23 and store it in a single variable here is my code

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{
char code[5] ={'\0'};
cout << "Enter Your Four Digit Code\nExample A23B\n";
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
}

but it's not working and produces the following errors

C:\helo\clan\Test\main.cpp||In function 'int main()':|
C:\helo\clan\Test\main.cpp|12|error: invalid conversion from 'char' to 'const char*'|
C:\helo\clan\Test\main.cpp|12|error:   initializing argument 1 of 'int atoi(const char*)'|
C:\helo\clan\Test\main.cpp|13|error: invalid conversion from 'char' to 'const char*'|
C:\helo\clan\Test\main.cpp|13|error:   initializing argument 1 of 'int atoi(const char*)'|
||=== Build finished: 4 errors, 0 warnings ===|

Upvotes: 1

Views: 9429

Answers (7)

domshyra
domshyra

Reputation: 952

If anyone wants to do this code with out including to many library's(Some schools require just basic libraries like mine) This function is done with cmath and cctype It will take in a course number like cs163 and turn it into an int of 163 ascii value for 1 = 49 that is why the - 48 is there. Something like this will do it as well, but with out using a lot of function like string class. This isn't super optimised I know, but it will work.

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;

}

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59997

Why not

int a = int(code[1]-'0') * 10 + int(code[2] - '0');

i.e. Convert the two ASCII characters to the appropriate integers and then do the maths.

EDIT

You should check to ensure that the string is 4 characters long and characters 2 & are digits.

Upvotes: 1

xucheng
xucheng

Reputation: 275

For common situation , why not using std::string and std::stringstream like this:

#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;
}

You can use these two functions converting string between nums(int,double...).

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 477040

Your code is horribly unsafe (what if the user enters more than four digits? What if a line break takes up more than one byte?)

Try something like this:

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;
}

We need the isNumber helper:

inline bool isNumber(char c) { return '0' <= c && c <= '9'; }

Upvotes: 0

mattjgalloway
mattjgalloway

Reputation: 34912

I think you want something like this:

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{
char code[5] ={'\0'};
cout << "Enter Your Four Digit Code\nExample A23B\n";
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
}

And if you want 23 in one variable then maybe this:

char aStr[3] = {code[1], code[2], 0};
int a = atoi(aStr);

Upvotes: 0

hmjd
hmjd

Reputation: 121971

atoi takes a const char*, not char.

If you need to get '2' and '3' from "A23B":

int b = atoi(code + 2);
code[2] = 0;
int a = atoi(code + 1);

If you need to get '23' from "A23B" then:

int a = atoi(code + 1);

Upvotes: 3

MByD
MByD

Reputation: 137322

You pass a char to a function that expects char *, and you can't do that. Why not doing:

int a = code[1] & 0xff;
int b = code[2] & 0xff;

Upvotes: 0

Related Questions