C++For循环在输入新变量时被卡住

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

本文关键字:变量 循环 输入 新变量 C++For      更新时间:2023-10-16

我刚接触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。

编辑:在仔细查看代码后,我可以看到你使用的是数字[],而没有初始化它。两个嵌套的for循环将比较数字中的所有元素。但是,如果用户只输入了2个数字,其余的数字就会被统一起来,可能会产生意想不到的结果。

此外,您不需要每次都对所有元素进行检查。只需将新输入的数字(索引为i)与以前的所有数字进行核对即可。

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

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

处理非整数输入,例如"文本"

还有一些小事:

你还应该检查负数。

您使用的是|而不是||。它会很好地工作,但||似乎更正确,因为它是逻辑OR(而|是二进制OR)。