cygwin_exception :: open_stackdumpfile: дамп трассировки стека в class4.exe.stackdump - PullRequest
0 голосов
/ 01 октября 2018

main.cpp

#include <iostream>
#include <vector>
#include <cstdlib>
#include <chrono>

using namespace std;
int randNum = 0;
int digit = 0;
int length = 0;
int choice = 0;
int remainder = 0;
int counter = 0;
int origArray[5] = { };
int reverseArray[5] = { };
int temporary = 0;
int index = 0;
bool repetition = true;

int main () {
    srand(std::chrono::duration_cast<std::chrono::milliseconds>
     (std::chrono::system_clock::now().time_since_epoch()).count()%2000000000);
    // needed to autograde some test cases in Mimir

    do {
        cout << "Enter number of digits in code (3, 4 or 5): ";
        cin >> choice;
        cout << endl;
    }   while ((choice != 0) && (choice != 3) && (choice !=4) && (choice !=5));
    switch (choice) {
    case 0: {
        cout << "Enter code: ";
        cin >> digit;
        cout << endl;
            while (digit > 0) {
                remainder = digit % 10;
                digit = digit / 10;
                origArray[counter] = remainder;
                counter++;
            }// while loop that separates digits in reverse
            cout << "Enter number of digits in code: " << endl;
            cin >> length;
            for (int i = 0; i < length; i++) {
                reverseArray[i] = origArray[length - i - 1];
            }// for loop that sets the digits in the right order
            cout << "Number to guess: ";
            for (int i = 0; i < length; i++) {
                cout << reverseArray[i];
                if (i < length-1)
                    cout <<"-";
            }// for loop that outputs the digits with a dash
    }// what runs when 0 is selected
    break;
    default: {
            while(temporary < choice) {
                repetition = false;
                randNum = rand() % 10;
                for (int i = 0; i < choice; i++) {
                    if (randNum == reverseArray[i])
                        repetition = true;
                }// for loop that sees if random number is equal to inputted 
digit value array
                if (repetition == false) {
                    reverseArray[index] = randNum;
                    index = index + 1;
                    repetition = repetition + 1;
                }// for loop that populates array with random digits
            }// while loop that goes through each index of array.
            cout << "Number to guess: ";
            for (int i = 0; i < choice; i++) {
                cout << reverseArray[i];
                if (i < choice-1)
                    cout <<"-";
            }//for loop that outputs the digits with a dash
            break;
        }// what happens when 3, 4, or 5 are selected
    }//switch statement
}

Я получаю "cygwin_exception :: open_stackdumpfile: дамп трассировки стека в class4.exe.stackdump" всякий раз, когда я запускаю случай по умолчанию.Я запускаю его в затмении.

Это происходит время от времени, но я не знаю, почему это происходит только иногда.

Буду признателен за любую помощь, поскольку я не нашел полезной информации в Интернете.

Спасибо

1 Ответ

0 голосов
/ 01 октября 2018

есть исключение "вне границы" в

reverseArray[index] = randNum;
index = index + 1;
repetition = repetition + 1;

, которое можно предотвратить с помощью условия проверки диапазона

if(0 <= index && index < 5){
    reverseArray[index] = randNum;
    index = index + 1;
    repetition = repetition + 1;
}

Я не уверен, изменится ли это условиелогика вашего кода или нет, тогда, если вы введете 0, это даст последовательность чисел, но для 3, 4 или 5 это даст огромный цикл почти как бесконечный цикл

...