lundi 3 juin 2019

How can I compare the elements in a vector?

I took a look online and none of the answers solves the problem I have comparing the elements from a vector. I don´t get any error from the debugger first and once I run the program I suddenly get an error from the compiler.

I tried implementing a bool function but the problem is the same.

I am pretty new in c++ so please be patient!

#include <iostream>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <string>

using namespace std;

vector<int> input;
const unsigned int MAX_VEKTORSTELLEN = 5;
const unsigned int UPPER_GRENZE = 49;
const unsigned int LOWER_GRENZE = 1;
unsigned int i, j;
string output;
vector<int> randomNumbers;
int random, anzahlRichtige, eingabe;
vector<int> compareMe;

string lotto(vector<int>)
{
    if (input[i] < LOWER_GRENZE || input[i] > UPPER_GRENZE)
    {
        output = "Die Zahlen muessen zwischen 1 und 49 liegen! \n";
        input.pop_back();
    }
    else if (input.size() != MAX_VEKTORSTELLEN)
        output = "Es muessen 6 Zahlen uebergeben werde! \n";
    else
        compareMe = input;
        for (i = 0; i <= input.size(); i++)
            for (j = 0; j <= compareMe.size(); j++)
                if (compareMe[j] == input[i])
                    output = "Es duerfen keine doppelten Zahlen vorkommen! \n";
        for (i = 0; i <= MAX_VEKTORSTELLEN; i++)
            srand((unsigned)time(NULL) <= UPPER_GRENZE);
                    random = rand();
                    randomNumbers.push_back(random);
    return output;
}

int main()
{
    cout << "insert 6 numbers: ";
    while (cin >> eingabe)
    {
        input.push_back(eingabe);
        lotto(input);
        cout << output;
        for (i = 0; i < input.size(); i++) //Debug
            cout << input[i];
        continue;
    }
    for (i = 0; i < input.size(); i++)
        cout << input[i];
    system("pause");
    return 0;

I type the first number and then I get a "Debug Assertion Failed!" message saying: "Expression: vector subscription out of range".

From line 34 to line I didn´t finish to code but doesn´t really matter because I got stuck before.

Aucun commentaire:

Enregistrer un commentaire