从字符串数组读取输入

时间:2019-11-20 17:01:51

标签: c++

想要读取字符串输入(从数组中)并将其保存到其他变量中。我的代码:

const string myArray[]{"4", "1 - 5", "Text_1", "Text_2", "Text_3"};

//method, which saves 1st item from myArray into line1, 2nd into line2 and the rest into line3
void Parts::saveParts(const string *myArray, int rows) {
    // rows == length of myArray
    istringstream iss(*myArray);
    int id;
    char del;
    if (iss >> id >> del) {
        line = id;
        string rawLine2;
        if (iss >> rawLine2 >> del) {
            this->setLine(rawLine2);
            string text;
            textItems[numberOfRows-2];
            if (iss >> text) {
                // how can I read the rest of myArray? What should I set as 'del'?
                // 'Text_1' should go into 'textItems[0]' and so on
            }
        }
    }
    else {
        // here I want to throw an exception, if reading (iss >> id >> del) goes wrong (in case of id is not an integer
    }

}

如果代码结构本身是正确的,您能指导我完成带符号的if语句吗?

0 个答案:

没有答案
相关问题