从用户输入中提取整数的枚举 Switch 语句
Posted
技术标签:
【中文标题】从用户输入中提取整数的枚举 Switch 语句【英文标题】:Enumarated Switch Statement With Extracting an Integer From User Input 【发布时间】:2019-01-25 00:58:38 【问题描述】:我有一个具有以下要求的 switch 语句菜单:
“GetNewMatrix”作为枚举选项,为 0 到 9 之间的数字。
用户输入必须全部在一行。
必须使用枚举。
我需要用户能够输入“GetNewMatrix 5”之类的内容,并让 switch 语句看到 GetNewMatrix 以启动该案例,并将 5 传递到该案例中以初始化 matrix[5].MatrixType( )
我完全不知道如何实现这一点。 我目前有以下(相当粗略的)代码,但这并不能帮助我从他们的输入中提取用户整数,因为它需要如上所述在所有一行中完成。
matrix 是一个大小为 [10] 的数组,属于 MatrixType 类,包含 int 值 [MAX_ROWS][MAX_COLS]、int numRows 和 int numCols
input 是一个字符串,用于获取用户输入并将其与枚举案例进行比较以决定继续使用哪种案例
name 是一个介于 0 和 9 之间的整数,用于标记 [10] 数组中的 Matrix
r是一个整数,用来保存用户指定的行数
c是一个整数,用来保存用户指定的列数
enum Choice Start, GetNewMatrix, AddMatrices, SubMatrices, MultiplyMatrices, PrintMatrix, Quit;
Choice ch = Start;
while(ch != Quit)
cout << "=======================================================" << endl;
cout << "GetNewMatrix # (Create a new Matrix)" << endl;
cout << "AddMatrices # # # (Adds two matrices together)" << endl;
cout << "SubMatrices # # # (Subtracts a second matrix from the first)" << endl;
cout << "MultiplyMatrices # # # (Multiplies two matrices together)" << endl;
cout << "PrintMatrix # (Print out a matrix)" << endl;
cout << "Quit (Quit the program)" << endl;
cout << "=======================================================" << endl << endl;
cout << "Please enter your choice here: ";
cin >> input; //Unable to assign ch to a string
if (input == "GetNewMatrix")
ch = GetNewMatrix;
else
if (input == "Quit")
ch = Quit;
else
cout << "Unknown entry. Please use exact capitalization." << endl;
switch(ch)
case GetNewMatrix: //Placeholder until integer extraction is figured out
cout << "Please enter a value (between 0 and 9) to name the matrix: ";
cin >> name;
matrix[name].MatrixType();
cout << "Matrix " << name << " created." << endl;
cout << "Please enter values (between 1 and 10) for row and column size: ";
cin >> r >> c;
matrix[name].SetSize(r - 1,c - 1);
cout << "Matrix size set to " << r << " x " << c << endl;
break;
【问题讨论】:
请包括input
、matrix
、name
、r
和c
的定义?
用户输入必须全部在一行上。 有效地表示您不能很容易地使用cin >> input;
,因为>>
将行尾丢弃为空格。将std::getline
与std::istringstream
一起使用。 See this answer for inspiration.
请同时包含input
的定义。是定义为std::string
还是别的什么?
不相关:与其解释代码中缺少的内容,不如添加缺少的代码。
不相关:如果您不知道如何处理像阅读输入这样重要的事情,那么编写正确的代码真的很难。与其屏蔽代码并试图击败它直到它起作用,不如做一些一次性的小实验,你可以用它来弄清楚如何阅读和处理一行代码。然后围绕这些实验的结果编写程序。很有可能,当你得到你正在工作的东西时,你会把所有东西都扔掉两到三遍,或者花费不成比例的时间来试图保存错误的逻辑。从一些有用的小东西开始。从那里构建。
【参考方案1】:
正如你现在所见的枚举背后的工作就像一个从 0 开始的数组索引等等,因此使用 char 使你的 char ch;像这样 int ch 的变量到 int; 然后您可以在 int 中输入供选择,并根据您的案例将运行的输入将其作为 0 到 9 放入 switch。
【讨论】:
【参考方案2】:使用 User4581301 的信息(感谢您的其他帖子以及添加的信息)我得到了以下为我工作:
int main()
string input;
int count = 0;
int i;
int j;
int item;
int num1;
int num2;
int num3;
string case_value;
int r; //used for row size
int c; //used for column size
MatrixType matrix[10];
enum Choice Start, GetNewMatrix, AddMatrices, SubMatrices, MultiplyMatrices, PrintMatrix, Quit;
Choice ch = Start;
while(ch != Quit)
cout << "=======================================================" << endl;
cout << "GetNewMatrix # (Create a new Matrix)" << endl;
cout << "AddMatrices # # # (Adds two matrices together)" << endl;
cout << "SubMatrices # # # (Subtracts a second matrix from the first)" << endl;
cout << "MultiplyMatrices # # # (Multiplies two matrices together)" << endl;
cout << "PrintMatrix # (Print out a matrix)" << endl;
cout << "Quit (Quit the program)" << endl;
cout << "=======================================================" << endl << endl;
cout << "Please enter your choice here: ";
if(count > 0)
cin.ignore();
getline(cin, input);
istringstream copy;
copy.str (input);
count++;
copy >> case_value;
copy >> num1; //value used in the first # of EACH case if it has one
copy >> num2; //value used in the second # of EACH case if it has one
copy >> num3; //value used in the third # of EACH case if it has one
if (case_value == "GetNewMatrix")
ch = GetNewMatrix;
else
if (case_value == "PrintMatrix")
ch = PrintMatrix;
else
if (case_value == "Quit")
ch = Quit;
else
cout << "Unknown entry. Please use exact capitalization." << endl;
switch(ch)
case GetNewMatrix:
cout << "Matrix " << num1 << " obtained." << endl;
cout << "Please enter values (between 1 and 10) for row and column amount: ";
cin >> r >> c;
matrix[num1].SetSize(r,c);
cout << "Matrix size set to " << r << " x " << c << " reading up->down then left->right." << endl;
i = 0;
while (i < c)
cout << "Please enter the " << r << " value(s) for column " << i + 1 << " separated by spaces: ";
cin.ignore();
getline(cin, input);
istringstream copy;
copy.str (input);
for (j = 0; j < r; j++)
int temp;
copy >> temp;
matrix[num1].StoreItem(temp, i, j);
i++;
break;
【讨论】:
小心cin.ignore()
。 std::getline
不会给ignore
留下太多你需要的东西,当你必须ignore
时,在你需要ignore
ed 的流中留下你需要的东西的操作之后不久,而不是在下一个操作之前。这消除了ignore
没有任何东西的可能性,如果你ignore
以防万一,它使原因和治疗紧密结合在一起,因此更容易记住。旁注:istringstream copy; copy.str (input);
可以是 istringstream copy(input);
我不确定这会在运行时节省多少,但输入的更少,
如果你想弄乱你老师的脑袋(假设你有老师),你可以用将字符串映射到枚举的std::map<std::string, Choice> choicemap "Start", Start, "GetNewMatrix",GetNewMatrix, etc... ;
替换if (case_value == "blah blah blah") ch = PrintMatrix;
。这让你ch = choicemap.at(case_value);
。它还可以通过在map
中添加枚举和条目来轻松扩展可能性。
这会导致map
的字符串到函数,因此您不需要enum
或switch
,但这超出了分配的范围。跨度>
始终使用 .equals 来比较字符串。例如:case_value.equals("") = = 0以上是关于从用户输入中提取整数的枚举 Switch 语句的主要内容,如果未能解决你的问题,请参考以下文章