I'm trying to print the char. How do I convert the string to char? I get this error:
error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' in assignment
#include <iostream>
#include <random>
#include <string>
#include <ctime>
using namespace std;
int const MAX = 4;
string randomString(int n){// generating random letter
char alphabet[MAX] = { 'r', 'a', 'n', 'd'};
string res = "";
for (int i = 0; i < n; i++)
res = res + alphabet[rand() % MAX];
return res;
}
int main (){
srand(time(NULL));
char random;
random = randomString(1);// problem lies here
char a1 = random;
char a2 = random;
char a3 = random;
cout<<a1<< " " << a2 << " " << a3<< endl;
}
randomStringreturns astd::string,randomis achar.std::__cxx11::string {aka std::__cxx11::basic_string}just means "std::string". Your error is "cannot convert 'std::string' to 'char' in assignment". Which is true. Astd::stringhas no implicit conversion tochar.