我目前正在尝试为一个学习项目做终端应用程序,这是我的第一个真正的c ++程序。我知道代码的基础知识,但是我不明白这是怎么工作的。
因此,我的程序应采用输入字符串,将其解析为单词数组,并按照程序底部所示检查这些单词。
这是我目前所拥有的,该程序没有经过100%调试,因为我在手机上重写了该程序,没有机会对其进行编译...:
#include <iostream>
bool isRunning;
char* input;
int main() {
isRunning = true;
while (isRunning) {
// Get input
std::cout << ">";
std::cin >> input;
// Separate by space
char* words[];
int ctn = 0;
char* word = "";
for (auto c : input) {
if (c == ' ') {
words[ctn] = word;
ctn++;
word = "";
} else {
word = word + x;
}
}
words[ctn] = word;
ctn++;
// Log the words
for (auto word : words) {
std::cout << word << std::endl;
}
// - Command checks
// Exit command
if (words[0] == "exit") {
isRunning = false;
}
// Say command
if (words[0] == "say") {
if (words[1] != null) {
std::cout << "[Say]:" << words[1] << std::endl;
} else {
std::cout << "Error: Argument missing!" << std::endl;
}
}
}
system("pause");
return 0;
}
您使用std::stringstream
最容易地用空格分隔输入。您使用std::stringstream
填充数组,然后使用std::vector
解析最简单的单词。
类似这样的东西:
std::vector
上面的文档链接将帮助您确定需要哪个#include才能起作用。