Question

What I need to do, is input an int c string from the keyboard, but each digit should be a single digit int.

For example in the running program if I would input "1234", it shouldn't read '1234', but a '1' followed by a '2', '3' and then '4'. I would submit my thought process on it, but I'm at a complete loss here.

This is in C++ by the way, and I have the restriction of not being able to use anything from the STL library.

Was it helpful?

Solution

say we have a char array of that you've gotten from the user, and that array of chars looks something like this char line[] = {'1', '2', '3', '4', '\0'}; which is basically equivalent of entering 1234 in the console.

it would then be possible to get the integer value of an indexed position in the previously mentioned array by doing something like this: int one = line[0] - '0';

this:

char line[] = {'1', '2', '3', '4', '\0'};
int one = line[0] - '0';
std::cout << one << std::endl;

will output this:

1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top