如何一次打印一个数组元素?

时间:2017-01-23 15:23:55

标签: c++ arrays queue

我用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");
    }

}

1 个答案:

答案 0 :(得分:0)

我不完全确定你的期望(我想是一个记忆游戏),但让我们来看看这个。我正在使用usleep,我确定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

}