2017-01-23 2 views
-1

다음 코드를 C++로 작성했습니다. 이것은 임의의 문자 및/또는 숫자 시퀀스를 생성하고 사용자에게 한 번에 하나씩 표시 한 다음 사용자에게 재생산을 요청하는 프로그램입니다 그것은 문자 시퀀스를 저장하는 큐를 사용합니다. 배열 요소를 한 번에 하나씩 인쇄하도록 코드를 수정하고 싶습니다. 제발 도와주세요.배열의 요소를 한 번에 하나씩 인쇄하는 방법은 무엇입니까?

#include <iostream> 
#include<cstdlib> 
#include <queue> 
using namespace std; 
int main() 
{ 

std::queue<int> myqueue; 
int array[6]; 
for (int i = 0; i < 6; i++) 
{ 

    array[i]=rand()%100; 

    printf("\n%d", array[i]); 

    if(i==5) 
    { 
     printf("\n Reproduce the sequence again"); 
    } 

} 
+0

각 요소가 표시된 후에 화면을 지우겠습니까? – George

+0

휴대 성을 원하는 경우 [ncurses] (https://en.wikipedia.org/wiki/Ncurses) – NathanOliver

+0

* 표준 * C++ 언어는 화면을위한 기능이 없으므로 화면을 지우는 것이 플랫폼 문제입니다 (모든 플랫폼이 필요하지는 않음). 화면을 가지고). –

답변

0

나는 당신이 무엇을 기대하는지 (나는 메모리 게임이라고 생각한다)는 모르지만, 이것에 대해 살펴 보자. 나는 잠들기를 사용 중입니다. Windows 용으로 상응하는 것이 있다고 확신합니다.

#include <unistd.h> // Required for usleep 
#include <iostream> // cin, cout, flush 
#include <stdlib.h> // rand 

int main() { 

    int array[6]; // Note : This will only give you an array of digits, not letters 

    for (int i = 0; i < 6; i++) 
    { 

     array[i]=rand()%100; 

     // "\r" is a carriage return, it moves the writing to the beginning of the line 
     std::cout << "\r" << array[i] << std::flush; 

     // sleeps 1 second 
     usleep(1000000); 
    } 


    printf("\r Reproduce the sequence again (use space to separate, enter to validate"); 
    std::string userAnswer; 
    std::cin >> userAnswer; 

    // parse userAnswer 

} 
+1

C++ 11 이상에서는 ['std :: this_thread :: sleep_for'] (http://en.cppreference.com/w/cpp/thread/sleep_for)를 사용할 수 있습니다. – NathanOliver

+0

C + +11을 사용할 수는 있지만 실제로는 훌륭한 선택이 될 것입니다. – fzd

+0

고정 폭으로 숫자를 써야합니다. '100'을 쓰고 '2'를 입력하면 '200'이 표시됩니다. – Jarod42

관련 문제