0

I have a code like this:

int cal ()
{
    int sum=0;
    for(vector<vector<float> > tw : b)
    {
        int cases=0;
        float summ=0;
        for (vector<float> bev : tw)
        {
            for (float annual : bev)
            {
                if (summ>=700)
                    cases=1;
            }
        }
        sum+=cases;
    }
    return sum;
}

It's a 3D vector, and I read a file which contains numbers:

538.45 -189.45 105.25 -154.21
251.45 -102.25 506.53 -164.79
789.14 -189.15 763.08 -199.15
562.51 -152.72 429.89 -312.81
417.45 -164.45 364.15 -155.25

Alright, and this would be the result, but It's not okay:

Financial statistics of Paris Saint-Germain in last 5 years:
******************************************************************
            Expenditure Income
Winter Transfer window  538.45  -189.45
Summer transfer window  105.25  -154.21
________________________________________________________
            Expenditure Income
Winter Transfer window  251.45  -102.25
Summer transfer window  506.53  -164.79
________________________________________________________
            Expenditure Income
Winter Transfer window  789.14  -189.15
Summer transfer window  763.08  -199.15
________________________________________________________
            Expenditure Income
Winter Transfer window  562.51  -152.72
Summer transfer window  429.89  -312.81
________________________________________________________
            Expenditure Income
Winter Transfer window  417.45  -164.45
Summer transfer window  364.15  -155.25
________________________________________________________
            Expenditure Income
Winter Transfer window  -155.25 -155.25
Summer transfer window  -155.25 -155.25
________________________________________________________
How many times did the team breach the fair play?: 0

Should the team get banned from UCL?(1=yes, 0=no):  0

So I don't know how to sum the rows of the brackets of a year. I mean I want to sum the both winter and summer transfer window (and both the expenditures and the income). Somehow I tried many different ways, but it requires the operator, which I don't know how to write it. And lastly, I have no idea how the program generated the last row on summary file.. (I mean the 6th, when I gave only 5 rows) I would appreciate for any kind of help! :D

You can see the whole program below:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;


struct Income
{
private:
    vector<vector<vector<float>>> b;
public:
    Income (string filename)
    {
        ifstream fin(filename.c_str());
        if (!fin.is_open())
            cerr<<"404 Error";

        vector<vector<vector<float>>> years;
        float ke;

        while(fin.good())
        {
            vector<vector<float>> tw;
            for (int i=0; i<2; i++)
            {
                vector<float> bev;
                for (int j=0; j<2; j++)
                {
                    fin>>ke;
                    bev.push_back(ke);
                }
                tw.push_back(bev);
            }
            years.push_back(tw);
        }
        fin.close();
        b=years;
    }


    int cal ()
    {
        int sum=0;
        for(vector<vector<float> > tw : b)
        {
            int cases=0;
            float summ=0;
            for (vector<float> bev : tw)
            {
                for (float annual : bev)
                {
                    if (summ>=700)
                        cases=1;
                }
            }
            sum+=cases;
        }
        return sum;
    }


    bool banned()
    {
        if((100*cal()/b.size())>49.9)
            return true;
    }

    void summary(string filename)
    {
        ofstream outf(filename.c_str());

        outf<<"Financial statistics of Paris Saint-Germain in last 5 years:"<<endl;
        outf<<"******************************************************************"<<endl;
        for(vector<vector<float> > tw : b)
        {
            outf<< '\t'<< '\t'<< '\t'<<"Expenditure"<<' '<<"Income"<<endl;
            int windows=0;
            for (vector<float> bev : tw)
            {
                if (windows==0)
                    outf<<"Winter transfer window";
                else if (windows==1)
                    outf<<"summer transfer window";
                for (float annual : bev)
                {
                    outf<<'\t'<<annual;
                }
                outf<<endl;
                windows++;
            }
            outf<<"________________________________________________________";
            outf<<endl;
        }
        outf<<"How many times did the team breach the fair play?: "<<cal()<<endl;
        
        outf<<"Should the team get banned from UCL?(1=yes, 0=no):  "<<banned()<<endl;
        outf.close();

        cout<<"Done! You can check it on summary.txt file"<<endl;
    }

};

int main()
{
    Income I("stats.txt");
    I.summary("summary.txt");

    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
RyD3r
  • 1
  • 3
    `for(vector > tw : b)` makes a complete copy of your vector on each iteration. Change it to `for (const vector& tw : b)` or simply `for (const auto& tw : b)` for improvement. Same treatment on on the inner for loop: `for (const auto& bev : tw)` – selbie May 16 '21 at 19:07
  • 1
    `cal()` always returns 0. It's an elaborate no-op, it doesn't actually sum anything. Observe how `summ` is initialized with 0 and then is never updated, so `if (summ>=700)` is always false, `cases` is always 0, and the function is busy adding up zeros. – Igor Tandetnik May 16 '21 at 22:59
  • Re: why you have an extra row in the output. That's because you are reading an extra row in the input; check `b.size()` at the end of `Income` constructor. See [this](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) for an explanation; `while(fin.good())` is a wrong way to detect end of file. – Igor Tandetnik May 16 '21 at 23:01

0 Answers0