我正在为我的c++类编写一个程序,我遇到了一个问题,正在寻求帮助。该计划应该模拟加州超级乐透,但不完整,因为我正试图解决我目前被困的问题。我的问题是,我有我的程序的一部分,导致应用程序挂起,不能继续进一步,我不知道为什么。这是大量的事情应该做的评论。你们能帮忙吗?
应用程序挂起在代码第151行,并以如下开头:d & lt;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;
}
bug就在这里:
for (int j = 0; j = 6; j++)
这将是一个无限循环,在这里
在循环退出条件处出现错误:
for (int j = 0; j = 6; j++)
This将永远不会退出;应该是
for (int j = 0; j < 6; j++)