0
好吧,基本上我的程序以用户输入开始。输入以整数n开始,指定要遵循的命令的数量。在该行之后,每行将有n行命令。我试图将这些命令中的每一个都作为一个字符串存储在一个字符串数组中,然后我试图处理每个字符串以找出它是什么类型的命令以及用户在同一行上输入的数字。存储字符串数组以及解析每个字符串的问题C++
示例输入:
./a
I 1 2
I 2 3
我希望我的节目,以便然后存储每行,在第一个输入(2)下,转换为一个字符串数组。然后我尝试处理每一个字母,并在单行中编号。
我的电流以下代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
int number_of_insertions;
cin >> number_of_insertions;
cin.ignore();
string commandlist[number_of_insertions];
for(int i = 0; i < number_of_insertions; i++) {
string command;
getline(cin, command);
commandlist[i] = command;
}
string command;
char operation;
int element;
int index;
for(int i = 0; i < number_of_insertions; i++) {
command = commandlist[i].c_str();
operation = command[0];
switch(operation) {
case 'I':
element = (int) command[1];
index = (int) command[2];
cout << "Ran insertion. Element = " << element << ", and Index = " << index << endl;
break;
case 'D':
index = command[1];
cout << "Ran Delete. Index = " << index << endl;
break;
case 'S':
cout << "Ran Print. No variables" << endl;
break;
case 'P':
index = command[1];
cout << "Ran print certain element. Index = " << index << endl;
break;
case 'J':
default:
cout << "Invalid command" << endl;
}
}
}
然后我的针对示例输入输出如下:
冉插入。元素= 32和索引= 49
Ran插入。元素= 32和索引= 50
不知道如何解决这个问题,并期待从大家得到一些帮助。