循环混乱

时间:2018-11-22 03:30:18

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

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

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;

                                }

P.S。我的活动也有错误,但不要介意:D

1 个答案:

答案 0 :(得分:1)

首先,您尚未初始化任何东西,因此一切都在获取垃圾值。 首先初始化 det ,如果要输入整个数组,请运行循环。 cin >> nos [det]仅根据您的代码在垃圾索引中输入一个值。

此代码是一团糟。请修复初始化和输入,然后共享要获取的输出与所需的输出。