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

时间:2015-02-11 10:55:02

标签: c++ for-loop

我是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;
}

1 个答案:

答案 0 :(得分:1)

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

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)。