Implementing a break in my If-else statements

I'm trying to create a program that rolls a random number 1-6, which is then outputted to a die face, then it stops after each number has been rolled once. I figured majority of the code out, just don't know where to insert my break statements. What should I do?

**CODE BELOW**

// Constants
const int MIN_VALUE = 1; // Min number the die can be
const int MAX_VALUE = 7; // Max number the die can be


// Variables
int randomNumber;

// System time
unsigned seed = time(0);

// Seed the random number generator
srand(seed);
cout << "The number rolled is:" << endl;
randomNumber = (rand() % (MAX_VALUE - MIN_VALUE + 1));
cout << randomNumber << endl << "---------------" << endl;

if (randomNumber == 1) // If a 1 is drawn, output X7
{
OutputData(32);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 2) // If a 2 is drawn, output X1 and X6
{
OutputData(9);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 3) // If a 3 is drawn, output X1, X6 and X7
{
OutputData(41);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 4) // If a 4 is drawn, output X1, X3, X4 and X6
{
OutputData(89);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 5) // If a 5 is drawn, output X1, X3, X4, X6, and X7
{
OutputData(121);
Sleep(2000);
cout << "---------------" << endl;
}
else if (randomNumber == 6) // If a 6 is drawn, output X1, X2, X3, X4, X5, and X6
{
OutputData(95);
Sleep(2000);
cout << "---------------" << endl;
}
}
Last edited on
I am unsure what you are asking. Do you want to roll the die more than once?
No. I want the loop to stop once 1,2,3,4,5, and 6 were each rolled once, respectively.
Duthomhas wrote:
Do you want to roll the die more than once?
PopSmoke wrote:
No.


PopSmoke wrote:
I want the loop to stop once 1,2,3,4,5, and 6 were each rolled once, respectively.

That’s more than once.
Last edited on
my bad, I thought you meant for each number
So, what exactly do you want your program to do? Produce the numbers 1–6 in random order?
Yeah, then stop once each number has been called once in the loop.

-stop the program automatically after each of the outputs is displayed at least one time (1-6)
Last edited on
If you want EXACTLY once, then just put the numbers in an array and use std::shuffle()

If you want UNTIL AT LEAST ONCE then, e.g.,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;

int main()
{
   srand( time( 0 ) );
   const int LOW = 1, HIGH = 6;
   vector<bool> available( 1 + HIGH, true );
   int remaining = HIGH - LOW + 1;
   
   while( remaining )
   {
      int x = LOW + rand() % ( HIGH - LOW + 1 );
      remaining -= available[x];
      available[x] = false;
      cout << x << '\n';;
   }
}


2
4
3
2
5
2
6
4
2
1
Do you mean something like this - each number in the designated range will only be visited once in a random order:

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 <iostream>
#include <random>
#include <algorithm>
#include <numeric>

void OutputData(unsigned n) { std::cout << " output " << n << '\n'; }	// For testing

std::mt19937 rng(std::random_device {}());

int main() {
	constexpr unsigned MIN_VALUE {1}; // Min number the die can be
	constexpr unsigned MAX_VALUE {6}; // Max number the die can be
	constexpr unsigned odata[MAX_VALUE - MIN_VALUE + 1] {32, 9, 41, 89, 121, 95};

	unsigned nos[MAX_VALUE - MIN_VALUE + 1];

	std::iota(std::begin(nos), std::end(nos), 1);
	std::shuffle(std::begin(nos), std::end(nos), rng);

	for (auto n : nos) {
		std::cout << "The number rolled is: " << n;
		OutputData(odata[n - MIN_VALUE]);
	}
}



The number rolled is: 1 output 32
The number rolled is: 6 output 95
The number rolled is: 5 output 121
The number rolled is: 2 output 9
The number rolled is: 3 output 41
The number rolled is: 4 output 89

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;

void output( int p )
{
   char bits[] = { 0, 7, 6, 1, 5, 2, 4, 8, 3 };
   cout << "\n\n";
   for ( int i = 0; i < 9; i++ ) cout << ( p & (1<<bits[i]) ? '*' : ' ' ) << ( ( i + 1 ) % 3 ? "" : "\n" );
   cout << "\n\n";
}


int main()
{
   int dice[] = { 0, 32, 9, 41, 89, 121, 95 };
   srand( time( 0 ) );
   const int LOW = 1, HIGH = 6;
   vector<bool> available( 1 + HIGH, true );
   int remaining = HIGH - LOW + 1;
   
   while( remaining )
   {
      int x = LOW + rand() % ( HIGH - LOW + 1 );
      remaining -= available[x];
      available[x] = false;
      // cout << x << '\n';
      output( dice[x] );
   }
}




* *
* *
* *




* *
   
* *




* *
   
* *




*  
   
  *




* *
   
* *




* *
 * 
* *




*  
 * 
  *




*  
 * 
  *




* *
   
* *




*  
 * 
  *




   
 * 
   

Last edited on
that is a slick way to do the output, but standard 3 is the diagonal.
the rest match a standard d6.
Thanks @jonnin. I have edited to accommodate that. But there are still various possible orders (because the diagonal could go the other way, for example).

I just wondered why the student was outputting such odd numbers.
As the output relates to X1 - X7, possibly it is to interface to some hardware? 7-seg display?

if the requirement is to continue until at least one of each number is determined, then consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <random>
#include <bitset>

void OutputData(unsigned n) { std::cout << " output " << n << '\n'; }	// For testing

std::mt19937 rng(std::random_device {}());

int main() {
	constexpr unsigned MIN_VALUE {1}; // Min number the die can be
	constexpr unsigned MAX_VALUE {6}; // Max number the die can be
	constexpr unsigned odata[MAX_VALUE - MIN_VALUE + 1] {32, 9, 41, 89, 121, 95};
	const std::uniform_int_distribution<unsigned> distrib(MIN_VALUE, MAX_VALUE);
	std::bitset<MAX_VALUE - MIN_VALUE + 1> bits;

	while (!bits.all()) {
		const auto val {distrib(rng)};

		bits.set(val - MIN_VALUE);
		std::cout << "The number rolled is: " << val;
		OutputData(odata[val - MIN_VALUE]);
	}
}



The number rolled is: 3 output 41
The number rolled is: 3 output 41
The number rolled is: 2 output 9
The number rolled is: 2 output 9
The number rolled is: 1 output 32
The number rolled is: 4 output 89
The number rolled is: 5 output 121
The number rolled is: 1 output 32
The number rolled is: 5 output 121
The number rolled is: 3 output 41
The number rolled is: 6 output 95

@seeplus you're correct. Those numbers I used correlate to a TDB module which holds a die face and a 7 segment display
Topic archived. No new replies allowed.