【问题标题】:Looping Confusion循环混乱
【发布时间】:2019-04-24 16:41:35
【问题描述】:

如果我想再次运行代码,我的代码似乎询问了两次而不是一次。我只是希望它像我的其他案例一样正常运行,在我查看活动后只询问一次,而这两个活动是唯一有问题的活动。这里似乎有什么问题:

case 5:
cout<<"Here are the list of activities in Activity 5:" << endl;
cout<<"[5.1]Determining a Number within the Array" << endl;
cout<<"[5.2]Determining the Highest and Lowest integer" << endl;
cout<<"[5.3]Reversed Array" << endl;
cin >> choice;
system("CLS");

        if(choice == 5.1){
            counter +=1;
            int nos[10];
            int det;
            cout <<"Note: Do not input any decimal numbers." << endl;
                for(int array = 1; array < 11; array++){

                    cout << "Input integers 1-10 only. [" << array << "]";
                    cin >> nos[det];
                                        }
                    cout << "Type in 1 integer value only.[" << det << "]";
                    cin >> det; 

                    if(det >= nos[1] || det <= nos[10]){
                        cout << "The value is within the scope of the array.";
                                        }
                    else{
                        cout << "The value is not within the scope of the array.";
                                        }
        system ("PAUSE");
        system ("CLS");
            cout << "Do you want to run the program again? (y/n)" << endl;
            cin >> choose;
                        }

        else if(choice == 5.2){
            counter +=1;
        cout <<"Note: Do not input any decimal numbers." << endl;
        cout <<"Enter your integers." << endl;
        int nos[10];
        int put;
                for(int rep = 1; rep < 11; rep++){

                    cout <<"[" << rep << "]";
                    cin >> nos[rep];
                            }
                        int highnos = nos[1];
                        int lownos = nos[1];

                for(int rep = 1; rep < 11; rep++){
                if(nos[rep] > highnos){
                    highnos = nos[rep];
                                                    }           
                            }                           
                    cout << "Your highest integer is:" << highnos << endl;

                for(int rep = 1; rep > 11; rep++){
                if(nos[rep] < lownos){
                lownos = nos[rep];
                                    }
                                                }
        cout <<"Your lowest integer is:"<< lownos << endl;
            system ("PAUSE");
            system ("CLS");
            cout << "Do you want to run the program again? (y/n)" << endl;
            cin >> choose;

                                }

附:我的活动也有错误,但不要介意:D

【问题讨论】:

  • 将这段代码复制到一个新项目中,并将其转换为新项目的main调用的函数。这将使您在寻找错误时更容易进行试验,并使您更接近拥有发布帖子时所需的minimal reproducible example
  • 声明为 int nos[10] 的数组有十个元素,索引从 0 到 9。nos[10] 通过访问越界索引表现出未定义的行为。此外,您在 det 初始化之前使用 nos[det] - 未定义行为的另一个实例。

标签: c++ for-loop while-loop switch-statement


【解决方案1】:

首先,您还没有初始化任何东西,所以一切都在使用垃圾值。 首先初始化det,如果你想输入整个数组,然后运行一个循环。 cin>>nos[det] 根据您的代码,只在垃圾索引处输入一个值。

这段代码一团糟。请修复初始化和输入,然后分享你得到的输出和你想要的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-25
    • 2015-02-10
    • 2016-03-11
    • 1970-01-01
    • 2014-05-25
    • 2016-07-07
    • 2013-03-18
    相关资源
    最近更新 更多