-4

Since I am a newbie in this c++ programming. I kind a confuse with this question. If there is "const char str[]", How can I return this with integer value ??

for(int i=0; i<size; i++)
    {
        n = rand () % 98 + 1;

        str [i] = n + '0';
    }

Do I have to do str[i] = n -'0' ? How can I return this character digit with integer value ??

hoi
  • 11
  • 1
  • 4
  • 3
    Possible duplicate of [How to parse a string to an int in C++?](https://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – Johan Nov 09 '17 at 08:51
  • http://www.cplusplus.com/reference/cstdlib/strtol/ – mdewit Nov 09 '17 at 08:59
  • you just do the oppisite direction, instead of doing n + '0' in your for loop you will do n - '0' – Daniel Kovachev Nov 09 '17 at 09:00
  • Offtopic : Your rand for this function might be wrong, Ontopic: do another for loop and - '0' in the other function. – elanor Nov 09 '17 at 14:43

1 Answers1

0

First of all, if str is declared as a const char[] it means that the content of str is constant. Therefore you cannot modify it. You could try to const_cast str, but it is usually a bad idea.

Secondly you can simply convert a char to an int by assigning your char variable to an intvariable.

char c = 'a';
int c_value = c;

You can find more inforation about const-correcteness here, and about type-casting here.