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
不知道如何解決這個問題,並期待從大家得到一些幫助。