user3672304
user3672304

Reputation: 1

input char array and output it as integer array

I would like to have input as char array in single line like: "12 5 232 65 76 435 2345" and output it as int array

I started writing a code but it works only for single digitals (like "3 6 4 5 6" etc...)

#include <iostream>

int main(){

char A[100];
int intA[100];

std::cout << "Input Array numbers" << std::endl;
std::cin.getline(A,100);
std::cout << std::endl;

for(int i=0; A[i]!= 0; i++){

    if ( A[i] != ' ' ){
        intA[i] = (int) A[i] - '0';
        std::cout << intA[i] << std::endl;
    }
}

return 0;
}

additional question: could someone explain me what actually "- '0'" does (I know without it a char casted to int would be and ASCII representation and you need to add it to get actual number)

Upvotes: 0

Views: 338

Answers (5)

Rainbacon
Rainbacon

Reputation: 943

To convert multiple characters to an integer copy those characters into a separate char[] and call atoi on it.

Upvotes: 0

user1618143
user1618143

Reputation: 1748

If you want to do it all manually (for whatever reason), your current approach is pretty close to what you need. Try pushing an integer to the output array only when you see a space, and otherwise multiply the previous number by ten and add the new digit. Like this:

int curSum = 0;
int curIndex = 0;
for(int i=0; A[i]!= 0; i++){

    if ( A[i] == ' ' ){
        intA[curIndex] = curSum;
        curIndex++;
        std::cout << curSum << std::endl;
        curSum = 0;
    } else {
        curSum = curSum*10;
        curSum += (int) A[i] - '0';
    }
}

However, unless this is a homework problem or something, using an existing library is probably a better solution.

Upvotes: 0

Chewie
Chewie

Reputation: 71

It only works for single digits because you are working on the chars as letters (42 is not 42 it is '4' and then '2'), so your for loop runs on first '4', and then it runs on '2' - also incrementing i for the index used in intA. So you are not building 42 out of '4' and '2' but just placing 4 in intA[0] then 2 in intA[1].

When you read your input you need to first gather the multiple digit numbers into a number format and not separate letters.

The -'0' is needed because you are working on the letter 4 (ASCII value 52) and not the number 4, so you subtract 48 (ASCII value of '0') to get the numerical value.

Upvotes: 0

Columbo
Columbo

Reputation: 60999

could someone explain me what actually "- '0'" does

This is used if the character miuend is known to be a digit: Subtracting '0' gives the actual number that the Code-point is representing. For instance: '7' - '0' == 7.

I'm not sure whether this helps, but this is an example implementation:

std::string str; // use std::string
std::getline( std::cin, str );
std::istringstream stream(str);

std::vector<int> vec{ std::istream_iterator<int>{stream}, {} };
for( auto i : vec )
    std::cout << i << '\n';

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

This task is better to do using std::stringstream and standard algorithm std::copy

For example (without testing):

#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

int main()
{
   char A[100];
   int intA[100];

   std::cout << "Input Array numbers" << std::endl;
   std::cin.getline(A,100);
   std::cout << std::endl;

   std::istringstream is( A );

   int *p = std::copy( std::istream_iterator<int>( is ),
                       std::istream_iterator<int>(),
                       std::begin( intA ) );

   std::copy( std::begin( intA ), p, std::ostream_iterator<int>( std::cout, " " ) );
   std::cout << std::endl;

   return 0;
}

Upvotes: 1

Related Questions