vendredi 15 février 2019

C++: bool method returning unexpected numbers after cout statement

I'm working on some exam-examples for a c++ programming exam and the example where I'm stuck at requires me to code a class for returning the contents of a "Closet" object. One of the methods required in the example adds a vector of Garment objects to a second vector of Garment objects, (so, filling the closet with clothes). Up until this point my code has passed all the references and checks I've been given (a list with the supposed runtime errors and cout/cerr statements), so I've removed method-definitions and calls in the code I'm posting here to only show the part where I'm getting the unexpected returns.

I supposed that one of the constructors or even another method might interfere with the output, so I've ran several versions of the code trough a visualizer (Python tutor for c++), but that didn't shed any new insight either, no other methods were called (as expected) and no other output prompted from the constructors either.

#include <iostream>
#include <stdexcept>
#include <vector>
#include <string>

using namespace std;


enum class Color{Red, Blue, Gray, Yellow};
const std::vector<std::string> color_names{"red", "blue", "gray", "yellow"};

enum class Type{Pants, Blouse, Shirt, Skirt};
const std::vector<std::string> type_names{"pants", "blouse", "shirt", "skirt"};

class Garment {
  int preis;
  Color farbe;
  Type typ;


  public:

  //Konstruktor
  Garment (int p, Color f = Color::Gray, Type t = Type::Pants){

      this->preis = p;
      this->farbe = f;
      this->typ = t;

      //negativer Preis = exception

      if (p < 0){throw runtime_error("Preis kleiner als 0!");} }



int get_price() const{
return this->preis; }

    Type get_type() const{
    return this->typ; }

    bool has_color(Color f) const{}

    void deteriorate(int w){}

    int get_index_color() const{}

int get_index_type() const{}    



   friend ostream& operator<<(ostream& out, const Garment &g){
        //[40000 Cent, yellow blouse]

out << "[" << g.preis << " Cent, "<< color_names[g.get_index_color()] 
<< " " << type_names[g.get_index_type()];
        out << "]";
        return out;

    } 


};

class Closet {
size_t capacity;
vector<Garment> inventory;


public:
//Konstruktor Beginn

Closet (size_t c, vector<Garment> inv){

    this->capacity = c;
    this->inventory = inv;

if (capacity < 5 || capacity > 300){throw runtime_error ("Komplette Kapazitaet ueber oder unterschritten!");}


if (this->inventory.size() > this->capacity){throw runtime_error ("Relative kapazitaet ueberschritten");}


        vector<int>kleiderliste {0,0,0,0};

         for (auto x : inv){
              if (x.Garment::get_type() == Type::Pants){kleiderliste[0]++;}
              if (x.Garment::get_type() == Type::Blouse){kleiderliste[1]++;}
              if (x.Garment::get_type() == Type::Skirt){kleiderliste[2]++;}
             if (x.Garment::get_type() == Type::Shirt){kleiderliste[3]++;}
         }

       int zaehler = 0;
        for (auto y : kleiderliste){
              if (y != 0 ){zaehler++;}
         }
    if (zaehler <2){throw runtime_error("Nur mehr kleidungsstuecke eines typs im schrank");}



}


bool add(vector<Garment> v){

            if ((v.size() + this->inventory.size()) <= this->capacity){
            cerr << 1;
            this->inventory.insert(this->inventory.begin(),v.begin(),v.end());
            return true;
            }else{
            cerr << 0;
            return false;
            }

}

double mean_price() const{

}

friend ostream & operator<<(ostream &out,const Closet &c){

    out << "[" << c.capacity << ",{";
     for (auto x : c.inventory){
        out <<x;
     }

    out << "},";
    out << c.mean_price();
    out << "]";

    return out;

    }
};

int main(){

Garment pants{34500, Color::Blue, Type::Pants};
Garment blouse{12700, Color::Red, Type::Blouse};
const Garment shirt{2300, Color::Yellow, Type::Shirt};
Garment shirt2{23500, Color::Red, Type::Shirt};
Garment skirt{26600, Color::Gray, Type::Skirt};
Garment skirt2{4600, Color::Blue, Type::Skirt};


Closet closet {10, {skirt, blouse, shirt, pants, skirt}}; 
cout << closet.add({shirt2, skirt2}) << closet.add({blouse,skirt,pants}) << closet.add({}) << closet.add({pants}) << '\n';

 return 0; }       

This code is supposed to yield the following output via cout: 1110. The Closet::add method is supposed to return true three times and false one time in a row.

What I actually get as return values via cout << is: 0111 To test if the code does what it's supposed to I'm outputting 1 for true and 0 for false on the cerr channel too, and there I get the correct 1110 numbers.

What leads to the return output not be 1110? Are the method calls made in a different order in the compiler?

Aucun commentaire:

Enregistrer un commentaire