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;
}