C++ 循环混乱
如果我想再次运行代码,我的代码似乎会询问两次而不是一次。我只想让它像我的其他案例一样正常运行,在我查看活动后只询问一次,这两个活动是唯一有问题的。这里似乎有什么问题:C++ 循环混乱,c++,for-loop,while-loop,switch-statement,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<&l
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;
}
案例5:
cout首先,您还没有初始化任何内容,所以所有内容都是垃圾值。
首先初始化det,如果要输入整个数组,则运行循环。cin>>nos[det]根据您的代码,在垃圾索引处只输入一个值
这段代码乱七八糟。请修复初始化和输入,然后共享您获得的输出与您想要的输出。将此代码复制到新项目中,并将其转换为新项目的main
调用的函数。这将使您在搜索bug时更容易进行实验,并使您更接近于在发布帖子时所需的元素。声明为int-nos[10]
的数组有十个元素,索引为0到9<代码>编号[10]
通过访问超出范围的索引,显示未定义的行为。此外,在初始化det
之前使用nos[det]
,这是未定义行为的另一个实例。