我有一个switch语句菜单,具有以下要求:
“ GetNewMatrix”作为枚举选项,其值为0到9之间的数字。
用户输入的内容必须全部一行。
必须使用枚举。
我需要用户能够输入“ GetNewMatrix 5”之类的内容,并让switch语句查看GetNewMatrix来启动该情况,并将5传递给该情况以初始化matrix [5] .MatrixType( )
我对如何准确实现这一点完全迷失了。 我目前有以下(相当粗略的)代码,尽管这不能帮助我从用户输入中提取用户整数,因为如上所述需要在所有一行中完成。
矩阵是MatrixType类的大小为[10]的数组,其中包含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 coloum size: ";
cin >> r >> c;
matrix[name].SetSize(r - 1,c - 1);
cout << "Matrix size set to " << r << " x " << c << endl;
break;
答案 0 :(得分:0)
您可能现在看到的枚举背后的工作就像一个数组索引,它从0开始,依此类推,因此使用char可以使您的char ch;像这样int ch的int变量; 然后您可以将int中的输入作为选择,然后根据要运行的输入将其作为0到9进行切换。
答案 1 :(得分:0)
使用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 coloumn 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 coloum 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 coloumn " << i + 1 << " seperated 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;
}