从用户输入中提取整数的枚举开关语句
Enumarated Switch Statement With Extracting an Integer From User Input
我有一个开关语句菜单,具有以下要求:
-
"GetNewMatrix"作为枚举选项,它是一个从0到9的数字。
-
用户输入必须全部在一行中。
-
必须使用枚举。
我需要用户能够输入类似"GetNewMatrix 5"的内容,并让switch语句参见GetNewMatrix来初始化该情况,以及将5向下传递到初始化矩阵[5]的情况中。MatrixType()
我完全不知道如何实现这一点。我目前有以下(相当粗略的)代码,尽管这并不能帮助我从用户的输入中提取整数,因为它需要在所有一行中完成,如上所述。
-
matrix是一个大小为[10]的Class MatrixType数组,它包含int值[MAX_ROWS][MAX_COLS]、int numRows和int numCols
-
input是一个字符串,用于获得用户输入,并将其与枚举案例进行比较,以决定使用的情况
-
name是一个介于0和9之间的整数,用于标记[10]数组中的矩阵
-
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;
正如您现在看到的枚举后面的工作方式类似于从0开始的数组索引,依此类推,所以插入使用char使您的char ch;变量为int,如int ch;然后您可以将int中的input作为选项,并根据case将运行的输入将其作为0到9放入switch中。
使用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;
}
相关文章:
- 为什么我的开关/机箱在使用枚举时默认?
- 无法找到简单的开关大小写枚举错误
- 对clang、gcc和icc中开关枚举类返回的处理一致
- 枚举和开关语句C++
- 从用户输入中提取整数的枚举开关语句
- C 在开关语句中使用特定类和枚举范围
- 如何通过C 的开关运行类枚举
- c++警告:枚举值未在开关[-Wswitch]中处理
- 有没有办法不警告 gcc 中开关缺少"COUNT"枚举常量?
- 多个枚举值的开关/机箱
- C++无法同时使用枚举和开关
- c++开关枚举错误
- 从大量的枚举中,我尝试通过使用一些模板技巧来创建一个函数来应用正确的操作,而无需使用开关主体
- C++11 在开关大小写中混合枚举类和无符号 int 将无法编译
- 开关条件中从类到枚举类型的隐式转换
- 如何获取枚举值是否> C++枚举的开关语句中的常量 N?
- 如何在开关条件中使用枚举
- 使用枚举和开关而不是访问者模式
- GCC中枚举开关的控制流分析不足
- 枚举参数函数和开关优化