I am new to C++, and I am having some trouble with some code I am writing. I have written a program that works, but it is not quite what is wanted. Here is the problem:
Every time the door bell howls, provide the Count with a random trick or treat. 1 in 5 times the program should hand out candy to the little goblin, however the other 4 times, the goblin should be tricked.
My solution works but is not random. I am having trouble understanding how to adapt the srand or rand to make this work. I am only 8 weeks into this, and I am still learning the syntax. Here is the code I wrote. It executes fine, but doesn't fully fulfill the requirements of the problem. Can anyone help or provide insight as to how I should use rand or srand to do this?
#include <iostream>
using namespace std;
int main()
{
cout << "Happy Halloween! ";
cout << "Count Dracula is trying to decide whether to Trick or Treat the Hobgoblins that ring his doorbell on Halloween!\n ";
cout << "\nPick a number between 1 and 5 to send a hobgoblin to ring his doorbell:\n ";
while (true) {
int Hobgoblin = 0;
cin >> Hobgoblin;
if (Hobgoblin == 3) {
cout << "Here is a big bag of candy, little Hobgoblin!\n ";
break;
} else if (Hobgoblin <= 2 ){
cout << "Here is some already chewed gum covered in pocket lint, tee hee!\n ";
cout << "Pick another Hobgoblin:\n ";
} else if (Hobgoblin >= 4 ){
cout << "Have some moldy cheese and smelly socks, bwah ha ha ha ha ha!\n ";
cout << "Pick another Hobgoblin:\n ";}
}
return 0;
}
sorry about the layout, I am having some trouble with the accepted "style".
What you want to do is replace your user input with a call to rand(). You'll then process the returned value of rand() to yield a value between 1 and 5. This is normally done by taking the modulus 5 of the returned value, adding 1 to it, and assigning it to an int.
You use the srand() once, before your loop, to seed the random number generator. This prevents the RNG from always returning the same sequence of values to you.
I had looked at the rand and srand links prior to posting, but was unsure that I fully understood them. I had never seen some of the tags that you had suggested looking at. Thank you for your help.
Okay. I have provided you with all the function for rand. That is one function. Now you just put somewhere that function for rand number. In the brackets of the function should be rand_number(5) - 1. So it can start from 1 to 5...
Now for example:
1 2 3 4 5 6 7 8 9 10
// Random
int i_need_number = 0;
i_need_number = rand_number(5) - 1;
cout << "WOW I DID GET RANDOM! " << i_need_number << endl;
......
#include <iostream>
#include <ctime>
usingnamespace std;
int rand_number(int x);
int main()
{
srand(time(NULL));
cout << "Happy Halloween! ";
cout << "Count Dracula is trying to decide whether to Trick or Treat the Hobgoblins that ring his doorbell on Halloween!\n ";
cout << "\nPick a number between 1 and 5 to send a hobgoblin to ring his doorbell:\n ";
while (true) {
int Hobgoblin = 0;
cin >> Hobgoblin;
if (Hobgoblin == 3) {
cout << "Here is a big bag of candy, little Hobgoblin!\n ";
break;
} elseif (Hobgoblin <= 2 ){
cout << "Here is some already chewed gum covered in pocket lint, tee hee!\n ";
cout << "Pick another Hobgoblin:\n ";
} elseif (Hobgoblin >= 4 ){
cout << "Have some moldy cheese and smelly socks, bwah ha ha ha ha ha!\n ";
cout << "Pick another Hobgoblin:\n ";}
}
system("PAUSE");
return 0;
}
int rand_number(int x){
return rand() % x;
}
If I do that, it will pick one number between 1 and five to be random every time it runs? Am I understanding that correctly? I did read the Cplusplus notes on rand and srand, as well as surf the net.
Thank you for your help, I will try to do that. If I add the rand, will I need to remove the else if's to make it run correctly?
Yes, it should return a number between 1 and 5. If you're not sure that you have your rand() set up properly, put it in a loop and print out, say, 100 instances of what it returns.
And, no, your while loop is essentially OK, except you haven't put a rand() in it (at least not one I can see).
#include <iostream>
#include <ctime>
usingnamespace std;
int rand_number(int x);
int main()
{
int Hobgoblin;
bool loopAgain = true;
srand(time(NULL));
cout << "Happy Halloween! " << endl;
cout << "Count Dracula is trying to decide whether to Trick or Treat the Hobgoblins that ring his doorbell on Halloween!\n ";
while (loopAgain) {
Hobgoblin = rand_number(5);
if (Hobgoblin == 3) {
cout << "Here is a big bag of candy, little Hobgoblin!\n ";
cout << "Pick another Hobgoblin:\n ";
} elseif (Hobgoblin <= 2 ){
cout << "Here is some already chewed gum covered in pocket lint, tee hee!\n ";
loopAgain = false;
} elseif (Hobgoblin >= 4 ){
cout << "Have some moldy cheese and smelly socks, bwah ha ha ha ha ha!\n ";
cout << "Pick another Hobgoblin:\n ";}
}
system("PAUSE");
return 0;
}
int rand_number(int x){
return rand() % x;
}
But I'm not sure you want a loop here, or if you just want to run the rand() once, provide the trick or treat, and exit. You may want to modify it according to how you want it to behave.