我正在學習C++,所以請耐心等待,並且事前爲任何白癡道歉。匹配使用getline()無限運行的單詞C++程序?
我試圖寫一些代碼,將每行上的第一個字匹配到「num_lines」,「num_words」或「num_chars」文件中,名爲「command.txt」。
如果第一行的第一個單詞與先前提到的單詞不匹配,它將讀取下一行。 一旦它遇到匹配的單詞(僅第一個單詞!)它打印出匹配的單詞。
這裏是我的所有代碼:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream comm_in("commands.txt"); // opens file
string command_name = "hi"; // stores command from file
bool is_command() {
if (command_name == "num_words" || command_name == "num_chars" || command_name == "num_lines") {
return true;
} else {
return false;
}
}
// FIND a first word of a line in file THAT MATCHES "num_words", "num_chars" or "num_lines"
void get_command() {
string line;
char c;
while (!is_command()) { // if command_name does not match a command
// GET NEXT LINE OF FILE TO STRING
getline(comm_in, line);
// SUPPOSED TO GET THE FIRST WORD OF A STRING (CANT USE SSTREAM)
for (int i = 0; i < line.size(); i++) { // increment through line
c = line[i]; // assign c as index value of line
if (c == ' ' || c == '\t') { // if c is a space/tab
break; // end for loop
} else {
command_name += c; // concatenate c to command_name
} // if
} // for
} // while
return;
}
int main() {
get_command();
cout << command_name; // supposed to print "num_lines"
}
的command.txt文件的內容:
my bear is happy
and that it
great ha
num_lines sigh
編譯正確,但是當我在終端運行它,什麼也不顯示;它似乎永遠不會停止加載。 我該如何解決這個問題?
看看你的'while'循環做。什麼會導致ti停止? – Jay
while循環在is_command返回true時停止,即當command_name ==「num_lines」時,它一旦讀取到command.txt文件的第4行就必須停止?這個邏輯有什麼問題嗎? – Salvatross
這是怎麼回事?一旦退出循環,它應該打印並退出程序。 – Jay