簡體   English   中英

當用戶這樣說時停止 while / for 循環

[英]stopping a while / for loop when an user says so

我是 c++ 的新手,所以我只知道 iostream 和語言的語法。

我正在編寫一個程序,該程序使用 fstream 創建無限數量的 .txt 文件,但我有點卡住了,我希望它在程序中有一個菜單,以便用戶可以在我將編碼的命令的幫助下干擾該過程在無效函數中(“暫停”、“停止”等...)

這意味着創建 .txt 文件的過程應該不斷運行,但用戶也可以在 cin>> 中寫入上面顯示的關鍵字以干擾該過程

我計划編寫多個命令,所以將來肯定會使用 switch 語句,但為了向您展示我的問題,我只會在一個命令中使用 while 循環所以我嘗試過這樣的事情:

void stop()
{
return 0;
}

int main()
{
string command= "";
int i=1;

if (command!="stop")
{
     while (i<=2)
     {
     CreateNewFile();
     cin >> command;
     }

} else

{
stop();
}
}

但是這種循環的問題在於它要求用戶每次都寫一些東西來重置循環,而我特別想避免這種事情......我想保持循環運行只要我的用戶希望它

我確信答案非常簡單,但我沒有通過詢問谷歌找到任何幫助,所以我會問你們。

提前感謝那些願意花時間回答我的人

所以過了一會兒,這基本上是最簡單的解決方案:

#include <iostream>
#include <thread>
#include <string>
static bool finished = false;
void createFile() {
    while (!finished) {
        //Your code
    }
}

int main() {
    std::string answer;
    std::thread question(createFile);
    std::cin >> answer;
    if (answer == "stop") {
        finished = true;
    }
    question.join();
    return 0;
}

在這里,我們創建一個線程,它將一直運行直到他們鍵入 stop。 我運行了代碼並且它正在工作我讓它打印出 1 直到我寫停止。

我的建議,亞當,是改變你的問題描述,所以你問用戶他們想要創建多少個文件,然后多次運行循環:

for(;;) {
  unsigned n;
  cout << "How many files do you want to create (0 to stop)? ";
  cin >> n;
  if(!n) break;
  for(unsigned i = 0; i < n; i++) {
    CreateNewFile();
  }
}

我在這條線上有一些東西,其中正在執行實際工作的線程繼續進行,並且僅在用戶願意時才處理命令。

#include <iostream>
#include <string>
#include <thread>
#include <stack>
#include <chrono>
#include <mutex>

int main() {
    std::stack<std::string> commandQueue;
    std::mutex m;
    int i = 0;
    std::thread worker([&]() {
        bool suspend = false;
        while(true) {
            {
                const std::lock_guard<std::mutex> lock(m);
                if(!commandQueue.empty()){
                    std::string command = commandQueue.top();
                    commandQueue.pop();
                    if(command == "continue"){
                        suspend = false;
                        //process continue
                    } else if (command == "pause"){
                        suspend = true;
                        //process other commands....
                    } else if(command == "stop") {
                        //process stop
                        return; //exit thread
                    } 
                }
            }
            //Commands processed, continue as usual
            if(!suspend) {//Process only if not paused
                using namespace std::chrono_literals;
                //Continue creating files or whatever it is that needs to be done continuously
                std::this_thread::sleep_for(500ms);
                i++;
                //std::cout<<"Value of i is "<<i<<'\n';
                //CreatFileName();
            }
        }
    });
    std::string command;
    do {
        std::cout<<"Enter commands to interact\n";
        std::cin>>command;
        {
            const std::lock_guard<std::mutex> lock(m);
            commandQueue.push(command);
        }
    }while(command != "stop");
    worker.join();
    std::cout<<"After execution, value of i is "<<i<<'\n';
}

當然,這個特定示例的 output 有點難看,因為它從 2 個不同的線程打印到控制台,但是像stoppausecontinue這樣的命令處理得很好,因為只有一個線程( main線程)讀取命令,但由於這只是為了演示如何實現您的意圖,因此我沒有在output美學上花費太多時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM