我正在创建一个简单的函数 printNode,它接受一个 int 或字符串“all”,并打印相应的输出。该函数的 int 方面运行良好,但是当我调用
printNode all
该函数打印“错误:无效参数”,我似乎无法弄清楚原因。
int main() {
...
getline(cin, line)
stringstream lineStream(line);
stringstream elineStream(line);
printNode(lineStream, elineStream);
...
}
int printNode(stringstream& lineStream, stringstream& elineStream) {
//String aspect of printNode
int nodeid;
string name;
// Parsing nodeid or name
lineStream >> nodeid;
// If parsing to nodeid fails, check if input is string "all"
if (lineStream.fail()) {
elineStream >> name;
if (elineStream.fail()) {
if (elineStream.eof()) {
cout << "Error: too few arguments" << endl;
return 0;
}
else {
cout << "Error: invalid argument" << endl;
return 0;
}
}
if (name != "all") {
cout << "Error: invalid argument" << endl;
return 0;
}
// Check for too many arguments
elineStream >> ws;
if (!elineStream.eof()) {
cout << "Error: too many arguments" << endl;
return 0;
}
// Print output for input "all"
cout << "Print: all nodes" << endl;
return 1;
}
任何帮助将非常感激。