Ash. S - 4 years ago 115
C++ Question

# C++ Random Number Output "Game"

This is for my intro C++ homework and I'm not sure if I should be asking for homework help on here but I'm going crazy trying to figure this out because everywhere I've looked seems to have a different answer for how to do random numbers with in a given range.

The program will ask the user to enter five single characters separated by spaces, and depending on the characters, it will generate a random number.

Here are the rules:

• If input character is 'A', program will generate a random number from 1 to 20.

• If input character is 'B', program will generate a random number from 21 to 40.

• If input character is 'C', program will generate a random number from 41 to 60.

• If input character is 'D', program will generate a random number from 61 to 80.

• If input character is 'E', program will generate a random number from 81 to 100.

• If input character is not one of the above characters, display a "*" in the output.

I'm not really sure how to best construct this to output the numbers in the given ranges. Keep in mind this is an intro class so we've only gotten as far as "if" statements and using rand() and srand().

I'm not asking for my homework to be done for me lol. I'm just trying to get an idea of what to do since I can find 2 slides from the powerpoints my professor has given us pertaining to this assignment and I have no clue what to do.

EDIT:
This question is not a duplicate because the other question uses more advanced code than I have been exposed to. This is for intro C++ class and we've only gotten to if statements.
Also thanks for the downvotes! Really makes me feel welcome here.

Check up `srand()` and `rand()` for random number generator. For starters, rand() will always generate the same number on runtime no matter how many times you call it. However, with `srand()`, you'll most probably generate a different number.

I have done once a project with `rand()` and `srand()` will update should I find that. Then, for the control structure, you may want to consider a switch statement by comparing the ASCII value of the input. (65 = 'A', 66 = 'B' and so on. This is if you are not considering lowercase as input.)

References for `srand()`
`rand()` returns same number
C++ reference : `srand()`

References for `rand()`
C++ reference : `rand()`

# Edit:

Since you've stated the control structure you've been exposed is only upto if, then it should suffice should you do like this

``````if(input == 'A')
//do stuff here
else if(input == 'B')
//do stuff here
...continue the pattern
else //should it fail from A to E
std::cout << "*";
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download