从用户输入中提取整数的枚举开关语句

Enumarated Switch Statement With Extracting an Integer From User Input

本文关键字:枚举 开关 语句 整数 提取 用户 输入      更新时间:2023-10-16

我有一个开关语句菜单,具有以下要求:

  • "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;
}