添加时间延迟来模拟正在绘制的彩票号码

时间:2014-09-17 22:35:57

标签: c++ timedelay

这里的论坛新手并开始学习C ++。这个网站已经用语法和其他东西帮助了我很多。我正在尝试使用我的代码将数字打印到屏幕,有时间延迟,然后打印下一个数字。目前时间延迟有效,但它会打印所有生成的13个数字。对我做错了什么想法?

这是我的代码:

#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()

1 个答案:

答案 0 :(得分:0)

cout通常是缓冲的,换行符会将缓冲区刷新到屏幕上。当您在同一行显示数字时,这可以解释尽管您已经构建了延迟,但所有内容都显示在onece上。

使用cout.flush();强制完成输出而不缓冲延迟。您也可以使用manipulator formcout << number << " " << flush;