【发布时间】: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