This is a board game created using C++. In order to win, one player must be able to reach the end of the board three times. But the problem is that it is the the first player who is entered into the board game that is always winning. I have a function setup so the that the simulated dice uses a random number-generator. How can I fix this issue?
#include <iostream>
using namespace std;
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>
#define SIZE 20 //EDITED: SIZE is Defined at 20 for the number of squares wanted for the board game. Makes is easier for change
//NOTE TO SELF: This is the revamped version. "EDITED" marks addition/change from 'assignment2_fixed.cpp
/**********************
* FUNCTION PROTOTYPES *
***********************/
void displayRules();
void action();
void firstRoll();
void showState();
int spinDie();
bool play();
bool winner();
int square[SIZE]; //For each square on the board, SIZE is Defined
const int loseTurn=1, switchPlace=2, goAgain=3, moveBack=4; //CONSTANT! Labels each action location for each square
int *location, *lastsq, *losTurn; //EDITED: Declared for dynamic allocation for location on board, last square of board, and lost turn
string *names; //EDITED: This is to hold the names of the players
int numPlayers = 0; //Holds the number of players
int turn; //EDITED: First players turn
/************
* FUNCTIONS *
*************/
void displayRules(){ //Displays the rules to the player once the game starts
cout << "Welcome to 3X" << endl;
cout << "The first player to go around the board three times wins!" <<endl;
cout << "The player with the lowest number goes first" << endl;
cout << "The players take turns until one player wins" << endl;
cout << "The players must move forward the number of spaces the die displays" << endl;
cout << "If the player lands on a square with an action, the player must perform that action, otherwise the next player goes" <<endl;
cout << endl << endl;
}
void action(){ //EDITED: This lists actions for each square
square[2] = goAgain;
square[4] = switchPlace;
square[6] = goAgain;
square[8] = loseTurn;
square[11] = goAgain;
square[14] = moveBack;
square[16] = switchPlace;
square[18] = loseTurn;
}
void firstRoll(){ //EDITED: Initializes for the first die roll
numPlayers = spinDie();
for(int i=0; i<numPlayers; i++)
location[i];
for(int i=0; i<numPlayers; i++)
lastsq[i];
for(int i=0; i<numPlayers; i++)
losTurn[i];
}
void showState(){
for(int i=0; i<numPlayers; i++){
cout << "Player " << names[i] << " is at location " << location[i] << endl;
}
for(int i=0; i<numPlayers; i++){
cout << names[i] << " lands on last space " << lastsq[i] << " times " << endl;
}
}
int spinDie(){ //For the dice. Number is generated randomly based on time
int x = rand()%6 +1 ;
cout<< " a "<< x<< " was rolled"<<endl;
return x;
}
bool play(){ //EDITED: Play function through squares
srand(time(0));
while(true){
if(losTurn[turn]){
losTurn[turn] = 0;//EDITED: Finds if player lost turn based on the value of 'turn'
turn = (turn) % numPlayers;
continue;
}
cout << "Turn: "<< names[turn] << endl;
int spot = spinDie(); //EDITED: Players spot on the board based on random number generator
cout << "Number on die : " << spot << endl;
location[turn] += spot;
if(location[turn] >= SIZE-1){
location[turn] -= SIZE;
lastsq[turn]++;
cout << names[turn] << " passes End!"<<endl;
if(winner())
return true;
}
switch(square[location[turn]]){
case goAgain:
cout<<"Action: Go again.\n";
showState();
break;
case loseTurn:
cout<<"Action: Lose turn.\n";
losTurn[turn] += 1;
break;
case switchPlace:
cout<<"Action: Switch places\n";
break;
case moveBack:
cout<<"Action: Move back 2 places.\n";
location[turn] -= 2;
break;
default:
cout<<"Action: None\n";
}
showState();
turn = (turn) % numPlayers;
}
return false;
}
bool winner(){ //Finds the winner who hits the end square three times
if(lastsq[turn] == 3){
cout<<"\n"<<names[turn]<<" WIN!\n";//Prints name of player with their turn
return true;
}
return false;
}
/***************
* MAIN PROGRAM *
****************/
int main(){
srand(time(0)); //Makes time based number in die random
displayRules(); //Diplays the rules first
cout << "Please type in the number of players between 1-6 : " << endl;
cin >> numPlayers;
while ((numPlayers < 1) || (numPlayers > 6)) //EDITED: Guard against any values outside of 1-6
{
cout << "ERROR: Enter a value from 1 - 6: ";
cin >> numPlayers;
}
names = new string[numPlayers]; //EDITED: Dynamically allocates array of string to hold names of players
location = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold locations of players
losTurn = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold lost turn of players
lastsq = new int[numPlayers]; //EDITED: Dynamically allocates array of int to hold count of each time player reach end square
for(int i=0; i<numPlayers; i++){
cout << "Enter name of player " << i+1 << " " << endl; //EDITED: To get name of each player starting with Player 1 (i+1)
cin >> names[i]; //EDITED: Gets names of players into the dynamically allocated strings
}
while(true){//EDITED: Continues the loop
if(play())//EDITED: If the "Play" function is true
break;//EDITED: Breaks once the winner is found
}
return 0;
}
Although your question is vague, I can give you some help to start you off. First off I'm going to show your code for the random factor on the dice.
int spinDie(){ //For the dice. Number is generated randomly based on time
int x = rand()%6 +1 ;
cout<< " a "<< x<< " was rolled"<<endl;
return x;
This code makes it so that the compiler will generate ANY number within an integer range. Then you divide that random number by 6 and find the remainder with the remainder operator (%). The +1 will add to the remainder if it is a factor of 6 such as 36. Your code is fine. The problem is that it is based off of time. The first one on the board probably gets similar or the same numbers each time and the second one does too. Which would explain the first one always winning. I'm assuming the first one gets similar or the same rolls every time because of your lack of explanation of the SPECIFIC problem. Hope this helped a little lol.
Related
I am working on a program for the game Scattegories. In the program there is a table that the player inputs the words while a timer thread is working in the background. I would that when the time is over, the player won't be able to input anymore and for his turn to end.
How can I make that happen?
The table and the timer:
void timer()
{
cout << "You got 2 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop
for (int i = 120; i > 0; i--)
{
sleep_for(1s);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table(int plr)
{
string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "An object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" };
string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
std::thread t1(timer); //gives time to write the words. Optimaly it would finish the round for player when time is up
t1.detach();
cout << "When the timer ends please enter '0' in the remaining catagories\n";
for (int i = 0; i<12; i++) //changing catagory
{
cout << ctr[i] << ": ";
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
}
Full code:
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
#include <chrono>
#include <thread>
using std::cout;
using std::cin;
using std::string;
using std::getline;
using namespace std::chrono_literals;
using std::this_thread::sleep_for;
void ltr() //gives a random letter
{
char letter;
letter = rand() % 26 + 65; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 2 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop
for (int i = 120; i > 0; i--)
{
sleep_for(1s);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table(int plr)
{
string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "An object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" };
string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
std::thread t1(timer); //gives time to write the words. Optimaly it would finish the round for player when time is up
t1.detach();
cout << "When the timer ends please enter '0' in the remaining catagories\n";
for (int i = 0; i<12; i++) //changing catagory
{
cout << ctr[i] << ": ";
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming evreything is writen down
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<12; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
sleep_for(5s);
}
int points() //points gained per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act() //running the program
{
int Players, Points[6];
cout << "How many people are playing? (Up to six players)";
cin >> Players;
ltr();
table(Players);
//Points = points();
cout << "You have earned " << Points << " this round\n\n";
return 1;
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
srand(time(NULL)); //gives a differant pattern of letters every time
int Points;
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act(); //keeping score of the rounds
}
else
{
auto end = std::chrono::high_resolution_clock::now();
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
sleep_for(5s); //time to read the last text
return 0;
}
/*
To do list:
-Make timer stop the table when time is up
-Check if words in the table (for differant players) are the same and give points accordingly
-Check if words are actual words (connect an online dictonary?)
-Make interface? (if possible and I have time to learn how)
-Comment rest of the code
*/
P.S
I'm tying to keep this code portable so I preffer sugestions that will keep it this way. But I would appreciate any suggestion.
I'm using windows 10 (but I'm writing console aplications) I have access to c++11 and c++14
A non-portable solution
bool getUserInput(string &result, Time deadline) // pick a timer, any timer
{
while(deadline > Time::now()) // assuming your timer has a now function
{
if(kbhit()) // THIS IS NOT PORTABLE. On windows it's called _kbhit()
{
char key = getch(); //read the key (also non-portable)
if(key == '\n' or key == '\r')
{
return true;
}
else
{
string += key;
}
}
}
return false;
}
When I run the code I get the messege Scattegories.exe has triggered a breakpoint.
It happens after I enter the 12th nuber into my array, but the array still has a "spot" for it.
There is also a thread so maybe it's that, but it seems to work fine.
So I don't know what it is, thank you.
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
#include <chrono>
#include <thread>
using std::cout;
using std::cin;
using std::string;
using std::getline;
using namespace std::chrono_literals;
using std::this_thread::sleep_for;
void ltr() //gives a random letter
{
char letter;
letter = rand() % 26 + 65; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 1.5 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop
for (int i = 10; i > 0; i--)
{
sleep_for(1s);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table(int plr)
{
string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "A object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" };
string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
std::thread t1(timer); //gives time to write the words. Optimaly it would run in the background while each player writes the words.
for (int i = 0; i<12; i++) //changing catagory
{
cout << ctr[i] << ": ";
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming evreything is writen down
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<12; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
sleep_for(5s);
}
int points() //points gained per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act() //running the program
{
int Players, Points[6];
cout << "How many people are playing? (Up to six players)";
cin >> Players;
ltr();
table(Players);
//Points = points();
cout << "You have earned " << Points << " this round\n\n";
return 1;
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
srand(time(NULL)); //gives a differant pattern of letters every time
int Points;
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act(); //keeping score of the rounds
}
else
{
auto end = std::chrono::high_resolution_clock::now();
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
sleep_for(5s); //time to read the last text
return 0;
}
/*
To do list:
-Convert to arduino
-Make timer work in background of of table
-Check if words in the table (for differant players) are the same and give points accordingly
-Check if words are actual words (connect an online dictonary?)
-Make interface? (if possible and I have time to learn how)
-Think of what to do with Hardwear
-Comment rest of the code
*/
std::thread::~thread says this :
If *this has an associated thread (joinable() == true), std::terminate() is called.
If you ever destroy a std::thread that is joinable, std::terminate() is called. Visual Studio kindly breaks on std::terminate() to allow you to inspect what went wrong. In void table(int plr) you create a thread with std::thread t1(timer); but you never join with it or detach from it.
To solve this, your timer method should be changed to return immediately when the player has finished his turn, and then the main thread should join with it.
I'm writing this program for the game "Scattegories" and I would like to connect some sort of dictionary (to check if the words exist) to the program.
If I were to do that how would I do it?
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
#include <chrono>
#include <thread>
using std::cout;
using std::cin;
using std::string;
using std::getline;
using namespace std::chrono_literals;
using std::this_thread::sleep_for;
void ltr() //gives a random letter
{
srand(time(NULL)); //gives a differant pattern every time
char letter;
letter = rand() % 26 + 65; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 1.5 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop
for (int i = 90; i > 0; i--)
{
if (i % 5 == 0)
cout << i << "\n";
sleep_for(1s);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table()
{
int plr, ctr;
string lst[5][20]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cout << "How many players?";
cin >> plr;
cout << "How many catagories?";
cin >> ctr; //parameters for later
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
cout << "Player number " << x+1<<":";
timer(); //gives time to write the words. Optimaly it would run in the background while each player writes the words.
for (int i = 0; i<ctr; i++) //changing catagory
{
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming evreything is writen down
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<ctr; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
sleep_for(5s);
}
int points() //points gained per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act() //running the program
{
int Points;
ltr();
table();
Points = points();
cout << "You have earned " << Points << " this round\n\n";
return Points;
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
int Points;
cout << "Starting in five seconds\n";
sleep_for(5s);
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act(); //keeping score of the rounds
}
else
{
auto end = std::chrono::high_resolution_clock::now();
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
sleep_for(5s); //time to read the last text
return 0;
}
/*
To do list:
-Convert to arduino
-Make timer work in background of of table
-Check if words in the table (for differant players) are the same and give points accordingly
-Check if words are actual words (connect an online dictonary?)
-Make interface? (if possible and I have time to learn how)
-Think of what to do with Hardwear
-Comment rest of the code
-Make a point count for each player
-change "srand" placement
*/
Please keep in mind that I'm relatively new to programing (almost half a year), so please try to explain it as simple as you can, thanks.
Can be an interesting exercise to implement your own dictionary.
Use a large array of characters to store the words, sorted alphabetically, each null-terminated. In addition, keep an array of indexes, each pointing to the start of a word.
Then implement a dichotomic search for lookup. It will be fairly efficient because you will find a word after at most Log(N) string comparisons, each taking between 1 and L character comparisons, where L is the word length.
A solution based on hashing can be more efficient, but is a little harder to code.
Hello I'm currently taking C++, and I'm a beginner. We had a choice to choose a dice game for our program. I chose a game that has the following rules:
Choose a number and roll the dice. You score a point every time you roll that number. When you roll that number, you get another turn. When that number is not rolled, the turn is over. Mark the tally for each time you roll the number. First one to a certain score of 10 points wins.
I've been working on this for the past 2 days, and I'm so frustrated. Any help is greatly appreciated. We're using classes and constructors. My main issue is being able to go back and forth between the two players. I tried using a do while loop, but it didn't really help. Here's my code:
//Dice game
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
class Die
{
private:
int num;
public:
Die(); //Default constructor because it doesn't require arguments
void roll();
int getNum();
void gameRules();
};
class Players
{
private:
int player1Num;
int player2Num;
public:
void playerTurn();
};
void Die::gameRules()
{
cout << " ****Welocome to Madawi's Dice Game ****\n\n ";
cout << "Here are the rules:\n\n ";
cout << "This is a two player game, so grab a buddy!\n\n ";
cout << "\t1.)Please choose a number from 1-6\n\n ";
cout << "\t2.)Then roll the dice, if it lands on the number";
cout << "\tyou chose, you get a point and go again\n\n ";
cout << "\t3.)If you choose a number and it doesn't land on";
cout << "\tthe number you chose,you don't recieve a point, ";
cout << "\tand the second player goes\n\n ";
cout << "\t4.)The first person to get to 5 points first wins!\n\n ";
cout << "ENJOY!!! Let's begin:\n\n ";
}
Die::Die()
{
num = 1; //Initialize so that the values start at one
srand((unsigned)time(NULL));
}
void Die::roll()
{
num = rand()%6 + 1;
}
int Die::getNum()
{
return num;
}
void Players::playerTurn()
{
Die die1;
cout << "Hello player 1! Please choose a number from 1-6:\n";
cin >> player1Num;
cout << "You've chosen the number " << player1Num << endl;
die1.roll(); //rolls the dice
cout << die1.getNum() << endl; //displays number rolled
if (player1Num == die1.getNum())
{
cout << "Good job player 1! You got the same number\n ";
player1Points++; //Keeps track of player 1's score
if(player1Points == 5)
{
cout << "Congratulations player 1 you've won the game!\n";
cout << "Thanks for playing!\n ";
}
}
else
{
cout << "Sorry the numbers didn't match up\n ";
cout << "Player 2 its your turn\n ";
cout << "Player 2 please choose a number ";
cin >> player2Num;
cout << "You've chosen the number " << player2Num << endl;
die1.roll();
cout << die1.getNum() << endl;
if(player2Num == die1.getNum())
{
cout << "Good job player 2! You got the same number\n ";
player2Points++; //Keeps track of player 2's points
cout << "Player 2 its your turn again, please choose a number:\n ";
cin >> player2Num;
die1.roll();
cout << die1.getNum() << endl;
}
if(player2Points == 5)
{
cout << "Congratulations player 1 you've won the game!\n";
cout << "Thanks for playing!\n ";
}
}
}
int main()
{
Die dice1;
Players player1;
Players player2;
dice1.gameRules(); //Says the game rules
player1.playerTurn(); //Player makes a selection
return 0;
}
You need to change your architecture:
class Players
{
private:
int player1Num;
int player2Num;
public:
void playerTurn();
};
int main()
{
Die dice1;
Players player1;
Players player2;
//...
}
As defined above, you have 4 players. Each Players class has 2 player numbers.
Decide. Are you having a class containing all the players and controlling them or are you have a single player class and letting the main function control the players?
You should create helper function void makeTurn(int player); And use it to make move for any player. It should contain do-while loop with rolling and scoring.
Pseudocode:
function makeTurn(playerNumber):
display "Player " + playerNumber + "turn"
do
roll = rollDice();
if (roll == playersChoice[playerNumber])
display "You scored one point!"
playersPoints[playerNumber]++;
else
display "Sorry, you have failed :("
break //End the loop
while true //Do infinitely (till break)
end function
Also, I don't see a reason for the Players class. Use an array for containing player scores and choices, then you can easily extend it to more players. If you meant it to be an instance of player, then you should just have score and choice fields, and the player move should be the makeMove function from the code I presented to you before, just using local fields instead of playersChoice and playersPoints arrays.
new to the forums here and beginning to learn C++. This site has already helped me so much with syntax and other things. What I'm trying to do with my code is have the number print to screen, have the time delay, then print the next number. Currently the time delay works, but it prints all 13 numbers generated. Any ideas of what I'm doing wrong?
Here's my code:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main( )
{
// Function prototypes
int random ( int minValue, int maxValue);
// Constant declarations
const int maxValue = 9;
const int minValue = 0;
// Local variable declarations
int seed;
int numberOfPeople;
int peopleCount = 0;
int numberCount;
int number;
// Initialize the random number generator
cout << "Welcome to the Lottery!" << endl;
cout << "Enter your lucky number to start: " << endl;
cin >> seed;
srand (seed);
// Generate and display numbers
cout << "Enter the number of people participating in the lottery:" << endl;
cin >> numberOfPeople;
cout << "Your lucky lottery numbers for the day are:" << endl;
cout.setf (ios::left, ios::adjustfield);
cout << setw(8) << "Pick 3" << setw(10) << "Pick 4" <<
setw(15) << "Pick 6" << endl;
while (peopleCount < numberOfPeople) {
numberCount = 0;
while (numberCount < 13){
number = random (minValue, maxValue);
Sleep (500); // pauses for half a second
cout << number << " ";
if (numberCount == 2){
cout << " "; }
else if (numberCount == 6){
cout << " "; }
else if (numberCount == 12){
cout << endl; } //end if, else if
numberCount++;
} //end nested while
peopleCount++;
} // end while
return 0;
} // end main()
/**
* Produces a pseudo-random number
* #param minValue minimum value that can be generated
* #param maxValue maximum value that can be generated
*
* #return psuedo-random number in the specified range
*/
int random ( int minValue, // min possible number to be generated
int maxValue) // max possible number to be generated
{
return ( (rand() % maxValue) + minValue);
} // end random()
cout is generally buffered, and the newline causes the buffer to be flushed to the screen. As you display the numbers on the same line, this could explain that everything is displayed at onece despite the delay that you've build in.
Use cout.flush(); to force output to be done without buffering delay. You could as well use the manipulator form: cout << number << " " << flush;