0

After expanding my program to include change such as 0.01,0.02,0.05,0.1,0.2,0.5 (zł) I was given:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Process returned 3 (0x3)   execution time : 56.358 s
Press any key to continue.

It isn't the first time I have gotten this message, but it only happens upon using vectors.

The program would be working fine had I refrained from adding the update, but I'm curious as to why this message pops out, and what the cause of it may be. I suppose it has to do with the bad placement of something in the memory?

Thank you for your help people.

    #include <iostream>
#include <vector>
using namespace std;

int main(){

    int iloscMonet=9;
    double monety[iloscMonet]={0.01,0.02,0.05,0.1,0.2,0.5,1,2,5};
    double resztaDoWydania=4.01;
    int licznikMonet=0;
    vector <int> jakieMonety;
        while(resztaDoWydania){
            int nominal = 0;
            for(int i=0;i<iloscMonet;i++){
                if((monety[i]<=resztaDoWydania)&&(monety[i]>nominal)){
                    nominal=monety[i];
                }
            }
            resztaDoWydania-=nominal;
            jakieMonety.push_back(nominal);
            licznikMonet++;
        }
        cout<<"ile monet?: "<<licznikMonet<<endl;
        cout<<"jakie monety?: ";
        for(int i=0;i<jakieMonety.size();i++){
            cout<<jakieMonety.at(i)<<" ";
        }

    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Related: [language agnostic - Is floating point math broken? - Stack Overflow](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – MikeCAT Jul 15 '21 at 11:32

2 Answers2

0

Calculation of floating-point number may contain errors.

When I add #include <cstdio> in top of your code and printf("%.30f\n", resztaDoWydania); after licznikMonet++;, I found that the value of resztaDoWydania stacking at 0.009999999999999786837179271970.

You should avoid using floating-point numbers as much as you can. In this case, you can multiply each of the values by 100 to make them integers.

#include <iostream>
#include <vector>
using namespace std;

int main(){

    int iloscMonet=9;
    int monety[iloscMonet]={1,2,5,10,20,50,100,200,500};
    int resztaDoWydania=401;
    int licznikMonet=0;
    vector <int> jakieMonety;
        while(resztaDoWydania){
            int nominal = 0;
            for(int i=0;i<iloscMonet;i++){
                if((monety[i]<=resztaDoWydania)&&(monety[i]>nominal)){
                    nominal=monety[i];
                }
            }
            resztaDoWydania-=nominal;
            jakieMonety.push_back(nominal);
            licznikMonet++;
        }
        cout<<"ile monet?: "<<licznikMonet<<endl;
        cout<<"jakie monety?: ";
        for(int i=0;i<jakieMonety.size();i++){
            cout<<jakieMonety.at(i)<<" ";
            // if you want outpuf of floating-point numbers as the original
            //cout<<(jakieMonety.at(i)/100.0)<<" ";
        }

    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • This obviusly solve the bug in the code, but maybe you could explain why the bad_alloc is a consequence of that bug, it seems to be a good thing to point out. – Federico Jul 15 '21 at 13:45
  • The loop goes inifinitely without changing the value of `resztaDoWydania`. It means it does `jakieMonety.push_back(nominal);` again and again, and eventually it will do too many times to cause `bad_alloc`. – MikeCAT Jul 15 '21 at 21:28
-1

try while(resztaDoWydania>0)

Jaly
  • 9
  • 1