Using rand and while loops for Selection Menus?

Hello, I am in need of assistance on how to correctly output a selection menu using the rand function and while loops. I aim to have the line of code randomly output different statements from the first two selections, and for the program to continuously loop the number 3 is typed into the console.


Here is a rough draft for my line of code.

#include<iostream>
#include<string>
using namespace std;

int main(){

int number;

cout<<"**************"<<endl;
cout<<"Random Selection Generator"<<endl;
cout<<"**************"<<endl;

cout<<"1. Fortune Cookie saying."<<endl;
cout<<"2. Bible Verse."<<endl;
cout<<"3. Quit"<<endl;
cin>>number;

//Random Outputs?
while (number == 1){
cout<<"Good Luck is coming your way!\n"<<endl;
break;
}
while (number == 1){
cout<<"Bad Luck is coming your way!\n"<<endl;
break;
}

//Repeat Until Quit?
while (number == 1 || number == 2)
{
cout<<"**************"<<endl;
cout<<"Random Generator"<<endl;
cout<<"**************"<<endl;

cout<<"1. Fortune Cookie saying."<<endl;
cout<<"2. Ancient Proverb."<<endl;
cout<<"3. Quit"<<endl;
cin>>number;
if (number == 3); //Not Quitting Correctly?
//Not Repeating the Selection Outputs?
}
}


I am aware that the current line of code will not output what I want, and that the rand function and statements of the second selection have yet to be implemented.

Help would be much appreciated!
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
#include <iostream>
#include <random>

// returns a random number from range [min,max)
int random_int( int min, int max)
{
    // Initializes a pseudo-random engine with a random number 
    static std::default_random_engine e( std::random_device{}());
    
    // Sets distribution parameters
    std::uniform_int_distribution<> d(min, max-1);
    
    // Returns a uniformly distributed value
    return d(e);
}

int main()
{
    for (int i = 0; i < 20; ++i)
    {
        std::cout << random_int(0,2);  // range 0-1
        std::cout << random_int(8,10); // range 8-9
    }
}

Topic archived. No new replies allowed.