Witam,
Mam nastepujacy problem, mam zapetlone menu wyboru, w case 1 chce zapisac dane z tablicy do pliku, jednak dane zapisuja mi sie podwojnie, mecze sie z tym dluzszy czas i nie wiem jak to poprawic, czy moglby mi ktos pomoc? Ponizej zamieszczam kod:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
class Plyta {
private:
string tytul, autor, rodzaj_muzyki;
int id_plyty;
public:
Plyta()
{
tytul = "brak";
autor = "brak";
rodzaj_muzyki = "brak";
id_plyty = 0;
}
Plyta(int id_plyty, string tytul, string autor, string rodzaj_muzyki)
{
this->id_plyty = id_plyty;
this->tytul = tytul;
this->autor = autor;
this->rodzaj_muzyki = rodzaj_muzyki;
}
void wypisz_plyta()
{
cout << id_plyty << " " << tytul << " " << autor << " " << rodzaj_muzyki << endl;
}
int daj_idplyty()
{
return id_plyty;
}
string daj_tytul()
{
return tytul;
}
string daj_autor()
{
return autor;
}
string daj_rodzajmuzyki()
{
return rodzaj_muzyki;
}
};
class Wypozyczajacy {
private:
int id_wypozyczajacego, wiek;
string imie, nazwisko;
public:
Wypozyczajacy()
{
id_wypozyczajacego = 0;
imie = "brak";
nazwisko = "brak";
wiek = 0;
}
Wypozyczajacy(int id_wypozyczajacego, string imie, string nazwisko, int wiek)
{
this->id_wypozyczajacego = id_wypozyczajacego;
this->imie = imie;
this->nazwisko = nazwisko;
this->wiek = wiek;
}
void wypisz_wypozyczajacy()
{
cout << id_wypozyczajacego << " " << imie << " " << nazwisko << " " << wiek << endl;
}
int daj_idwypozyczajacego()
{
return id_wypozyczajacego;
}
string daj_imie()
{
return imie;
}
string daj_nazwisko()
{
return nazwisko;
}
int daj_wiek()
{
return wiek;
}
};
class Wypozyczenie {
private:
int id_wypozyczajacego;
int id_plyty;
public:
Wypozyczenie()
{
id_wypozyczajacego = 0;
id_plyty = 0;
}
Wypozyczenie(int id_wypozyczajacego, int id_plyty)
{
this->id_wypozyczajacego = id_wypozyczajacego;
this->id_plyty = id_plyty;
}
void wypisz_wypozyczenie()
{
cout << id_wypozyczajacego << " " << id_plyty << endl;
}
int daj_idwypozyczajacego()
{
return id_wypozyczajacego;
}
int daj_idplyty()
{
return id_plyty;
}
};
int main()
{
//PŁYTA:
string tytul, autor, rodzaj_muzyki;
int id_plyty = 0, i = 0;
Plyta tplyty[4];
int menu;
ofstream doplyty("plyta.txt", ios_base::app);
while (true) {
system("CLS");
cout << "1. Dodaj plyte" << endl;
cout << "2. Wyswietl plyty" << endl;
cout << "3. Dodaj klienta" << endl;
cout << "4. wyswietl klientow" << endl;
cout << "5. Koniec programu" << endl;
cout << "Wybierz: ";
cin >> menu;
switch (menu) {
case 1: {
cout << "Tytul: " << endl;
cin >> tytul;
cout << "Autor: " << endl;
cin >> autor;
cout << "Rodzaj muzyki: " << endl;
cin >> rodzaj_muzyki;
id_plyty++;
Plyta plyta(id_plyty, tytul, autor, rodzaj_muzyki);
tplyty[i] = plyta;
i++;
for (int n = 0; n < i; n++) {
doplyty << tplyty[n].daj_idplyty() << " " << tplyty[n].daj_tytul() << " " << tplyty[n].daj_autor() << " " << tplyty[n].daj_rodzajmuzyki() << endl;
}
} break;
}
}
return 0;
}