-2

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.

Ash. S
  • 3
  • 2
  • 1
    The interface to the Standard Library random number generators is a little fiddly, but this example is pretty good: http://en.cppreference.com/w/cpp/numeric/random#Example – BoBTFish Oct 22 '16 at 19:44
  • 2
    what exactly isnt clear to you? You know how to get a random number? Did you try anything? – 463035818_is_not_an_ai Oct 22 '16 at 19:45
  • Possible duplicate of [How to generate a random number from within a range](http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range) – Ricky Mutschlechner Oct 22 '16 at 20:11

3 Answers3

-1

The mod operator is your friend. To generate a random number from low to high: rand()%(high-low+1)+low

Donghui Zhang
  • 1,133
  • 6
  • 8
  • [`rand()` considered harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful). "When you need a random number, don't call `rand()` and especially don't say `rand() % 100`!" – BoBTFish Oct 22 '16 at 19:48
  • If the mod operator is your friend, [``](http://en.cppreference.com/w/cpp/numeric/random) is the smoking-hot super-model that promised to love you forever. – WhozCraig Oct 22 '16 at 19:48
  • @BoBTFish it is about second class of CS1... i wouldn't take they worry for technicalities. – Tomer W Oct 22 '16 at 19:49
  • 1
    @TomerW Why learn the wrong way now, only to have to learn it the right way later? Simplification is a valid teaching technique. Teaching entirely the wrong thing is not. – BoBTFish Oct 22 '16 at 19:51
  • 1
    @BoBTFish tend to agree, BTW is there reasonable alternative before C++11 URNG ? – Tomer W Oct 22 '16 at 20:26
-1

To get a random integer number between a and b Num=rand( ) %(b-a+1)+a

amanuel2
  • 4,508
  • 4
  • 36
  • 67
felit
  • 13
  • 3
-1

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 << "*";
Community
  • 1
  • 1
Nicky HFE
  • 143
  • 9
  • "rand() will always generate the same number on runtime no matter how many times you call it" - that is *not* true. Unless otherwise seeded with `srand(some entropic value)`, [the default seed of `1` will be used](http://en.cppreference.com/w/cpp/numeric/random/rand), and `rand()` will generate the same *sequence* of numbers, not the same *number*. The same is true for any hard-set seed; not just 1. – WhozCraig Oct 22 '16 at 19:55
  • Thank you! You have given me the most helpful answer I've found on the internet. I'll have to figure out where to incorporate `srand()` into the code so it gives me a different set of numbers each time the program is run, but your answer helps so much. – Ash. S Oct 22 '16 at 20:36