"int" to "vector <char>"

I would normally just do "Vector.push_back(String[i])" in a for loop (for strings) but I cant do Int[i] with integers, so how could I do this?

1
2
3
4
void HiddenNumberGenerator() {
        vector <char> HiddenNumber;
	int HiddenNumber = rand() % 9000+ 1000;
}
Last edited on
Why are you trying to use a vector<char> instead of a vector<int>? Remember that a signed char can only hold very limited values (-128 to +127). Since your random numbers can be much larger you should be using an int instead.

Oh well how do I convert to int vector?
You generate an integer, whose value is in range [1000-10998], and then you want to convert its digits into array of char?

There is always to_string: http://www.cplusplus.com/reference/string/to_string/
Lines 2 and 3 are creating a duplicate variable name.

Oh well how do I convert to int vector?

Simply put int inside the vector<>. i.e. vector<int>.

1
2
3
4
5
6
void HiddenNumberGenerator() 
{   int temp;
     vector <int> HiddenNumber;
    temp = rand() % 9000+ 1000;
    HiddenNumber.push_back (temp);  
}  //HiddenNumber goes out of scope here.   


Topic archived. No new replies allowed.