What does this function expect to return? - c++

Program specifies the following:
Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line.
The coin types are dollars, quarters, dimes, nickels, and pennies.
Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
Your program must define and call the following function.
void ExactChange(int userTotal, vector& coinVals)
Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
My code is below:
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
/*
1) Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line.
2) The coin types are dollars, quarters, dimes, nickels, and pennies.
3) Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
4) Your program must define and call the following function.
void ExactChange(int userTotal, vector<int>& coinVals)
5) Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
*/
void ExactChange(int userTotal, vector<int>& coinVals);
const int PENNIES_IN_DOLLAR = 100, PENNIES_IN_QUARTER = 25, PENNIES_IN_DIME = 10, PENNIES_IN_NICKEL = 5;
int main() {
int userTotal, changeRemaining;
cin >> userTotal;
changeRemaining = userTotal;
int dollars = changeRemaining / PENNIES_IN_DOLLAR;
changeRemaining = changeRemaining % PENNIES_IN_DOLLAR;
int quarters = changeRemaining / PENNIES_IN_QUARTER;
changeRemaining = changeRemaining % PENNIES_IN_QUARTER;
int dimes = changeRemaining / PENNIES_IN_DIME;
changeRemaining = changeRemaining % PENNIES_IN_DIME;
int nickels = changeRemaining / PENNIES_IN_NICKEL;
changeRemaining = changeRemaining % PENNIES_IN_NICKEL;
int pennies = changeRemaining;
vector<int> changeAmount;
vector<int> coinVals{dollars, quarters, dimes, nickels, pennies};
changeAmount = coinVals;
ExactChange(userTotal, changeAmount);
return 0;
}
void ExactChange(int userTotal, vector<int>& coinVals) {
if (userTotal == 0) {
cout << "no change" << endl;
}
if(coinVals.at(0) > 0) {
cout << coinVals.at(0);
if(coinVals.at(0) > 1) {
cout << " dollars" << endl;
}else {
cout << " dollar" << endl;
}
}
if(coinVals.at(1) > 0) {
cout << coinVals.at(1);
if(coinVals.at(1) > 1) {
cout << " quarters" << endl;
} else {
cout << " quarter" << endl;
}
}
if(coinVals.at(2) > 0) {
cout << coinVals.at(2);
if(coinVals.at(2) > 1) {
cout << " dimes" << endl;
}else {
cout << " dime" << endl;
}
}
if(coinVals.at(3) > 0) {
cout << coinVals.at(3);
if(coinVals.at(3) > 1) {
cout << " nickels" << endl;
}else {
cout << " nickel" << endl;
}
}
if(coinVals.at(4) > 0) {
cout << coinVals.at(4);
if(coinVals.at(4) > 1) {
cout << " pennies" << endl;
}else {
cout << " penny" << endl;
}
}
}
However, zybooks, the site where our college course does our labs gives me these messages indicating some problem with my code:
My question is, what do those "messages" mean? Any how could I fix the issues? It seems to me they are saying the function is outputting something incorrectly given a certain input, however, they also don't give me a correct output to compare to.

Your code is taking user input, converting it manually directly in main() before calling ExactChange(), and then passing the result of that conversion to ExactChange() for it to display as-is.
The way I read the instructions, and the way the screenshot shows the tests being performed, it is more likely that ExactChange() is expected to take user input and convert it onto a vector of coin amounts as output.
That would be consistent with the fact that ExactChange() takes a vector by non-const reference, which means it can freely modify the contents of the vector. If ExactChange() were meant for text output, it would make more sense for it to take the vector by const reference instead, so that it can't modify the vector, only view it.
If so, then it makes sense that your program would pass tests that give it user-defined input and look for specific text results, but your program would fail tests that execute ExactChange() directly with specific inputs and look for specific vector outputs. That is the whole point of Unit Tests - to test functions directly for expected behaviors, not test whole programs.
Your code likely needs to look more like the following instead:
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
/*
1) Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line.
2) The coin types are dollars, quarters, dimes, nickels, and pennies.
3) Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
4) Your program must define and call the following function.
void ExactChange(int userTotal, vector<int>& coinVals)
5) Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
*/
void ExactChange(int userTotal, vector<int>& coinVals);
const int PENNIES_IN_DOLLAR = 100, PENNIES_IN_QUARTER = 25, PENNIES_IN_DIME = 10, PENNIES_IN_NICKEL = 5;
int main() {
int userTotal;
cin >> userTotal;
if (userTotal == 0) {
cout << "no change" << endl;
}
else {
vector<int> coinVals;
ExactChange(userTotal, coinVals);
if (coinVals[0] > 0) {
cout << coinVals[0];
if (coinVals[0] > 1) {
cout << " dollars" << endl;
} else {
cout << " dollar" << endl;
}
}
if (coinVals[1] > 0) {
cout << coinVals[1];
if (coinVals[1] > 1) {
cout << " quarters" << endl;
} else {
cout << " quarter" << endl;
}
}
if (coinVals.at(2) > 0) {
cout << coinVals[2];
if (coinVals[2] > 1) {
cout << " dimes" << endl;
}else {
cout << " dime" << endl;
}
}
if (coinVals[3] > 0) {
cout << coinVals[3];
if (coinVals[3] > 1) {
cout << " nickels" << endl;
}else {
cout << " nickel" << endl;
}
}
if (coinVals[4] > 0) {
cout << coinVals[4];
if (coinVals[4] > 1) {
cout << " pennies" << endl;
}else {
cout << " penny" << endl;
}
}
}
return 0;
}
void ExactChange(int userTotal, vector<int>& coinVals) {
int dollars = userTotal / PENNIES_IN_DOLLAR;
userTotal %= PENNIES_IN_DOLLAR;
int quarters = userTotal / PENNIES_IN_QUARTER;
userTotal %= PENNIES_IN_QUARTER;
int dimes = userTotal / PENNIES_IN_DIME;
userTotal %= PENNIES_IN_DIME;
int nickels = userTotal / PENNIES_IN_NICKEL;
userTotal %= PENNIES_IN_NICKEL;
int pennies = userTotal;
coinVals.resize(5);
coinVals[0] = dollars;
coinVals[1] = quarters;
coinVals[2] = dimes;
coinVals[3] = nickels;
coinVals[4] = pennies;
}

I agree with Remy. An easier way of saying it would be that Zybooks sets the size for the vector as you open the section:
vector<int> changeAmount(5);
To pass the two problems, instead of clearing out the (5) or push_back'ing the amounts (which is 100% what I originally did), you just need to point the coin amount to the already created vector index directly.
Your code did this:
vector<int> coinVals{dollars, quarters, dimes, nickels, pennies};
changeAmount = coinVals;
When all you needed to do for each amount was:
coinVals.at(3) = dollars;
Because the vector is pass by reference (that '&' right before the vector name), your updates to the vector will be carried back through to your main function.
Thanks for posting! This thread helped me solve this one as well!

Related

How to code in C++ mutiple return values to main from mutiple modules and display each modules totals?

I must write a program where the user can choose to practice with topic addition or topic multiplication that starts with a self-driven menu.
It must keep track of questions answered right, wrong and the number of questioned asked.
Which my current program is doing within each module(topic). Example Addition keeps track of the questions while the user is practicing Addition only and Multiplication does the same.
However, they are not being feedback to main, so they are not being added or displayed before the user can select another topic to practice or to exit the program.
Currently it is only to keeping track of the question (right /wrong/ total of questions) for each module (topic).
My goal is for the values to be passed to main and display the total number (right /wrong/ total of questions) before the user exits the program, but at the same time I must display the number of question in the Additional Topic and the Multiplication topic and provide a total.
Example Table of Addition, Multiplication and Totals ?
This is the code I have to start with. Can someone help me in how to code to return values of the (right /wrong/ total of questions) of the two topics and accomplish to display something like the table information.
******************************************************************************* /
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <string> // String managment funtions.
#include <iostream> // For input and output
#include <cmath> // For math functions.
#include <math.h>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////////
// Implementing menu driven programs.
// Function Prototypes.
int menu();
void sums();
void products();
int main()
{
srand(time(0));
int option;
do {
option = menu();
switch (option) {
case 1: {
sums();
break;
}
case 2: {
products();
break;
}
default:
cout << "Program exit" << endl;
}
} while (option != 6);
return 0;
}
int menu()
{
cout << "Please select an option" << endl;
cout << "1) Practice with Addition " << endl;
cout << "2) Pratice with Multiplication " << endl;
cout << "3) Exit the program " << endl;
int option;
cin >> option;
return option;
}
void sums()
{
string keepgoing;
unsigned int quantity_total_questions = 0U;
unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;
do {
const int minValue = 10;
const int maxValue = 99;
int y = (rand() % (maxValue - minValue + 1)) + minValue;
// cout<< " the random number is y "<< y << endl;
int x = (rand() % (maxValue - minValue + 1)) + minValue;
// cout<< " the random number is x "<< x << endl;
cout << "What is " << x << " + " << y << " =" << endl;
int answer;
cin >> answer;
if (answer == (x + y)) {
cout << "Great!! You are really smart!!" << endl;
++quantity_correct_answers;
++quantity_total_questions;
}
else {
cout << "Oh Sorry Try Again." << endl;
++quantity_wrong_answers;
++quantity_total_questions;
}
cout << "Right: " << quantity_correct_answers;
cout << " Wrong: " << quantity_wrong_answers;
cout << " Total Questions: " << quantity_total_questions << endl;
cout << "Do you want to play again? [enter y for yes or n for no]" << endl;
cin >> keepgoing;
} while (keepgoing == "y");
}
void products()
{
{
string keepgoing;
unsigned int quantity_total_questions = 0U;
unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;
do {
const int minValueOne = 0;
const int maxValueOne = 9;
const int minValueTwo = 10;
const int maxValueTwo = 99;
int y = (rand() % (maxValueOne - minValueOne + 1)) + minValueOne;
// cout<< " the random number is y "<< y << endl;
int x = (rand() % (maxValueTwo - minValueTwo + 1)) + minValueTwo;
// cout<< " the random number is x "<< x << endl;
cout << " What is " << x << " x " << y << " =" << endl;
int answer;
cin >> answer;
if (answer == (x * y)) {
cout << "Great!! You are really smart!!" << endl;
++quantity_correct_answers;
++quantity_total_questions;
}
else {
cout << "Oh Sorry Try Again." << endl;
++quantity_wrong_answers;
++quantity_total_questions;
}
cout << "Right: " << quantity_correct_answers;
cout << " Wrong: " << quantity_wrong_answers;
cout << " Total Questions: " << quantity_total_questions << endl;
cout << "Do you want to play again? [enter y for yes or n for no]" << endl;
cin >> keepgoing;
} while (keepgoing == "y");
}
}
I would create a structure that contains the number of total answers and number of correct answers—the incorrect ones can be inferred—and then pass a reference to an instance of the structure to the respective sums() and products() functions.
Those functions can then populate the structure elements and when they return, your main function can read them out, knowing exactly how many questions were asked, how many were answered, or whatever other information you want to record and retrieve.

Issues with Id exit Status

This is my very first time posting of many, and I am in desperate need of help. I am attempting to put together a program to run Craps for school, and I am running into the error listed in the title. I have looked around here and messaged my professor, done everything he has asked, and yet still there is no fixing it. Rather than pester him again, could someone please help me figure out what to do next?
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int win1=7;
const int win2 =11;
const int crap1=2;
const int crap2=3;
const int crap3=12;
//prototypes for 5 functions are below, you need to code the bodies for these functions down below the main routine
int rollTheDice();
bool flag = false ;
bool winner( int );
bool craps( int );
void rollMore( int, double );
double getWager();
int diceRoll,dice1,dice2,roll,point;
double wager;
int main()
{
srand(34); //the final run will have 34, seed of 6 rolls a 7 -automatic winner, seed of 36 rolls 2-loser
cout << fixed << setprecision(2); //set output format
wager = getWager(); // call get wager and get back the amount the player wagered
cout<< "The amount wagered is "<<wager<<endl<<endl; //i added this cout to verify what wager is returned from getWager()
diceRoll = rollTheDice();
if( winner(diceRoll) ==true )
{
cout << "\nCongratulations! You have won " << wager << " $ \n";
}
else if( craps(diceRoll)==true) //call function craps and test if true is returned
{
cout << "\nSorry! You have lost " << wager << " $ \n";//print loser message and amount loss
}
else
{
rollMore( diceRoll, wager );
}
return 0;
//Get the user's wager amount with a cin and verify it before returning the value to main
double getWager()
{
cout << "Please enter your initial bet, in whole dollar amounts. The starting bet is 5.00$. \n" ;
cin >> wager ;
while (wager < 5)
{
cout << "I am sorry, the number you have entered is an insufficient bet. Please Try Again \n" ;
cin >> wager;
}
return wager;
}
int rollTheDice() //method is called and returns the roll of two dice, value between2-12
{ //you need two dice variables and generate a random number between 1-6 for each dice
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
diceRoll = dice1 + dice2;
roll=diceRoll;
cout << "You have rolled a " << diceRoll <<" \n";
return roll;
}
bool winner( int roll ) //returns true if roll is 7 or 11,else return false
{
if (roll==win1 or roll==win2)
{
return true;
}
else return false;
}
bool craps( int roll ) //returns true if player craps out which is a roll is 2,3,12,
if (roll==crap1 or roll==crap2 or roll==crap3)
return true ;
else return false ;
}
void rollMore( int point, int diceRoll, double wager, bool flag )
{
point=diceRoll;
cout << "You did not win or lose! We are now rolling to match the point by rolling " << point << "\n";
while (flag=false)
{
diceRoll = rollTheDice();
if (diceRoll==7)
cout << "\nSorry! You have lost " << wager << " $ \n";
flag == true ;
if (diceRoll=point)
cout << "\nCongratulations! You have won " << wager << " $ \n";
flag==true;
}
}

How would I calcuate the total of all the items entered to then calculate the total price?

Hi there apologise if my question is poorly worded, I'm struggling to find a solution to my problem.
The purpose of my program is to allow the user to enter predefined bar codes that associate with items and a price. The user enters as many barcodes as they want, and when they're done they can exit the loop by pressing "F" and then total price for all the items is displayed.
This is my code so far, I'm very new to programming..
#include <iostream>
#include <iomanip>
using namespace std;
int index_of(int arr[], int item, int n) {
int i = 0;
while (i < n) {
if(arr[i] == item) {
return i;
}
i++;
}
return -1;
}
const int SIZE = 10;
int main()
{
string item [SIZE] = {"Milk", "Bread", "Chocolate", "Towel", "Toothpaste", "Soap", "Pen", "Biscuits", "Lamp", "Battery"};
int barcode [SIZE] = {120001, 120002, 120003, 120004, 120005, 120006, 120007, 120008, 120009, 120010};
float price [SIZE] = {10.50, 5.50, 8.00, 12.10, 6.75, 5.20, 2.00, 4.45, 20.50, 10.00};
cout << "*************************************************************" << endl;
cout << "WELCOME TO THE CHECKOUT SYSTEM" << endl;
cout << "Please scan a barcode or manually enter the barcode ID number" << endl;
cout << "*************************************************************\n" << endl;
int newBarcode;
while (true){
cout << "Please enter a barcode (Type 'F' to finish): ", cin >> newBarcode;
int index = index_of(barcode, newBarcode, (sizeof(barcode) / sizeof(barcode)[0]));
cout << "\n>> Name of item: " << item[index] << endl;
cout << ">> Price of item: \x9C" << setprecision (4)<< price[index] << endl;
cout << ">> " <<item[index] << " has been added to your basket. \n" << endl;
float total = 0 + price[index];
cout << ">> Your current basket total is: \x9C" << setprecision(4) << total << endl;
/*float total = 0;
float newtotal = 0;
price[index] = total;
total = newtotal;
cout << ">> " << "Basket total: " << newtotal << endl; */
}
return 0;
}
You will need to iterate over all items and add their value to a variable. You can do it the old way:
float sum = 0;
for(int i = 0; i < SIZE; i++) {
sum += price [i];
}
Or the C++11 way:
float sum = 0;
for(float p : price) {
sum += p;
}
However I must point out a few important issues with your code:
Your array has a fixed size but user can enter as many entries as he wants. To avoid this issue, use vector. It behaves like array but has dynamic size. Simply use push_back() to add a new element.
Don't use separate containers (arrays) for the same group of objects. It's a bad coding practice. You can define a structure for product which will contain name, barcode and price, then make one container for all of the products.
Edit
I'm sorry, I misunderstood your problem. There are many ways to solve this, the most elegant way is to create a map where key is the bar code and value is your product object or just a price.
map<int, float> priceMap;
priceMap.insert(pair<int, float>([your bar code here], [your price here]))
Afterwards just create a vector of bar codes, fill it with user data and iterate over it sum all prices:
float sum = 0;
for(int b : userBarcodes) {
sum += priceMap.at(b);
}
You are trying to read from cin into an int. As you decide to put a stopping condition on 'F' input you must read into a string. Then decide what to do with the value. You will need to check if the input is an int or not. You can do it as given here or here.
Or you may change the stopping condition to a less likely integer like -1. And make sure you always read an int into newBarcode.
There are various small errors which are hard to list out. I have changed them in the code below which is implementing point 2 (You have to add the stopping condition).
One of the error or wrong practice is to declare new variables inside a loop. In most cases you can declare the variables outside and change there values in the loop.
I replaced (sizeof(barcode) / sizeof(barcode)[0] with SIZE as the lists are predefined and unchanging. Anyways you should use (sizeof(barcode) / sizeof(barcode[0]) for length calculation.
#include <iostream>
#include <iomanip>
using namespace std;
int index_of(int arr[], int item, int n) {
int i = 0;
while (i < n) {
if(arr[i] == item) {
return i;
}
i++;
}
return -1;
}
const int SIZE = 10;
int main()
{
string item [SIZE] = {"Milk", "Bread", "Chocolate", "Towel", "Toothpaste", "Soap", "Pen", "Biscuits", "Lamp", "Battery"};
int barcode [SIZE] = {120001, 120002, 120003, 120004, 120005, 120006, 120007, 120008, 120009, 120010};
float price [SIZE] = {10.50, 5.50, 8.00, 12.10, 6.75, 5.20, 2.00, 4.45, 20.50, 10.00};
cout << "*************************************************************" << endl;
cout << "WELCOME TO THE CHECKOUT SYSTEM" << endl;
cout << "Please scan a barcode or manually enter the barcode ID number" << endl;
cout << "*************************************************************\n" << endl;
int newBarcode;
float total = 0;
int index;
while (true){
cout << "Please enter a barcode (Type -1 to finish): \n";
cin >> newBarcode;
while(cin.fail()) {
cout << "Not an integer" << endl;
cin.clear();
cin.ignore(100,'\n');
cin >> newBarcode;
}
index = index_of(barcode, newBarcode, SIZE);
cout << index;
if (index == -1) {
cout << "Apologies here for unsupported barcode\n";
continue;
} else {
cout << ">> Name of item: " << item[index] << endl;
cout << ">> Price of item: " << price[index] << "\n";
cout << ">> " <<item[index] << " has been added to your basket. \n";
total = total + price[index];
cout << ">> Your current basket total is: " << total << "\n";
}
}
return 0;
}
Your question could be more helpful to others if you find out what is wrong with your implementation and ask implementation specific questions which will probably be already answered. Asking what is wrong with my code is not quite specific.

Why is my code not outputting (just) numbers?

exercise prompt for code: Write a program that tells what coins to give for any amount of change from 1 cent to 99 cents. Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies). Do not use nickel and half-dollar coins. Your program will use the following function (among others):
void compute_coins(int coin_value, int& num, int& amount_left);
#include <iostream>
#include <string>
using namespace std;
void prompt(int *amount_left);
void remaining_change(int *amount_left, int coin_value);
void compute_coins(int coin_value, int *num, int *amount_left);
void output(string coin_name, int *num);
int main() {
int change = 0, num = 0, amount_left = 0;
const int quarter = 25, dime = 10, penny = 1;
string q = "quarter(s)", d = "dime(s)", p = "penny(s)";
prompt(&change);
compute_coins(quarter, &num, &amount_left);
remaining_change(&amount_left, quarter);
output(q, &num);
compute_coins(dime, &num, &amount_left);
remaining_change(&amount_left, dime);
output(d, &num);
compute_coins(penny, &num, &amount_left);
output(p, &num);
}
void prompt(int *change)
{
cout << "How much change is there? ";
cin >> *change;
cout << "You entered " << change << endl;
cout << "That is equal to: ";
}
void remaining_change(int *amount_left, int coin_value)
{
*amount_left = (*amount_left % coin_value);
}
void compute_coins(int coin_value, int *num, int *amount_left)
{
*num = *amount_left / coin_value;
}
void output(string coin_name,int *num)
{
cout << num << " " << coin_name << ", ";
}
You are outputting the value of the pointers, not the value of the object pointed to.
The simple fix is to dereference the pointers first:
cout << "You entered " << *change << endl;
// ^
cout << *num << " " << coin_name << ", ";
// ^
However, I'd suggest not using pointers for things like this at all. For built-in types you should take a reference when you want to update the variable and a value otherwise.
Personally I wouldn't update those variables from inside the functions either, I'd carry out the necessary input or calculation and return a value to be assigned.
In prompt(), change is a pointer, so in order to output the value that change points to you would need to modify this line:
cout << "You entered " << change << endl;
to:
cout << "You entered " << *change << endl;
Better still, though, you could use a reference rather than a pointer:
void prompt(int &change)
{
cout << "How much change is there? ";
cin >> change;
cout << "You entered " << change << endl;
cout << "That is equal to: ";
}
and then you would just call this as:
prompt(change);
This is much more idiomatic C++ – the pointer method is more "old skool" C-style programming.
Ditto for the other places where you are printing the pointer itself, e.g. num.

Adding a time delay to simulate lottery numbers being drawn

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;