C++ Saving 2-dimensional database into file ISSUE

时间:2017-05-16 09:29:06

标签: c++ arrays file save

I'm writing a small console app (name it student evidention), which has couple of functions, i.e it allows user to export database into text file.

The problem which occurs during this process is that program actually saves only the first record of mentioned database.

void saveDataToFile() {             // ZAPISZ BAZĘ DANYCH DO PLIKU - ISTNIEJE PROBLEM Z OKREŚLENIEM ŚCIEŻKI ZAPISU ORAZ ZAPISYWANIEM JEDYNIE PIERWSZEGO REKORDU Z BAZY
string fileName;
ofstream fileTemp;
cout << "Podaj nazwę pliku: "; cin >> fileName;
fileTemp.open(fileName);
fileTemp << "+--------------------------------------------------------------------------------------------------------+" << endl;
fileTemp << "|\tImię\t|\tNazwisko\t|\tNr PESEL\t|\t Telefon\t|\tMail\t |" << endl << "+--------------------------------------------------------------------------------------------------------+" << endl;
for (row; row < 20; row++) {
    for (col; col < 5; col++) {
        if (col == 0) {
            fileTemp << "\t" << studentDb[row][col] << "\t";
        }
        else {
            fileTemp << studentDb[row][col] << "\t";
        }
    }
    fileTemp << endl;
}
fileTemp.close();

}

I have no idea what's wrong with that code, because the loop itself seems to be completely fine...

I'd be glad for any tips for resolving this issue. Ignore lines written in polish - it has only some cosmetic, text-formatting functions in the program.

EDIT.

The way I create a new record:

void createNewRecord() {        // UTWÓRZ NOWY REKORD W BAZIE
string *pNewRecord;
pNewRecord = &studentDb[recordCounter][0];
cout << "Imię: "; cin >> *pNewRecord;
pNewRecord = &studentDb[recordCounter][1];
cout << "Nazwisko: "; cin >> *pNewRecord;
pNewRecord = &studentDb[recordCounter][2];
cout << "Numer PESEL: "; cin >> *pNewRecord;
pNewRecord = &studentDb[recordCounter][3];
cout << "Numer telefonu: "; cin >> *pNewRecord;
pNewRecord = &studentDb[recordCounter][4];
cout << "Adres e-mail: "; cin >> *pNewRecord;
recordCounter++;
cout << endl << "Nowy rekord został pomyślnie dodany do bazy." << endl;
cout << "Czy chcesz kontynuować? (t/n): "; cin >> response;
cout << endl;
if (response == 't' || response == 'T') {
    cout << "Kontynuujesz dodawanie do bazy." << endl << endl;
    createNewRecord();
}
else if (response == 'n' || response == 'N') {
    Sleep(1000);
    system("cls");
    void displayMenuWindow();
}

}

1 个答案:

答案 0 :(得分:1)

设置&#39;行&#39;和&#39; col&#39;在&#39; for&#39;的初始化部分中变量为零。循环。并将它们作为局部变量(将它们声明为&#34; int row = 0&#34;)。