C ++应用程序挂彩票

时间:2015-10-25 22:03:11

标签: c++ windows

我遇到了一个程序的问题我试图为我的C ++课程写作并正在寻求帮助。该程序应该模拟加利福尼亚超级乐透,但不完整,因为我试图解决我目前所困扰的问题。我的问题是我的程序部分导致应用程序挂起而不能继续进行,我无法弄清楚原因。这是对应该做的事情的评论。你能帮忙吗?

应用程序在代码行151上挂起,并以此开头:for(int d = 0; d< totalTickets; d ++)

因为这是技术上的功课,我不是在寻找给予我的答案,而是在朝着正确的方向努力。提前感谢您提供的所有帮助!代码如下:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <math.h>
#include <array>

using namespace std;

// Define functions to be seen by main
int lottoPlayedHere();

// Main function
int main()
{
    lottoPlayedHere();
}

// Generates the tickets for the game and compares all values.
int lottoPlayedHere()
{
    unsigned int randSeed = time(0);
    int difference, randomValue, lowerLimit, upperLimit;
    int winningTicket[6];

    // Ask user for lower and upper limits to the tickets generated
    cout << "Hello.  Please enter the lower limit for the number of tickets to" << endl;
    cout << "generate for this week's super lotto plus." << endl << endl;
    cin >> lowerLimit;

    cout << endl << "Please enter the upper limit for the number of tickets to generate." << endl << endl;
    cin >> upperLimit;

    // Seed the RNG
    srand(randSeed);

    // Do math to determine how many tickets to pull
    difference = upperLimit - lowerLimit;
    randomValue = rand() % difference;
    randomValue += lowerLimit;

    // Fill winningTicket with values
    for (int i = 0; i < 6; i++)
    {
        winningTicket[i] = 1 + rand() % 47;

        if (i == 5)
        {
            winningTicket[5] = 1 + rand() % 27;
        }
    }

    // This is here to force two elements to be equal so that the program will close 
    // if there is a duplicate value on the lotto ticket in the first five positions.
    // winningTicket[0] = winningTicket[1];

    // DEBUGGING PURPOSES; 
    /* cout << lowerLimit << " " << upperLimit << " " << difference << " " << randomValue << endl;
    for (int j = 0; j < 6; j++)
    {
        cout << winningTicket[j] << endl;
    } */


    // Cleaner code than below, more lines
    for (int a = 0; a < 5; a++)
    {
        for (int b = 0; b < 5; b++)
        {
            if (a != b)
            {
                if (winningTicket[a] == winningTicket[b])
                {
                    cout << endl << "The winning ticket includes duplicate values across the first five elements.\nThe program will now close." << endl;
                    exit(0);
                }
            }
        }
    }

    // Long code, less lines
    /* if (winningTicket[0] == winningTicket[1] || winningTicket[0] == winningTicket[2] || winningTicket[0] == winningTicket[3] || winningTicket[0] == winningTicket[4] ||
    winningTicket[1] == winningTicket[2] || winningTicket[1] == winningTicket[3] || winningTicket[1] == winningTicket[4] || winningTicket[2] == winningTicket[3] ||
    winningTicket[2] == winningTicket[4] || winningTicket[3] == winningTicket[4])
    {
        cout << "The winning ticket includes duplicate values across the first five elements.\nThe program will now close." << endl;
        exit(0);
    } */

    // Variables for pulled tickets
    int totalTickets = randomValue, counter, mega = 0, oneMega = 0, twoMega = 0, three = 0, threeMega = 0, four = 0, fourMega = 0, five = 0, fiveMega = 0;
    bool megaValue;
    int pulledTicket[6];

    cout  << endl << "The program will now pull the tickets for this week's superlotto." << endl;

    // Pulls tickets depending on min/max values given.  This is where the application hangs.
    for (int d = 0; d < totalTickets; d++)
    {
        for (int j = 0; j = 6; j++)
        {
            pulledTicket[j] = 1 + rand() % 47;

            if (j == 5)
            {
                pulledTicket[5] = 1 + rand() % 27;
            }
        }

        // Makes sure the pulled ticket doesn't have duplicate values.  Because I'm pulling so many,
        // this probably isn't needed and should be a break rather than an exit.
        // Perhaps I need to make the pulledTicket loop it's own function?
        for (int k = 0; k < 5; k++)
        {
            for (int l = 0; l < 5; l++)
            {
                if (pulledTicket[k] == pulledTicket[l])
                {
                    cout << endl << "The pulled ticket includes duplicate values across the first five elements.\nThe program will now close." << endl;
                    // exit(0);
                    break;
                }
            }
        }

        // Compares winningTicket to pulledTicket
        for (int c = 0; c < 6; c++)
        {
            counter = 0;
            if (winningTicket[c] == pulledTicket[c])
            {
                counter++;

                // If mega number is equal to pulled ticket, set megaValue to true and increment.
                if (winningTicket[5] == pulledTicket[5])
                {
                    megaValue = true;
                }
            }
        }

        // Categorize the values.
        if (counter == 0 && megaValue == true)
        {
            mega++;
        }
        if (counter == 1 && megaValue == true)
        {
            oneMega++;
        }
        if (counter == 2 && megaValue == true)
        {
            twoMega++;
        }
        if (counter == 3 && megaValue == false)
        {
            three++;
        }
        if (counter == 3 && megaValue == true)
        {
            threeMega++;
        }
        if (counter == 4 && megaValue == false)
        {
            four++;
        }
        if (counter == 4 && megaValue == true)
        {
            fourMega++;
        }
        if (counter == 5 && megaValue == false)
        {
            five++;
        }
        if (counter == 5 && megaValue == true)
        {
            fiveMega++;
        }
    }

    // Print out the winning ticket.
    cout << winningTicket << endl;

    return randomValue;
}

2 个答案:

答案 0 :(得分:1)

错误就在这里:

for (int j = 0; j = 6; j++)

这将是一个无限循环,在这里。

答案 1 :(得分:0)

循环的退出条件出错:

for (int j = 0; j = 6; j++)

这永远不会退出;它应该是

for (int j = 0; j < 6; j++)