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)
-'0'subtracts the ascii value '0' from your character, so if'0'is48then'2'is represented by the number50, than'2'-'0'is equivelant to50 - 48, which is2