1

ここのフォーラムは初めてで、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()
4

1 に答える 1

0

cout は通常バッファリングされ、改行によってバッファが画面にフラッシュされます。同じ行に数字を表示すると、組み込みの遅延にもかかわらず、すべてが一度に表示されることを説明できます。

cout.flush();バッファリング遅延なしで強制的に出力を行うために使用します。マニピュレータ フォームを使用することもできます。cout << number << " " << flush;

于 2014-09-17T23:18:29.317 に答える