hi can anyone help me i am trying to get a random character generator which generates 10 characters in ASCII range and then cast them into characters

heres my code but its not working. it says error C2440: '<function-style-cast>' : cannot convert from 'std::string' to 'char'

heres my code

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>


using namespace std;

int main()

{
	string RandNum; // name of the string
	char UserLetter; // what the user will enter  and the place of storage in char


	for (int i(0); i !=10; i++) // a for loop which will run until the game has 10 letters
	{
	srand ((unsigned) time(0) ); 
	
	RandNum = 65 + rand()% 26; // generate the numbers in the ASCII range

	}


	cout << (char)RandNum << endl; // casting them into characters
	return 0;
}

any help will be greatly appreciated

You can't cast a string to a char, which is what you are trying to do in line 25. That's where the error is coming from.

Also, your line:

RandNum = 65 + rand()% 26; // generate the numbers in the ASCII range

is assigning a value from 65 to 90 to a string. You should replace RandNum in this line with a variable that is a character variable.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>


using namespace std;

int main()

{
        srand ((unsigned) time(0) ); //<----Only seed once	
//A string is not necessary, you aren't using any the facilities of the class
        char* RandNum;
//What is this supposed to do? There's no input from the user?	
        char UserLetter; // what the user will enter  and the place of storage in char


	for (int i=0; i <10; i++)	
	     RandNum[i] = 65 + rand()% 26;

	cout << RandNum << endl;
	return 0;
}

There is no need to cast a string to a char* anyway since cout already knows what a string is and can output it just fine

can anyone help me, what if i want to random only vowel (a,e,i,o,u) ?thz

for a set of random letters like only vowels you can create an array that holds the vowels and then use rand % (size of array) to get the index for the element in the vowel array.

oh i got it....thank.

oh i got it....thank.

In that case mark this thread as solve, please.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>


using namespace std;

int main()

{
        srand ((unsigned) time(0) ); //<----Only seed once	
//A string is not necessary, you aren't using any the facilities of the class
        char* RandNum;
//What is this supposed to do? There's no input from the user?	
        char UserLetter; // what the user will enter  and the place of storage in char


	for (int i=0; i <10; i++)	
	     RandNum[i] = 65 + rand()% 26;

	cout << RandNum << endl;
	return 0;
}

There is no need to cast a string to a char* anyway since cout already knows what a string is and can output it just fine

really, I mean come on. RandNum is never allocated, but still written into. Plus its
not null-terminated. And on top of that you tell him not to use std::string, SMH.

Even though, he might not need to use string's capability now, he might later on. And
at the very least, he does not have to worry about dynamic memory management.

Hey guys not sure if you realize but this thread was resurrected by yangclan. I didn't notice until he replied back that he had bumped a dead thread.

Can someone explain a little bit more about the random letter generator.. please I need help on that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.