I am trying to get the following code to take in either a number or an integer, determine whether what the user inputted is an integer or a letter, and then print a random digit if it is an integer, or a random letter if it is a letter.

``````#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>

using namespace std;

/*
IGNORE THIS FUNCTION
int random()
{
int random;
srand(time(0) * 2345738 * 345478 * 34534 * 347562834 * 345643 * 345349);
cout << rand() << "\n" << endl;
cin >> random;
cout << "\n" << random << "\n" << endl;
return random;
}
*/

int numout(int x)
{
short int random;
srand(x);
random = time(0) * 123456789;
if(random >= 0 || random < 3200)
cout << "5";
if(random >= 3200 || random < 6400)
cout << "6";
if(random >= 6400 || random < 9600)
cout << "1";
if(random >= 9600 || random < 12800)
cout << "2";
if(random >= 12800 || random < 16000)
cout << "3";
if(random >= 16000 || random < 19200)
cout << "4";
if(random >= 19200 || random < 22400)
cout << "7";
if(random >= 22400 || random < 25600)
cout << "8";
if(random >= 25600 || random < 6400)
cout << "9";
if(random >= 3200 || random < 6400)
cout << "0";
}

int alphaout(int x)
{
short int random;
srand(x);
random = time(0) * 123456789;
if(random > 0 || random < 1000)
cout << "A";
else if(random >= 1000 || random < 2000)
cout << "B";
else if(random >= 2000 || random < 3500)
cout << "C";
else if(random >= 3500 || random < 4500)
cout << "D";
else if(random >= 4500 || random < 5500)
cout << "E";
else if(random >= 5500 || random < 7000)
cout << "F";
else if(random >= 7000 || random < 8000)
cout << "G";
else if(random >= 8000 || random < 9000)
cout << "H";
else if(random >= 10000 || random < 11000)
cout << "I";
else if(random >= 11000 || random < 12500)
cout << "J";
else if(random >= 12500 || random < 13500)
cout << "K";
else if(random >= 13500 || random < 14500)
cout << "L";
else if(random >= 14500 || random < 15500)
cout << "M";
else if(random >= 15500 || random < 16000)
cout << "N";
else if(random >= 16000 || random < 17000)
cout << "O";
else if(random >= 17000 || random < 18000)
cout << "P";
else if(random >= 18000 || random < 19000)
cout << "Q";
else if(random >= 19000 || random < 20000)
cout << "R";
else if(random >= 20000 || random < 21500)
cout << "S";
else if(random >= 21500 || random < 23000)
cout << "T";
else if(random >= 23000 || random < 24000)
cout << "U";
else if(random >= 24000 || random < 25000)
cout << "V";
else if(random >= 25000 || random < 26500)
cout << "W";
else if(random >= 26500 || random < 28000)
cout << "X";
else if(random >= 28000 || random < 29500)
cout << "Y";
else
cout << "Z";
}

int ifalpha(int x)
{
if(isalpha(x) == true)
return 0;
if(isalpha(x) == false)
return 1;
}

int main()
{
int a;
cin >> a;
switch(ifalpha(a))
{
case 0:
alphaout(a); break;
case 1:
numout(a); break;
}
system("PAUSE");
}``````

Any ideas?

A couple of hints:
Call `srand((unsigned)time(0));` once and only once at the top of main (or anytime before your first `rand()` call but top of main is a good place).

To get numbers in a range 0 to maxvalue inclusive use `rand() % (maxvalue+1)` (not the ideal way but the simplest -- read http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx for some much better options).

You don't get anything out of using your ifalpha function. isalpha() already returns a bool so just use it in an if statement.

See if you can figure out a way to output your random letter just using that `rand() % 26` gets you a number between 0 and 25.

Try those suggestions out and post back...

Be a part of the DaniWeb community

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