I have created a simple random number program that takes 5 inputs and outputs different random numbers.
A user can enter 5 vowels irrespective of case and the function calculates a random number based on the input.
Possible Incomes: a A a A e
Possible Outcomes: 1 2 3 19 25
Problem: I do not get different numbers when I enter the same vowel more than once, but this is not the same when I put a breakpoint and run my code in debugger mode
Following is my code
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int createRandomFromChar(char inputChar);
int main()
{
char answer;
char inputOne, inputTwo, inputThree, inputFour, inputFive;
cout << endl <<
"This program plays a simple random number guessing game." << endl;
do
{
cout << endl << "Enter 5 vowel characters (a,e,i,o,u or A,E,I,O,U) separated by spaces: ";
cin >> inputOne >> inputTwo >> inputThree >> inputFour >> inputFive;
cin.ignore();
int randomNumberOne = createRandomFromChar(inputOne);
int randomNumberTwo = createRandomFromChar(inputTwo);
int randomNumberThree = createRandomFromChar(inputThree);
int randomNumberFour = createRandomFromChar(inputFour);
int randomNumberFive = createRandomFromChar(inputFive);
cout << "The random numbers are " << left <<
setw(3) << randomNumberOne << left <<
setw(3) << randomNumberTwo << left <<
setw(3) << randomNumberThree << left << setw(3) << randomNumberFour
<< left << setw(3) << randomNumberFive;
cout << endl << "Do you want to continue playing? Enter 'Y' or 'y' to continue playing: "
<< endl;
answer = cin.get();
cin.ignore();
}
while ((answer == 'y') || (answer == 'Y'));
}
int createRandomFromChar(char inputChar)
{
srand(time(0));
int n1 = 1 + (rand() % 20);
int n2 = 21 + (rand() % 20);
int n3 = 41 + (rand() % 20);
int n4 = 61 + (rand() % 20);
int n5 = 81 + (rand() % 20);
if ((inputChar == 'a') || (inputChar == 'A'))
{
return n1;
}
else if ((inputChar == 'e') || (inputChar == 'E'))
{
return n2;
}
else if ((inputChar == 'i') || (inputChar == 'I'))
{
return n3;
}
else if ((inputChar == 'o') || (inputChar == 'O'))
{
return n4;
}
else if ((inputChar == 'u') || (inputChar == 'U'))
{
return n5;
}
else
{
return 0;
}
}