A Decision making program in C++

Hi there,
I have come up with this program that will help you make a decision, whether it is through tossing a coin or maybe rolling dice or through choosing a playable card through a deck of fifty-two cards. This program is the solution to all three of them.
What the program does is ask, whether the user would prefer a coin, or card, or a dice. Once the user has inputted their desired choice, the program runs, and prints out a valid output.
In the making of this program, I have used three more methods apart from the main, to provide the result.
Using methods offer flexibility and re-usability to our program.

C++ code for decision making

Below is the complete C++ code:

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include <stdlib.h>
int toss()
{

int i;
time_t t;

srand( time(0));

i= rand() % 2;
if(i==0)
    printf("The coin gives out Heads");
else
    printf("The coin gives out Tails");
getch();
return 0;

  }
int roll()
{
int i;
time_t t;

srand( time(0));

i= rand() % 6;
i=i+1;
printf("The Dice Gives out %d",i);
getch();
return 0;
}
int card()
{
int i,suit;
time_t t;

srand( time(0));

i= rand() % 13;
i=i+1;
if(i==11)
    printf("Jack of ");
if(i==12)
    printf("Queen of ");
if(i==13)
    printf("King of ");

suit=rand() % 4;

 if(suit==0)
    printf("Hearts ");
 if(suit==1)
    printf("Spades ");
 if(suit==2)
    printf("Clubs ");
 if(suit==3)
    printf("Diamonds ");
getch();
return 0;
}
int main()
{
    int ch ;
    printf("Enter: 1/2/3 for card/coin/dice respectively \n");
    scanf("%d",&ch);
    switch (ch)
    {
        case 1 :
            card();
            break;
        case 2 :
            toss();
            break;
        case 3 :
            roll();
            break;
        default :
            printf("invalid");
    }
    getch();
    return 0;
}

 

The output of the above decision making C++ program is given below:

Enter: 1/2/3 for card/coin/dice respectively
1
Jack of Spades


Enter: 1/2/3 for card/coin/dice respectively
2
The coin gives out Heads

Enter: 1/2/3 for card/coin/dice respectively
3
The Dice Gives out 3

 

Below is the explanation of our decision making C++ program:

Also, read:

The code begins by importing the standard libraries of i/o, time, etc.
Apart from the main function, I have implemented three more functions which are:

  1. toss(): To toss a coin, which gives either Head or Tails.
  2. roll(): To roll a dice, which generates a random number between one to six.
  3. card(): which generates a random number between one to thirteen, and prints the face value of the cards; it then again generates a random card between one-two four and prints out the suit of the card.

In the main body of the program, we take input depending on the action we would like to perform. Depending on the input provided, the respective function is called, which provides us with the desired output.

Leave a Reply

Your email address will not be published. Required fields are marked *