I don't know what I'm doing.

Hi everyone,

I’m new to this programming world so I’m sorry if I’m asking a stupid question. I have a problem trying to get a variable output from a random number generator input. What I’m trying to do is take the output from the number generator and have that determine the out come. My number generator goes from 1-12, what I want it to do is have the output vary so that if an output is between 1-4 it says no, and if it is between 5-8 it say maybe, and 9-12 is yes. I got the number generator working fine, but I can’t get the code right to make it determine the proper outputs. Is there a special magic word I should be using? My “if” statements always come back with errors that I don’t know how to fix. I’m sure I’m trying to do something that is way beyond my knowledge level and I shouldn’t even be trying this, but I want to learn as much as possible.
- I don't know how your RNG works so I assume it gives you integer values -
1
2
3
4
5
6
7
// assume 'x' holds the random value: 
if ( x < 5 ) // x in [1,4]
   cout << "No";
else if ( x < 9 ) // x in [5,8]
   cout << "Maybe";
else
   cout << "Yep";


If you post your code we can tell you what you are doing wrong
Sorry I should have added it before.

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

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

int random_integer;
for(int index = 0; index < 1; index++){
random_integer = (rand() % 12)+1;
cout << random_integer << endl;
}

int random_integer;
Var yes, no ,maybe;

if (random_integer <= 4){
cout << "No\n";
}
Else if (random_integer <= 8) {
cout << "maybe \n";
}
Else if (random_integer <=12) {
cout << "yes \n";
}
}
C++ is case sensitive, replace 'Else' with 'else'

The line Var yes, no ,maybe; does nothing and 'Var' is not a standard type
That was my problem. I changed the else and everthing is fine. I've also stripped the line var from my code. Thank you very much. If you don't mind me asking, but what would I need to add if I wanted to have the generator produce multiple outputs and give me the responce to all of those outputs? i.e. 12,2,6,9,1.....yes,no,maybe,yes,no
Use a while loop around the code you want to repeat:

http://www.cplusplus.com/doc/tutorial/control/#while
You may also need to store the numbers in a container: http://www.cplusplus.com/reference/stl/deque/begin/
You've all given me great advise, thank you soo much. I'll begin studing these now.
OK, I'm going crazy! I've tried this with the while loop (which causes the whole thing to blow up). All I want is for it to give me the answer for each output.

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{


srand(time(NULL));

int random_integer;
for(int index = 0; index < 11; index++){
random_integer = (rand() % 12)+1;
cout << random_integer << endl;
}
if (random_integer < 5){
cout << "no\n";
}
else if (random_integer < 9) {
cout << "Maybe \n";
}
else
cout << "yes \n";
}

what I get is 11 variable numbers and the answer (yes, no, or maybe) for the last output. Shouldn't it tell me what each of the 11 outputs are? what am I missing?
closed account (iw0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main(){

	srand(time(NULL));

	int random_integer;

	for(int index = 0; index < 11; index++){

		random_integer = (rand() % 12)+1;
		cout << random_integer << ' ';
	
		if (random_integer < 5)cout << "no \n";

		if (random_integer >= 5 && random_integer< 9)cout << "Maybe \n";

		if (random_integer >= 9)cout << "yes \n";
	}
}
Wow! that worked great. What was I doing wrong?
very easy to create a RNG function or 2, setter and getter I include both in 1 function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int randomNumGen(int numOutOf)
{
     return 1 + rand() % numOutOf;
}

int main()
{
     srand(time(NULL));
     int num1=randomNumGen(12);
     int num2=randomNumGen(192382);

....  etc etc etc   ....


or you if you don't want to set on each call then:
1
2
3
4
5
6
7
8
9
10

void setRandNum(int numOutOf)
{
     randNum=numOutOf;
}

int getRandNum()
{
     return 1+rand()%randNum;
}

Last edited on
Topic archived. No new replies allowed.