-2

I wish to make a char with digits between 0-9. The user decides how many digits to use. For example, if the user inputs 4, the char should be 01234. Please note I cannot use the string data type. I have to use char. I know how to generate a string for the same logic but not a char. So if there is a way to convert string to char, that will work well. I tried

string randomString; //this contains the set of numbers 0-9 on the basis of the users  input 
char charString = randomString;

This however does not work.

Sheru
  • 7
  • 3
  • Not knowing much about C++ I'm assuming the type `char` can just hold a 1-byte integer (only a character, letter or number). Here you can read more: http://www.learncpp.com/cpp-tutorial/27-chars/ – simeg Feb 22 '16 at 22:55
  • Terrible question; lazy and did not want to look up. The function is well documented. – Java Man Tea Man Feb 22 '16 at 23:01

1 Answers1

2

So if there is a way to convert string to char

Yes, it's called a character array and you can easily convert a string type to a character array like so:

const char* charString = randomString.c_str();

You can find more information about c_str() method here and you should review this material regarding character arrays.

If you require a non-const (can be modified) character array, refer to the above links which will explain it and actually give examples about how to accomplish that.

Jonathon Ogden
  • 1,562
  • 13
  • 19