Part of the assignment I'm working on is to display random letters between my good letters to encode text. Right now I can make it display random numbers. How can I change this to out put random letters?

#include <iostream>
#include <fstream>
#include <ctype.h>
#include <ctime>
#include <cstdlib>
#include "stdafx.h"

using namespace std;

int main () 
{
int i;
int array[10];
char words;

do
{ 
cin>>words;
		 {
			for(i =0; i<5; i++)
			  {
				  array[i] = rand ()%9;
				  
				  {
					  cout<<array[i];
				  }
			}
			cout.put(words);
		 }

		 
     } while (c != EOF) ;
  return 0;  
}
//Loads the array with ascii values ranging from 65 to 122 ('A' thru 'z')
static_cast<char>(array[i]) = rand()%58 + 'A';
//Loads the array with ascii values ranging from 65 to 122 ('A' thru 'z')
static_cast<char>(array[i]) = rand()%58 + 'A';

would that go in place of line 24?

Yes, line #24 would be the logical place to give this a try. (sorry for not being specific enough)

Yes.. depending on what you want to do of course, but yes, line #24 would be the logical place to give this a try. (sorry for not being specific enough)

I am getting an error with the = in the statement.

Then try this..

//line #24:  Load ascii values
array[i] = rand()%58 + 'A';

//Then whenever you want to use the array as a 'char', do this:  Line #27
cout << static_cast<char>(array[i]);

array = (rand()%58) + 'A';

cout<<(char)array;

added this to line #24 and #27. Works great thanks for the help.

Just make a function that returns a random lowercase or a random uppercase depending on the parameter.

Do you know how to generate random values ?

srand( time(0) ); //#include<ctime> needed for time function
int randomNum = rand();

With that in mind generating a random character is easy :

char randomUpperCase= 'A' + rand() % 26; 
char randomLowerCase= 'a' + rand() % 26;

>With that in mind generating a random character is easy:
>char randomUpperCase= 'A' + rand() % 26;
>char randomLowerCase= 'a' + rand() % 26;

Easy if you ignore the portability issue. The only characters that are guaranteed to be consecutive in C++ are the decimal digits '0'-'9'. Your code in a different character set (EBCDIC, for example), would not work as expected. Compare to a portable solution that specifies the set to extract from:

char ruc = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
char rlc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() % 26];

>With that in mind generating a random character is easy:
>char randomUpperCase= 'A' + rand() % 26;
>char randomLowerCase= 'a' + rand() % 26;

Easy if you ignore the portability issue. The only characters that are guaranteed to be consecutive in C++ are the decimal digits '0'-'9'. Your code in a different character set (EBCDIC, for example), would not work as expected. Compare to a portable solution that specifies the set to extract from:

char ruc = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
char rlc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() % 26];

Ok, didn't know that. Learn something new everyday.

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.