I'm writing quite simple program in c++ using Visual Studio but i keep getting 2 errors and i could use some help.
One sometimes pops up when i run Local Windows Debugger after the app crashed and it says:
The application was unable to start correctly (0xc0000142)
message sometimes when running
And the second one i guess happens between after cin >> kwota;. Don't really know when though, because VS is not showing any exception. It's just a message box from the os. It says:
Debug Assertion Failed!
[...]
File: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vector"
Line: 1566
Expression: vector subscript out of range
message after puting second time into cin
Here you have my code. I got no idea what can cause it so any help would be great.
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
int wyznaczZlote(vector<int> c, int z, int k)
{
int nowakwota, pozycja, ilosc;
if (z > k)
return 0;
for (int p = k/z; p >= 0; p--)
{
nowakwota = k % z;
while (nowakwota > 0)
{
pozycja = c.size() - 1;
while (c[pozycja] > nowakwota && pozycja >= 0)
{
pozycja--;
if (pozycja < 0)
break;
}
if (pozycja < 0)
break;
ilosc = nowakwota / c[pozycja];
nowakwota = nowakwota % c[pozycja];
}
if (nowakwota == 0)
return p;
}
return -1;
}
int main()
{
int ilecyna, ilezloto, kwota, liczbazlotych;
vector<int> cyna;
vector<int> zloto;
string wejscie, wejscie2;
cin >> ilecyna;
cin.ignore();
getline(cin, wejscie);
stringstream wejscieStream(wejscie);
int element;
for (int i = 0; i < ilecyna; i++)
{
wejscieStream >> element;
cyna.push_back(element);
}
sort(cyna.begin(), cyna.end());
cin >> ilezloto;
cin.ignore();
getline(cin, wejscie2);
stringstream wejscieStream2(wejscie2);
for (int i = 0; i < ilecyna; i++)
{
wejscieStream2 >> element;
cyna.push_back(element);
}
sort(zloto.begin(), zloto.end());
cin >> kwota;
liczbazlotych = wyznaczZlote(cyna, zloto[0], kwota);
cout << '\n';
if (liczbazlotych >= 0)
cout << liczbazlotych << '\n';
else
cout << "NIE" << '\n';
cin.ignore();
getchar();
}
Thanks!