简体   繁体   English

循环混淆

[英]Looping Confusion

My code seems to ask twice instead of once if I want to run the code again.如果我想再次运行代码,我的代码似乎询问了两次而不是一次。 I just want it to run normally like my other cases which only ask once after I have viewed the activity and these two activities are the only ones with problems.我只是希望它像我的其他案例一样正常运行,在我查看活动后只询问一次,而这两个活动是唯一有问题的活动。 What seems to be the problem here:这里似乎有什么问题:

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;

                                }

PS my activities also has errors but dont mind it :D PS我的活动也有错误,但不要介意:D

First of all, you haven't initialized anything so everything is taking garbage values.首先,你还没有初始化任何东西,所以一切都是垃圾值。 initialize det first and if you want to input the whole array then run a loop.首先初始化det ,如果你想输入整个数组然后运行一个循环。 cin>>nos[det] only inputs one value at a garbage index according to your code. cin>>nos[det] 根据您的代码仅在垃圾索引处输入一个值。

This code is a mess.这段代码一团糟。 Kindly fix initializations and inputs, then share the output you're getting vs the output you want.请修复初始化和输入,然后共享您获得的输出与您想要的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM