首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >\r和\n或缓冲区冲洗的问题

\r和\n或缓冲区冲洗的问题
EN

Stack Overflow用户
提问于 2015-01-05 03:17:20
回答 1查看 172关注 0票数 1

我在多个地方读到\n在我的代码中使用时不会刷新缓冲区,我将在这个问题的末尾添加它,它似乎就是这样做的,或者至少从输出中看起来是这样的(可能由于我执行couts的方式,背景中还发生了其他事情?)。

预期产出:

激光1周期将在x秒内完成. 激光2周期将在x秒内完成. 激光3周期将在x秒内完成. 激光4周期将在x秒内完成.

我在CLI里得到的是:

激光1周期将在x秒内完成. 激光2周期将在x秒内完成. 激光3周期将在x秒内完成. 激光4周期将在x秒内完成. 激光1周期将在x秒内完成. 激光2周期将在x秒内完成. 激光3周期将在x秒内完成. 激光4周期将在x秒内完成. 激光1周期将在x秒内完成. 激光2周期将在x秒内完成. 激光3周期将在x秒内完成. 激光4周期将在x秒内完成.

我希望输出保持不变,就像预期输出示例中的输出一样,只需在代码执行的每一个时间循环中更新自己。

这是我的代码:

代码语言:javascript
复制
#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <random>
#include <thread>
#include <future>

using namespace std; //Tacky, but good enough fo a poc D:

class mLaser
{
public:
    mLaser(int clen, float mamt, int time_left)
    {
        mlCLen = clen;
        mlMAmt = mamt;
        mCTime_left = time_left;
    }

    int getCLen()
    {
        return mlCLen;
    }

    float getMAmt()
    {
        return mlMAmt;
    }

    void setMCOld(int old)
    {
        mCTime_old = old;
    }

    void mCycle()
    {
        int mCTime_new = GetTickCount(); //Get current tick count for comparison to mCOld_time

        if (mCTime_old != ((mCTime_new + 500) / 1000)) //Do calculations to see if time has passed since mCTime_old was set
        {
            //If it has then update mCTime_old and remove one second from mCTime_left.
            mCTime_old = ((mCTime_new + 500) / 1000);
            mCTime_left -= 1000;
        }

        cur_time = mCTime_left; 
    }

    int getCTime()
    {
        return cur_time;
    }

    int getCTLeft()
    {
        return mCTime_left;
    }


private:
    int mlCLen; //Time of a complete mining cycle
    float mlMAmt; //Amoung of ore produced by one mining cycle (not used yet)
    int cur_time; //The current time remaining in the current mining cycle; will be removing this as it is just a copy of mCTime_left that I was going to use for another possiblity to make this code work
    int mCTime_left; //The current time remaining in the current mining cycle
    int mCTime_old; //The last time that mCycle was called
};

void sMCycle(mLaser& ml, int i1, thread& _thread); //Start a mining cycle thread

//Some global defines
random_device rd;
mt19937 gen(rd());

uniform_int_distribution<> laser(1, 3); //A random range for the number of mlaser entities to use
uniform_int_distribution<> cLRand(30, 90); //A random time range in seconds of mining cycle lengths
uniform_real_distribution<float> mARand(34.0f, 154.3f); //A random float range of the amount of ore produced by one mining cycle (not used yet)

int main()
{
    //Init some variables for later use
    vector<mLaser> mlasers; //Vector to hold mlaser objects
    vector<thread> mthreads; //Vector to hold threads
    vector<shared_future<int>> futr; //Vector to hold shared_futures (not used yet, might not be used if I can get the code working like this)

    int lasers; //Number of lasers to create
    int cycle_time; //Mining cycle time
    int active_miners = 0; //Number of active mining cycle threads (one for each laser)
    float mining_amount; //Amount of ore produced by one mining cycle (not used yet)

    lasers = laser(gen); //Get a random number
    active_miners = lasers; //Set this to that random number for the while loop later on

    //Create the mlaser objects and push them into the mlasers vector
    for (int i = 0; i < lasers; i++)
    {
        int clength = cLRand(gen);

        mlasers.push_back(mLaser(clength, mARand(gen), (clength * 1000)));
        
        //Also push thread obects into mthreads for each laser object
        mthreads.push_back(thread());
    }

    //Setup data for mining cycles
    for (int i = 0; i < mlasers.size(); i++)
    {
        int mCTime_start = GetTickCount(); //Get cycle start time
        mlasers.at(i).setMCOld(((mCTime_start + 500) / 1000));
    }

    //Print initial display for mining cycles
    for (int i = 0; i < mlasers.size(); i++)
    {
        cout << "Mining Laser " << i+1 << " cycle will complete in " << (mlasers.at(i).getCTLeft() + 500) / 1000 << " seconds...\n";
    }

    while (active_miners > 0)
    {   
        for (int i = 0; i < mlasers.size(); i++)
        {
            //futr.push_back(async(launch::async, [mlasers, i, &mthreads]{return sMCycle(mlasers.at(i), i + 1, mthreads.at(i)); }));
            async(launch::async, [&mlasers, i, &mthreads]{return sMCycle(mlasers.at(i), i + 1, mthreads.at(i)); }); //Launch a thread for the current mlaser object
            //mthreads.at(i) = thread(bind(&mLaser::mCycle, ref(mlasers.at(i)), mlasers.at(i).getCLen(), mlasers.at(i).getMAmt()));
        }

        //Output information from loops
        cout << " \r" << flush; //Return cursor to start of line and flush the buffer for the next info

        for (int i = 0; i < mlasers.size(); i++)
        {
            if ((mlasers.at(i).getCTLeft() != 0) //If mining cycle is not completed
            {
                cout << "Mining Laser " << i + 1 << " cycle will complete in " << (mlasers.at(i).getCTLeft() + 500) / 1000 << " seconds...\n";
            }

            else //If it is completed
            {
                cout << "Mining Laser " << i + 1 << " has completed its mining cycle!\n";
                active_miners -= 1;
            }
        }
    }

    
    /*for (int i = 0; i < mthreads.size(); i++)
    {
        mthreads.at(i).join();
    }*/


    //string temp = futr.get();
    //float out = strtof(temp.c_str(),NULL);

    //cout << out << endl;

    system("Pause");
    return 0;
}

void sMCycle(mLaser& ml, int i1,thread& _thread)
{
    //Start thread
    _thread = thread(bind(&mLaser::mCycle, ref(ml)));
    
    //Join the thread
    _thread.join();
}

根据本·沃格特的说法,\r似乎不能以我试图使用的方式使用它。除了Matthew关于单击每个更新的命令窗口的建议之外,还有谁有其他建议吗?也许是一些正在增强的东西,还是c++11的一些新东西?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-05 04:43:22

每次执行后,您可以尝试使用系统(“cls”)之类的方法清除控制台;下面是到post的链接(我如何清除console](https://stackoverflow.com/questions/6486289/how-can-i-clear-console) )

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27772964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档