我目前正在編寫一個命令行「解析器」,可以這麼說,到目前爲止它一直在工作,直到我嘗試了一些方法來添加選項/參數。字符數組的奇數輸出
void parser::nextCom() {
cout << parser::prompt; // parser::prompt = "> "
string com;
getline(cin, com);
char the_command[5]; // i want this to store the command e.g. "go" given that go is a command
for (int i = 0; i < com.size(); i++) {
if (com[i] == ' ') break;
else the_command[i] = com[i];
}
cout << the_command << endl;
}
命令被複制,但一些非常不想要的字符顯示當在打印the_command
到控制檯。
這是我所得到的,如果我通過「北上」的命令:
goÌÌÌÌÌÌÌÌÌÌÌÌÌÌØNi
我也不太清楚關於C++字符數組,但我不知道我怎麼得到這個輸出。任何幫助都將不勝感激。有關代碼的任何問題,或者如果您需要更多的代碼,只需評論,預先感謝
我想讓命令和使用if語句或switch語句來確定要運行哪個函數 – PurityLake 2013-03-21 20:22:50
@PurityLake什麼阻止你?看我的編輯。 – 2013-03-21 20:24:57
好吧,你有一個點,我會遇到與字符數組的麻煩,只是不知道如何使用字符串分離命令,是不是字符串流爲我做? – PurityLake 2013-03-21 20:27:13