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!
CodePudding user response:
This sstatement
cin >> wejscie;
inputs only one word as soon as a white space character is encountered.
So this for loop does not make a sense.
stringstream wejscieStream(wejscie);
int element;
for (int i = 0; i < ilecyna; i )
{
wejscieStream >> element;
cyna.push_back(element);
}
Instead of using the operator >>
in this statement
cin >> wejscie;
it seems you need to use std::getline
like
std::getline( std::cin, wejscie );
But before using it you need to clear the input buffer like
#include <lomits>
//...
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
Also this for loop
for (size_t p = k/z[0]; p >= 0; p--)
is an infinite for loop because the variable p
having the unsigned integer type size_t
never can be less than 0
.
And this while loop is suspecious
while (c[pozycja] > nowakwota)
pozycja--;
because there is no check whether pozycja
becomes less than 0.
And use English word as identifiers of variables. Otherwise your code will be unreadable for the most of users.
Also it is unclear why the second parameter denotes a vector
vector<int> z
while the only one its element z[0]
is used within the function.