How to select random letters in c++

arrays, c++, ctime, random

Solution

Use

cch = 'a' + rand()%26;

This will add a random number to 'a' (which in int equals 97 but using that number makes your code less readable), remember `'a' == 97` because C and C++ don't actually care about your data type.

So if it is stored as char is will be seen as char

Problem

Hi I'm working on an hangman project for my final in my c++ class. I've got most of it working but I wanted to make it so the player could have the option to play against a computer opponent in single player mode. I tried doing something like 97+rand()%123 so then convert the number to characters but I keep getting weird characters like an upside down f I checked to see if I was missing something but I have the right directives and I included a srand. a simplified version of what I did looks like this ``` #include <iostream> #include <fstream> #include <string> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; int main(){ char cch; char ch; unsigned seed= time(0) srand(seed) cch=97rand()%123; ch=cch; cout<<"computer chose "<< ch<<endl; } ``` please note that I didn't include everything my project

Original source