I have this class :
int br=0; // br is counter how much records i hold in my vector

class racunalo{
      public:
      void upis(string naziv, string velicinaRam, string velicinaHDD, string brzina, string proizvodac);  
      void upisKomp();  
      string getNaziv(int s) const;
      void pregled();
      void upisDat();
      void pregledNaziv();
      void pregledProizvodac();
      void brisanjeRac();
      string getVelicinaRam(int s) const;
      string getVelicinaHDD(int s) const;
      string getVelicinaBrzina(int s) const;
      string getProizvodac(int s) const;
      string getBrzina(int s) const;
      void ispisKomp();
      private:
      struct komp{
             string naziv;
             string velicinaRam;
             string velicinaHDD;
             string brzina;
             string proizvodac;
             };
      vector<komp>racunala;
      
        };

and i want to delete record from vector racunala

i tried to write function like this:

void racunalo::brisanjeRac(){
cout<<"Enter computer name you want to delete:"<<endl;
string comp_name;
cin>>comp_name;
for(int i=0;i<br;i++){
if(kompjuteri.getNaziv(i) == comp_name){
racunala.erase(racunala.begin()+i);}
     }}

but it doesnt work. Lets say i got 2 records in my file.

Acer
Toshiba

when i try to delete Acer it doesn't delete but it get replaced by Toshiba so i get output like

Toshiba
Toshiba

Please help me, thank you in advance.

Why are you trying to access a second element, in a 1 element vector?


Unless I am missing something..

//I think this...
if(kompjuteri.getNaziv(i) == comp_name){
     racunala.erase(racunala.begin()+i);}

//Should be this:
if(racunala[i].naziv == comp_name){
     racunala.erase(racunala.begin()+i);}

You need access to the vector as it is a private member of class racunalo.

getNaziv() does not do this for you, as it returns a 'string'

few possible options:

  • make racunala public.
  • create an accessor function that will return a pointer to racunala.
  • declare a public iterator to racunala.
  • create an accessor function that will return a private iterator
  • declare the struct and vector outside the scope of the class.

You need access to the vector as it is a private member of class racunalo.

getNaziv() does not do this for you, as it returns a 'string'

few possible options:

  • make racunala public.
  • create an accessor function that will return a pointer to racunala.
  • declare a public iterator to racunala.
  • create an accessor function that will return a private iterator
  • declare the struct and vector outside the scope of the class.

Thank you, in my assignment i got, there is written racunala must be in private section so i cant use that option. I cant use last one either.
Other three i don't know how to make. Can u give me some example or something else.

Thank you so far.

Can you use remove_if ?

Maybe i could, but i dont know how should i write it.

Why are you trying to access a second element, in a 1 element vector?


Unless I am missing something..

//I think this...
if(kompjuteri.getNaziv(i) == comp_name){
     racunala.erase(racunala.begin()+i);}

//Should be this:
if(racunala[i].naziv == comp_name){
     racunala.erase(racunala.begin()+i);}

No, i cannot use racunala, as its in private section and i cannot access to it direct.

Try this:

class racunalo
{
     private:

          struct komp
          {
               string naziv;
               string velicinaRam;
               string velicinaHDD;
               string brzina;
               string proizvodac;
         };

          vector<komp> racunala;         

     public:

          komp get_komp(int index){return racunala[index];}
          vector<komp> get_rac(){return racunala;}
          void erase(int element){racunala.erase(racuanala.begin()+element);}
          void erase(int first, int last){racunala.erase(racunala.begin()+first, racunala.begin()+last);}
};

int main()
{
     racunalo rac;
 
     for(int i=0; i<br; i++)
     {
          if(rac.get_komp(i).naziv == comp_name)
          {
               rac.erase(i);
               br--;
               i--;
          }
     }
     return 0;
}

Try this:

class racunalo
{
     private:

          struct komp
          {
               string naziv;
               string velicinaRam;
               string velicinaHDD;
               string brzina;
               string proizvodac;
         };

          vector<komp> racunala;         

     public:

          komp get_komp(int index){return racunala[index];}
          vector<komp> get_rac(){return racunala;}
          void erase(int element){racunala.erase(racuanala.begin()+element);}
          void erase(int first, int last){racunala.erase(racunala.begin()+first, racunala.begin()+last);}
};

int main()
{
     racunalo rac;
 
     for(int i=0; i<br; i++)
     {
          if(rac.get_komp(i).naziv == comp_name)
          {
               rac.erase(i);
               br--;
               i--;
          }
     }
     return 0;
}

I tried like u wrote here, it starts but when i enter comp_name and press enter program stop responding.

void racunalo::brisanjeRac(){
racunalo rac;
cout<<"Enter computer name you want to delete:"<<endl;
string comp_name;
cin>>comp_name;
for(int i=0;i<br;i++){
if(rac.get_komp(i).naziv == comp_name){
rac.erase(i);
br--;
i--;}
}
}

Here is complete program:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int br=0;
class racunalo{
      public:
      void upis(string naziv, string velicinaRam, string velicinaHDD, string brzina, string proizvodac);  
      void upisKomp();  
      string getNaziv(int s) const;
      void pregled();
      void upisDat();
      void pregledNaziv();
      void pregledProizvodac();

      void brisanjeRac();
      string getVelicinaRam(int s) const;
      string getVelicinaHDD(int s) const;
      string getVelicinaBrzina(int s) const;
      string getProizvodac(int s) const;
      string getBrzina(int s) const;
      void ispisKomp();
      private:
      struct komp{
             string naziv;
             string velicinaRam;
             string velicinaHDD;
             string brzina;
             string proizvodac;
             };
            vector<komp>racunala;
            komp get_komp(int index){return racunala[index];}
vector<komp> get_rac(){return racunala;}
void erase(int element){racunala.erase(racunala.begin()+element);}
void erase(int first, int last){racunala.erase(racunala.begin()+first, racunala.begin()+last);}
        };
racunalo kompjuteri;        
        
        
void racunalo::upis(string nazivNew, string velicinaRamNew, string velicinaHDDNew, string brzinaNew, string proizvodacNew){
     komp temp;
     temp.naziv = nazivNew;
     temp.velicinaRam = velicinaRamNew;
     temp.velicinaHDD = velicinaHDDNew;
     temp.brzina = brzinaNew;
     temp.proizvodac = proizvodacNew;
     br++;
     racunala.push_back(temp);
     }

string racunalo::getNaziv(int s) const
{return racunala[s].naziv;
}
string racunalo::getVelicinaRam(int s) const
{return racunala[s].velicinaRam;
}
string racunalo::getVelicinaHDD(int s) const
{return racunala[s].velicinaHDD;
}
string racunalo::getBrzina(int s) const
{return racunala[s].brzina;
}
string racunalo::getProizvodac(int s) const
{return racunala[s].proizvodac;
}
void racunalo::ispisKomp(){
                cout<<"Naziv"<<endl;
     for(int i=0;i<br;i++){
cout<<kompjuteri.getNaziv(i)<<endl;               
             }
     }
     //upis
void racunalo::upisKomp(){
     ifstream datoteka;
     string naziv,ram,hdd,brzina,proizvodac;
     datoteka.open("ducan.txt");
     while(!datoteka.eof()){
     datoteka >> naziv >> ram >> hdd >> brzina >> proizvodac;
     racunalo::upis(naziv,ram,hdd,brzina,proizvodac);}
     cout<<"Podaci iz datoteke su ucitani!"<<endl;
     system("pause");
     }
void racunalo::pregledNaziv(){
     cout<<"Unesite naziv racunala:"<<endl;
     string naziv;
     cin>>naziv;
     system("cls");
     cout<<"Naziv\tRam\tHDD\tBrzina\tProizvodac"<<endl;
     for(int i=0;i<br;i++){
             if(kompjuteri.getNaziv(i) == naziv){
             cout<<kompjuteri.getNaziv(i)<<"\t\t"<<kompjuteri.getVelicinaRam(i)<<"\t"<<kompjuteri.getVelicinaHDD(i)<<"\t"<<kompjuteri.getBrzina(i)<<"\t"<<kompjuteri.getProizvodac(i)<<endl;}}
     }    

void racunalo::brisanjeRac(){
racunalo rac;
cout<<"Enter computer name you want to delete:"<<endl;
string comp_name;
cin>>comp_name;
for(int i=0;i<br;i++){
if(rac.get_komp(i).naziv == comp_name){
rac.erase(i);
br--;
i--;}
}
}

//upis u datoteku
void racunalo::upisDat(){
     int brojac=0;
     ofstream datoteka;
     datoteka.open("ducan.txt");
     for(int i=0;i<br;i++){
     datoteka<<kompjuteri.getNaziv(i)<<" "<<kompjuteri.getVelicinaRam(i)<<" "<<kompjuteri.getVelicinaHDD(i)<<" "<<kompjuteri.getBrzina(i)<<" "<<kompjuteri.getProizvodac(i)<<endl;
     brojac++;}
     cout<<brojac<<" racunala je upisano u datoteku ducan.txt ."<<endl;
     }
     
     
void racunalo::pregledProizvodac(){
     cout<<"Unesite proizvodaca racunala:"<<endl;
     string naziv;
     cin>>naziv;
     system("cls");
     cout<<"Naziv\tRam\tHDD\tBrzina\tProizvodac"<<endl;
     for(int i=0;i<br;i++){
             if(kompjuteri.getProizvodac(i) == naziv){
             cout<<kompjuteri.getNaziv(i)<<"\t\t"<<kompjuteri.getVelicinaRam(i)<<"\t"<<kompjuteri.getVelicinaHDD(i)<<"\t"<<kompjuteri.getBrzina(i)<<"\t"<<kompjuteri.getProizvodac(i)<<endl;}}
     }    
     //pregled
void racunalo::pregled(){
     system("cls");
     cout<<"Prema kojem kriteriju zelite pregledati racunala:"<<endl;
     cout<<"1.Prema nazivu"<<endl;
     cout<<"2.Prema proizvodacu"<<endl;
     int izbor;
     cin>>izbor;
     switch(izbor){
                   case 1:
                        kompjuteri.pregledNaziv();
                        system("pause");
                        break;
                   case 2:
                        kompjuteri.pregledProizvodac();
                        system("pause");
                        break;
                        };
     }
     //meni
void meni(){
     for(;;){
     system("cls");
     cout<<"MENI"<<endl;
     cout<<"1. Unos podataka iz datoteke"<<endl;
     cout<<"2. Ispis podataka o racunalu"<<endl;
     cout<<"3. Brisanje podataka odabranog racunala"<<endl;
     cout<<"4. Pregled podataka o racunalima prema odredenom kriteriju"<<endl;
     int izbor;
     cin>>izbor;
     switch(izbor){
                   case 1:
                         kompjuteri.upisKomp();
                         break;
                   case 2:
                        kompjuteri.upisDat();
                        system("pause");
                        break;
                   case 3:
                        kompjuteri.brisanjeRac();
                        break;
                   case 4:
                        kompjuteri.pregled();
                        break;
                   case 5:
                        kompjuteri.ispisKomp();
                        system("pause");
                        break;
                         }}}     
int main(){
    meni();
    system("pause");}

I dont know where is error. Can you look at it and see.

P.S. Hrvatska = ljepe žene :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.