# Using conio.h in C++

• 12-01-2005
mikeprogram
Using conio.h in C++
Hello, Everyone:

My friend and I were doing a C++ program which relates to dice. Here is the problem:

Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] Figure 7.32 shows the 36 possible combinations of the two dice. Your program should roll the two dice 36,000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable.

Here is the code:
Code:

// Include files
#include <iostream>  // used for cin, cout
#include <conio.h>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

// Global Type Declarations

// Function Prototypes
void instruct (void);
void pause ();

//Global Variables - should not be used without good reason.

int main ()
{
// Declaration section

const int arraySize = 13;
int die1,
die2,
frequency[ arraySize ] = { 0 };

// Executable section
instruct ();

srand( time( 0 ) );

//Roll Dice 36000 times store result in frequency array
for ( int roll = 0; roll < 36000; ++roll ) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
++frequency[ die1 + die2 ];
}
cout << setw( 10 ) << "Dice Combination"
<< setw( 17 ) << "Frequency\n\n";

//Start at Element 2 To Skip Elements 0 and Element 1
for ( int combo = 2; combo < arraySize; ++combo )
cout << setw( 9 ) << combo << setw( 18 ) << frequency[ combo ]
<< endl;

pause ();
return 0;
}

void instruct (void)
{
// Declaration section
cout << "This program will simulate the rolling of two dice 36000 times. "
<< "It will then\nprint out the results of how many times each "
<< "combination of the two dice\nappeared. You should see in the "
<< "results that the 7 combination will appear\nthe most with the 2 "
<< "combination and the 12 combination appearing the least.\n"
<< "_______________________________________________________________"
<< "_____________"
<< "\n" << endl;

// Executable section
}

void pause ()
{
// Declaration section

// Executable section
cout << "\nPress any key to continue...";
getch();
cout << "\r";
cout << "                            ";
cout << "\r";
}

However, the g++ compiler says that <conio.h> does not exist:
Code:

dice.cpp:3: conio.h: No such file or directory
I understand <conio.h> is used in C not in C++, however, I need help finding out what <conio.h> means in C++. Please someone help us figure out the problem and what does conio.h means in C++. I will appreciate any help. Thank you.
• 12-01-2005
SlyMaelstrom
Why don't you take another look at that code you posted and try and see any relevance to the paragraph you wrote above it. :)

Check to see if you have conio.h in your include folder of your compiler.
• 12-02-2005
mikeprogram
My apologies. I wrote accidentally the wrong program. The actual program is in the same post. Sorry about that!
• 12-02-2005
7stud
Quote:

I understand <conio.h> is used in C not in C++, however, I need help finding out what <conio.h> means in C++.
http://www.bloodshed.net/dev/faq.html
• 12-02-2005
Ancient Dragon
Quote:

I understand <conio.h> is used in C not in C++
Says who??? conio.h is a non-standard file in either C or C++, what works with one compiler may or may not work with another compiler even if the two compilers are for the same operating system. The functions in that file were originally written by Borland for use by their compilers (Turbo C). Some (but not all) other compilers are also supporting some of the functions.
• 12-02-2005
Salem
Since it's perfectly possible to do the program without conio.h, why worry about it's non existence.

You're only waiting for a key at the end, and that's discussed in the FAQ.
• 12-02-2005
mikeprogram
Okay, thank you for your good advices to all of you who help me figure out what was the problem. I drop off <conio.h> and rearrange the code a little bit. Thank you once again.