Home > database >  C : Loop will run at most once (loop increment never executed)
C : Loop will run at most once (loop increment never executed)

Time:05-30

I know there is a topic Loop will run at most once (loop increment never executed), but it does not answear my question.

I have following code:

class Station{
public:
    Station();
    void addWartezimmer(Wartezimmer wartezimmer);
    void addBehandlungszimmer(Behandlungszimmer behandlungszimmer);
    int getWartezimmer();
private:
    list<Wartezimmer> WartezimmerListe;
    list<Behandlungszimmer> BehandlungszimmerListe;
};

Station::Station(){
    
}

void Station::addWartezimmer(Wartezimmer wartezimmer){
    this->WartezimmerListe.push_back(wartezimmer);
}

void Station::addBehandlungszimmer(Behandlungszimmer behandlungszimmer){
    this->BehandlungszimmerListe.push_back(behandlungszimmer);
}

int Station::getWartezimmer(){
    list<Wartezimmer>::iterator i;
    for (i = WartezimmerListe.begin(); i != WartezimmerListe.end(); i  ){
        
    return  i->getAnzahlPlaetze();
    
    }
   
    return 0;

};

int main(int argc, const char * argv[]) {
    Zimmer zimmer("05.23");
    
    Wartezimmer WarteZimmer1(12, "01.12");
    Wartezimmer WarteZimmer2(14, "03.12");
    
    Behandlungszimmer Behandlungszimmer1("Intensiv", "01.01");
    
    Station West;
    West.addWartezimmer(WarteZimmer1);
    West.addWartezimmer(WarteZimmer2);
    West.addBehandlungszimmer(Behandlungszimmer1);
    
    
    cout << West.getWartezimmer();
}

If I execute this command, it will be printed "12". I expect "1214", because there are two Objects which have Numbers.

If I change the function to void and print it in the function, it is "1214".

void  Station::getWartezimmer(){
    list<Wartezimmer>::iterator i;
    for (i = WartezimmerListe.begin(); i != WartezimmerListe.end(); i  ){
        
    cout <<   i->getAnzahlPlaetze();
    
    }
   

}; 

So my specific Question: How can I return a value of a function, if it is more than one value from a list? I don´t want only "12", I want "1214" because they are 2 Objects.

CodePudding user response:

If you would like to get whole data you could change int getWartezimmer(); to

list<Wartezimmer> Station::getWartezimmer()
{
   return WartezimmerListe;
}

but I guess you want AnzahlPlaetze whatever it is so int getWartezimmer you could simply do this

vector<AnzahlPlaetze> Station::getWartezimmer()
{
   vector<AnzahlPlaetze> result;
   for(const auto& ob: WartezimmerListe)
   {
      result.push_back(ob.getAnzahlPlaetze());
   }
   return result
}

CodePudding user response:

Your code is working exactly as it should work, the function getWartezimmer() returns something as soon as it finds it.

In this code

int Station::getWartezimmer(){
  list<Wartezimmer>::iterator I;
  for (i = WartezimmerListe.begin(); i != WartezimmerListe.end(); i  ){
    
  return  i->getAnzahlPlaetze(); //--> this line
  }

  return 0;
};

at the lined I've marked, the function getAnzahlPlaetze() fetches a value and returns it to a returns statement. What happens then? the return statement returns this value to the main function and the execution continues from there. So every time you call this function, it will always return the firs value of the WartezimmerListe vector.

I don't know the exact behavior that you are looking for after calling getWartezimmer(). do you wanna pop an element from the beginning of the array or from its end?

You can edit the getAnzahlPlaetze() function to return an element and then delete it or use std::vector::pop_back() if it matches your needs. This will effectively give you a new value for each call.

If you want to maintain the original vector but also poping values from it you can define a static variable inside the class and use it to index the vector.

I don't think you want getAnzahlPlaetze() to return the whole values of the vector because it makes no sense. You can use the vector itself.

  • Related