C++ For-Loop 在输入新变量时卡住

C++ For-Loop Gets stuck when entering new variables

我是 C++ 的新手,正在尝试为大学项目创建彩票游戏。

我有一个for循环来检查输入的数组中没有重复的数字。当您取出代码段来生成随机数时,这绝对可以正常工作。

只要我重新添加随机数部分,for 循环就会卡住。当它试图存储第一个数字时,它会不断地告诉我我已经输入了数字。

我已附上我的所有代码,如果您不需要全部代码,我们深表歉意。

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;
//int loto[6];
int main()
{
int numbers[6];
//void rand_num(int loto[6], int count);
int loto[6]; //used to store the loto numbers
//int james = 0;
//int l,j; //used in checking any duplicated

    srand(time(0));

        for(int count=0; count<6; count++)
            {
            loto[count] = (rand()%49)+1;

            cout << loto[count] << endl;
            }



//declares the variable i to increase each time a number is entered.
//this will only go as high as 6
for(int i=0;i<6;i++)
{
    cout<<"    " << i<<" : Please enter your lottery numbers: "<<endl;
    cin>>numbers[i];
        if ((numbers[i] >= 50) | (numbers[i] == 0))
        do
        {
            {
            //checks to see if the first number entered is above 50 or = to  0 and rejects it
            cout << "The Number must be between 1-49, please select again. " << endl;
            cin >> numbers[i];
            }
        }
        while ((numbers[i] >= 50) | (numbers[i] == 0));

//----------------------------------------------------------------------------

//this section of code is a loop within a loop to check the number entered against all numbers already stored.
//makes l the same as i effectively
for(int l=0;l<6;l++)
{
    //makes j one more than l
    for(int j=l+1;j<7;j++)
    {
    if( numbers[l] == numbers[j] )
        do
        {
            {
            cout << "Number has already been chosen, please re-enter number " << endl;
            cout << endl;
            cin >>numbers[i];
                //checks the number that is re-entered is not <50 or = 0
                //if so it rejects it and asks for another as above.
                if ((numbers[i] >= 50) | (numbers[i] == 0))
                    do
                    {
                        {
                        cout << "The Number must be between 1-49, please select again. " << endl;
                        cin >> numbers[i];
                        }
                    }
                    while ((numbers[i] >= 50) | (numbers[i] == 0));
            }
        }
        while (numbers[l] == numbers[j]);
    }
}
}

//----------------------------------------------------------------------------

//this displays the numbers that have been chosen.
cout << "Your Numbers are: " << endl;
for (int i = 0; i<6; i++)
{
    cout << "  " << numbers[i];
}

return 0;
}

我不确定这是不是真正的问题,但这是一个错误。尝试更正它,看看它是否有帮助。

for(int l=0;l<6;l++)
{
    //makes j one more than l
    for(int j=l+1;j<7;j++)
    {
        if( numbers[l] == numbers[j] )

内部循环将达到 j==6,因此您将访问数组外部。外循环应以 5 为限制,内循环应以 6 为限制。

编辑: 在仔细查看您的代码后,我可以看到您正在使用 numbers[] 而未对其进行初始化。两个嵌套的 for 循环将比较数字中的所有元素。但是,如果用户只输入了 2 个数字,其余的将被单元化并可能产生意想不到的结果。

此外 - 您不需要每次都检查所有元素。只需检查新输入的数字(由 i 索引)与所有以前的数字。

最后你可能需要这样的东西:

if (!(cin >> numbers[i])) {
  cout << "Please enter numbers only." << endl;
  cin.clear();
  cin.ignore(10000,'\n');
}

处理非整数输入,例如"text"

还有一些小事:

您还应该检查负数。

您正在使用 |而不是 ||。它会工作正常,但是 ||似乎更正确,因为它是逻辑或(而 | 是二进制或)。