Search code examples
c++for-loopvectorruntime-errorabort

Vector and for doesn't produce results


I'm working on a simple game (rock, paper, scissors) and I got this problem when I try to populate a vector with using elements of another vector as conditions. The code maybe is more easy to understand:

#include "stdafx.h"
#include "../../Library/std_lib_facilities.h"

// With the fibonacci series I can generate a secret sequence of number
int fib(int n){
    if (1 == n || 2 == n) {
        return 1;
    }
    else {
        return fib(n - 1) + fib(n - 2);
    }
}

// It shows the list of element in a given vector
void showVector(vector<int>myVector, string nameVector) {
    cout << "\n\n";
    cout << nameVector << " Debug: | ";
    for (int i = 1; i < 10; i++) {
        cout << myVector[i] << " | ";
    }
    cout << "\n\n";
}

// String variant
void showVector(vector<string>myVector, string nameVector) {
    cout << "\n\n";
    cout << nameVector << " Debug: | ";
    for (int i = 1; i < 10; i++) {
        cout << myVector[i] << " | ";
    }
    cout << "\n\n";
}

// Generate a sequence of number in a vector
vector<int> generateCode(int input) {

    vector<int>myVector;

    for (int i = 1; i <= 10; i++) {
        myVector.push_back(fib(i + input) % 3);
    }

    return myVector;
}

int main()
{
    // Secret sequence of moves, based on the value (0, 1 or 2) i will show Rock, Paper or Scissor
    vector<int>fibSeries;
    vector<string>movSeries;

    // Initialization and settings
    int input = 0;

    cout << "Digit an integer to start: ";
    while (cin >> input) {

        // Check the number to make sure is a valid one (i will implement a check later) and generate the secret code
        fibSeries = generateCode(input);

        // For each 0 i'll put Rock in the vector, same for 1 ( in this case paper ) and 2 ( scissor )
        for (int i = 0; i <= fibSeries.size(); i++) {
            if (fibSeries[i] == 0){
                movSeries.push_back("Rock");
            }
            else if (fibSeries[i] == 1) {
                movSeries.push_back("Paper");
            }
            else if (fibSeries[i] == 2) {
                movSeries.push_back("Scissor");
            }
            else {
                movSeries.push_back("Rock");
            }
        }

        // Shows the vector graphically, for debug.
        showVector(fibSeries, "fibisSeries");
        showVector(movSeries, "movSeries");

        // So for a combination of      1 - 2 - 0 - 1
        // The result should be:        Paper - Scissor - Rock - Paper

    }

    return 0;
}

After i execute the code, i get an (Abort). I don't understand, I'm new to C++ so please forgive me if is a stupid error. And most of the code is complex becouse I have rules to follow, so for example if I didn't study something i can't use it here. I just want to know why the code don't want me to use the for with the vector!


Solution

  • for (int i = 0; i <= fibSeries.size(); i++)

    As @Yola mentions, you iterate one element more than you have:

    if you have the vector std::vector<char> v = {'a', 'b', 'c'};, the elements can be accessed as:

    v[0] (-> 'a')
    v[1] (-> 'b')
    v[2] (-> 'c')
    

    As a rule, your max index will be length - 1.

    for (int i = 0; i <= fibSeries.size(); i++)
    

    iterates from 0 to less then or equal to size(); that is, 0, 1, 2, 3 (4 elements, in a 3 elements vector).

    Correct code should be:

    for (int i = 0; i < fibSeries.size(); i++)
                     ^^^