15,902,777 members
See more:
The program asks user to enter five single characters separated by spaces, and depending on the characters, it will generate a random number.

Here are the rules to follow exactly:
- If input character is 'a', program will generate a random number from 1 to 20.
- If input character is 'e', program will generate a random number from 21 to 40.
- If input character is 'i', program will generate a random number from 41 to 60.
- If input character is 'o', program will generate a random number from 61 to 80.
- If input character is 'u', program will generate a random number from 81 to 100.
- If input character is not one of the above characters, display a 0 in the output.

Here are some sample runs:

Run 1

This program plays a simple random number game.
Enter 5 vowel characters (a,e,i,o,u) separated by spaces: a e i o u
The random numbers are 10 25 46 69 99
---------------------------------------------------------------
Run 2

This program plays a simple random number game.
Enter 5 vowel characters (a,e,i,o,u) separated by spaces: a e a u o
The random numbers are 10 25 4 99 66
---------------------------------------------------------------
Run 3

This program plays a simple random number game.
Enter 5 vowel characters (a,e,i,o,u) separated by spaces: a z i e o
The random numbers are 10 0 43 29 76
---------------------------------------------------------------
Run 4

This program plays a simple random number game.
Enter 5 vowel characters (a,e,i,o,u) separated by spaces: a z i f o
The random numbers are 15 0 47 0 74

What I have tried:

C++
```#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

using namespace std;

/* Homework 3
Yong Yu Xuan
This program generates a random number depending on the
character inputted.
*/

int main()
{
srand(time(0));

//constant for setw()
const int GAP = 6;

//variables
int num;

int none = 0;
char alphabet;
char letter1, letter2, letter3, letter4, letter5;
letter1 = 'a';
letter2 = 'e';
letter3 = 'i';
letter4 = 'o';
letter5 = 'u';

//tell user to enter five characters at random
cout << "This program plays a simple, random number game." << endl;
cout << "Enter five vowel characters (a,e,i,o,u) in any order separated ";
cout << "by spaces: ";
cin >> alphabet >> alphabet >> alphabet >> alphabet >> alphabet;

alphabet = num;

cout << "The five random numbers are: " << setw(GAP) << num << setw(GAP);
cout << setw(GAP) << num << setw(GAP) << num;
cout << setw(GAP) << num << setw(GAP) << num;

if (alphabet == letter1)
cout << setw(GAP) << rand() % 19 + 1;

else if (alphabet == letter2)
cout << setw(GAP) << rand() % 19 + 21;

else if (alphabet == letter3)
cout << setw(GAP) << rand() % 19 + 41;

else if (alphabet == letter4)
cout << setw(GAP) << rand() % 19 + 61;

else if (alphabet == letter5)
cout << setw(GAP) << rand() % 19 + 81;

else
cout << none << endl;

return 0;
} ```
Posted
Updated 2-Feb-17 18:45pm
v2
Patrice T 3-Feb-17 0:39am
And you have a question ?
Stefan_Lang 6-Feb-17 2:19am
Solution 1 points out problems with your code. If it solves the problem, please accept the solution so we can see you don't need any more help. Otherwise add a comment so we know you need more advice.

Besides, now that you've slept over it (several times actually), maybe you should simply take another look on the program: you seem to have had the right ideas, but apparently got confused along the way. With a fresh mind, you may now be able to spot the issues by yourself.

## Solution 1

This line is highly suspect, it read 5 values in 1 variable
C++
`cin >> alphabet >> alphabet >> alphabet >> alphabet >> alphabet;`

what is the magic that will apply the rest of program to each values?
but it doesn't really matter because this one
C++
`alphabet = num;`

is killing the value.
I fear you have to rethink your program, whole organization is wrong.
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
-----
Mastering some analyze methods can help, Dijkstra Top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]

v2