我有一個關於一個函數的問題,它需要一個字符串(命令,名字和姓氏),並根據輸入的內容執行。我的函數還有其他幾個if語句,如果輸入被認爲是無效的,我該如何讓用戶輸入另一個命令?謝謝調用自己的C++輸入函數
EXAMPLE_INPUT = "CREATE John Doe"
std::string get_input(std::string& s)
{
std::string raw_input;
std::getline(std::cin, raw_input);
std::istringstream input(raw_input);
std::string command;
std::string first_name;
std::string last_name;
input >> command;
input >> first_name;
input >> last_name;
//what do I return? I can't return all 3 (command, first, last)
}
void std::string input_function(std::string& s)
{
if (s == "ONE")
{
call function_one()
}
else if (s == "TWO")
{
call function_two()
}
else
{
//How do I get user to type in something else(call get_input() again)?
}
}
由於您只是解析了它並將其拆分爲三個不同的字符串,所以返回'std :: string'沒有任何意義......也許您需要一個結構體。 – 2014-11-25 00:49:56
對於第一個結構,struct可能是最好的選擇,也許返回一個表示命令執行成功的「bool」會適用於第二個。 – IllusiveBrian 2014-11-25 00:51:31
這樣的結構與這3個元素,每個都是一個字符串。所以當我的第二個函數接受一個字符串時,我給它struct.element? – Steven 2014-11-25 00:52:12