vendredi 29 décembre 2017

C++ Program stops Debugging "randomly" no Error

I created a program that generates Random Number between 1 and 12, with several restrictions. the function counter12 counts the steps between the last number and the current one. the function isValid checks if the random Number is valid with my parameters. I created a while(1) loop for endless test, to check the counters. I run this, it runs several (randomly) times, but then stops before printing the counter (zfs_ges). Here is my Code, it would be really nice if you can help me. With "It stops" i mean the debugger (I am using VS2017) with f10 and f11 runs throuugh, but just stops debugging, i cant go any step further. Maybe it is Time related?

// mex1.1.cpp: Definiert den Einstiegspunkt für die Konsolenanwendung.
//

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include <time.h>

bool isValid(int zfz, int zfz_v)
{
    if (abs(zfz - zfz_v) < 5 || abs(zfz - zfz_v) > 7) {
        return true;
    }
    return false;
}

int counter12(int zfz, int zfz_v)
{
    int counter = 0;
    if (zfz < zfz_v) // neue Zahl kleiner als alte Zahl
    {
        while (zfz_v != 12)
        {
            zfz_v++;
            counter++;
        }
        while (zfz != 0)
        {
            zfz--;
            counter++;
        }
        if (counter > 6)
        {
            counter = abs(counter - 12);
        }
        return counter;
    }
    else if (zfz > zfz_v) // neue Zahl größer als alte Zahl
    {
        while (zfz != 12)
        {
            zfz++;
            counter++;
        }
        while (zfz_v != 0)
        {
            zfz_v--;
            counter++;
        }
        if (counter > 6)
        {
            counter = abs(counter - 12);
        }
        return counter;
    }
    else
    {
        return 0;
    }
}

int main()
{
    while (1)
    {
        srand((unsigned)time(NULL));
        int e = 55;
        int mai;
        int zfz, zfz_v, zfz_ges, temp, liste[12];
        float d;
        mai = 12;
        zfz_v = 1;
        zfz_ges = 0;
        temp = 0;
        d = 0;
        for (size_t i = 0; i < mai; i++)
        {
            liste[i] = i + 1;
        }

        while (temp < 12)
        {
            zfz = rand() % 12 + 1;
            if (isValid(zfz, zfz_v)) {
                for (size_t i = 0; i < mai; i++)
                {
                    if (zfz == liste[i])
                    {
                        liste[i] = 0;
                        temp++;
                        zfz_ges += counter12(zfz, zfz_v);
                        printf("%d\n", zfz);
                        zfz_v = zfz;
                        i = 12;
                    }
                }
            }
        }
        printf("%d\n", zfz_ges - 1);

        e--;
    }
}




Aucun commentaire:

Enregistrer un commentaire