如何跳过二维数组中的空白元素,使数组只存储非空值?

问题描述 投票:0回答:0

我正在从文件中读取并将字符串存储在二维数组中,但我的数组存储了我不需要的空行。

我有字符串所在的全局数组。

然后是将文件读入二维数组的函数。

然后我必须显示二维数组的内容,但是因为它在其元素中存储了一个空行,所以它破坏了内容的显示。

const int rows = 50;
const int columns = 5;
string questionsArray[rows][columns];

void readQuestions(ifstream &questionsFS, string (&questionsArray)[rows][columns]) {
    string textline;
    int x = 0;
    int y = 0;
    while(getline(questionsFS, textline)){
        questionsArray[x][y] = textline;
        y++;
        if(textline == "" && textline == " "){
            x++;
        }
        
    }

}

void showQuestion(string studentName, string (&questionsArray)[rows][columns]){
    int i = 1;
    cout << questionsArray[i][0] << endl;
    cout << "A. " <<questionsArray[i][1] << endl;
    cout << "B. " << questionsArray[i][2] << endl;
    cout << "C. " << questionsArray[i][3] << endl;
    cout << "D. "<< questionsArray[i][4] << endl;
    cout << "This is stored here: " << questionsArray[i][0] << endl;
    
}

这是我觉得是问题所在的代码。也没有矢量,因为我没有任何矢量经验,我认为我不能及时重写程序。

我认为问题出在 readQuestion() 函数中。我尝试了一些方法来使代码正常工作,但都没有奏效。如果需要,我可以发布整个程序。

arrays function file
© www.soinside.com 2019 - 2024. All rights reserved.