如何在do-while循环中结束程序 (C++)

huangapple go评论68阅读模式
英文:

How to end program in a do-while (C++)

问题

这是我的代码。我试图让整个程序在进入do-while循环内的第二个if语句时结束。但每次运行它时,程序都崩溃。我不确定我做错了什么。

#include <iostream>
using namespace std;

int main() {
    int myData[10];
    for(int i=0;i<10;i++){
        myData[i] = 1;
        cout<<myData[i];
    }
    
    do{
        int i;
        cout<<endl<<"输入索引: ";
        cin>> i;
        
        int v;
        cout<<endl<<"输入数值: ";
        cin>>v;
        
        if(i>=0&&i<10){
            myData[i]=v;
            for(int i=0;i<10;i++){
                cout<<myData[i]<<" ";
            }
        }
        
        if (i<0||i>=10){
            cout<<"索引超出范围。退出。";
            return 0;
        }
        
    }while(1);
}
英文:

Here is my code. I am trying to get the entire program to end if it goes into the second if statement inside the do-while loop. But every time I run it, it crashes. I am not sure what I am doing wrong.

#include &lt;iostream&gt;
using namespace std;

int main() {
    int myData[10];
    for(int i=0;i&lt;10;i++){
        myData[i] = 1;
        cout&lt;&lt;myData[i];
    }
    
    do{
        int i;
        cout&lt;&lt;endl&lt;&lt;&quot;Input index: &quot;;
        cin&gt;&gt; i;
        
        int v;
        cout&lt;&lt;endl&lt;&lt;&quot;Input value: &quot;;
        cin&gt;&gt;v;
        
        if(i&gt;=0||i&lt;10){
            myData[i]=v;
            for(int i=0;i&lt;10;i++){
                cout&lt;&lt;myData[i]&lt;&lt;&quot; &quot;;
            }
        }
        
        if (i&lt;0||i&gt;=10){
            cout&lt;&lt;&quot;Index out of range. Exit.&quot;;
            return 0;
        }
        
    }while(1);
} 

答案1

得分: 4

if(i>=0||i<10){

思考一下哪些数字要么大于零,要么小于十。我相信你意识到这对所有数字都成立。你本来想写的是

if(i>=0&&i<10){

这解释了你的崩溃,你正在使用超出数组范围的索引访问myData数组。

对初学者来说,混淆&&||是非常常见的,尤其涉及否定时更容易出错。

英文:
if(i&gt;=0||i&lt;10){ 

Think about which numbers are either greater than zero or less than ten. I'm sure you realise that is true of all numbers. What you meant to write is

if(i&gt;=0&amp;&amp;i&lt;10){

This explains your crash, you are accessing the myData array with an index that is outside the array bounds.

It's very common for beginners to get &amp;&amp; and || confused especially where there is negation involved as well.

答案2

得分: 0

其他人告诉过你这个问题:

if (i >= 0 || i < 10) {

我来解释为什么会出现这个问题。你之所以遇到这个问题,是因为你没有使用任何空格。

if (i >= 0 && i < 10) {

请,拜托,拜托,拜托,为了世界上一切美好的事物,请大量使用空格。你绝对会减少bug,因为我的版本更容易阅读。而且,如果你与年长的开发人员一起工作(比如我),我们实际上能更容易地阅读你的代码。

英文:

Other people have told you the problem:

    if(i&gt;=0||i&lt;10){

I'm going to explain why you hit this. You hit it because you didn't use any whitespace.

    if (i &gt;= 0 &amp;&amp; i &lt; 10) {

Please, please, please, for all that is good in the world, use whitespace liberally. You absolutely will have fewer bugs, because my version is far easier to read. Furthermore if you work with older developers (like me) we can actually read your code far more easily.

huangapple
  • 本文由 发表于 2023年2月10日 03:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定