简体   繁体   English

C++ 环路终止条件

[英]C++ Loop Termination Condition

I need to write a program which asks the user to input characters, until they input a vowel.我需要编写一个程序,要求用户输入字符,直到他们输入元音。 However, my program always sets the condition to "true" and also does not stop the loop.但是,我的程序总是将条件设置为“真”并且也不会停止循环。 Why is this happening?为什么会这样?

#include <iostream>
using namespace std;
int main ()
{
    char ch;
    bool condition=false;
    char vow[5];
    vow[0]='a';
    vow[1]='e';
    vow[2]='i';
    vow[3]='o';
    vow[4]='u';

    char arr[1000];
    do
    {   
    condition=false;
    for (int i=0;i<1000;i++)
    {
         cout<<"Enter a character:\n";
         cin>>ch;
         arr[i]=ch;

         for (int k=0;k<5;k++)
         {
            if (ch==(vow[k]))
            {
                condition=true;
            }
        }
    }
    while (condition=false);
}

As many have already said in the comments, your the main thing you are doing wrong is that you put condition=false in your while head.正如许多人在评论中已经说过的那样,您做错的主要事情是您将condition=false放在您的 while 头中。

For some reason, the program works properly after making that change, though.但是,出于某种原因,该程序在进行更改后可以正常工作。 Why is that?这是为什么?

What you are doing here is assigning false to your variable condition and afterwards compare it against != 0 which in boolean terms would mean != false .您在这里所做的是将false分配给您的变量condition ,然后将其与!= 0进行比较,在 boolean 术语中意味着!= false

So as a result you end up with something like this after compiling:因此,编译后你最终会得到这样的结果:
while((condition = false) != false) or while(false != false) while((condition = false) != false)while(false != false)
which evaluates to false and then breaks the loop.其评估结果为假,然后打破循环。 Together with your added change of using break as soon as you find a vowel, this makes your code 'work'.再加上您在找到元音后立即使用break的附加更改,这使您的代码“工作”。

Since it seems like you are new to programming here is your code with a few basic suggestions in the comments:由于您似乎是编程新手,因此您的代码在评论中提供了一些基本建议:

#include <iostream>

using namespace std;

int main ()
{
  char ch;
  bool condition = false;
  // You can initialize a primitive array in place without having to modify each value
  // individually.
  // Note however, you can not just reassign vow with this array initializer later on
  // but have to do it like you did before
  char vow[] = {'a', 'e', 'i', 'o', 'u'};
  char arr[1000];

  // To me it looks like you dont need the do...while loop around the for loop at all,
  // since you seem to want to break out of your loop the first time you find a vowel
  // anyways. Instead consider putting your condition as a seperate check into the
  // for header like this
  for (int i = 0; i < 1000 && !condition; i++) {
    cout << "Enter a character:\n";
    cin >> ch;
    arr[i] = ch;

    for (int k = 0; k < 5; k++) {
      if (ch == vow[k]) {
        condition = true;
      }
    }
  }

  // Dont forget the return statement. In your case its not really important since the
  // compiler adds return 0 by default, but for testing and such it can be very helpful
  return 0;
}

Also, using whitespaces and empty lines improves the readability of your code alot.此外,使用空格和空行可以大大提高代码的可读性。
Of course, codestyle is always a bit of personal preference but I think everyone can agree to that:D当然,codestyle 总是有点个人喜好,但我想每个人都可以同意这一点:D

do
    {   
       for (int i=0;i<1000;i++)
        {
            cout<<"Enter a character:\n";
            cin>>ch;
            arr[i]=ch;

            for (int k=0;k<5;k++)
             {
                if (ch==(vow[k]))
                 {
                  x=1;
                 }
             }

            if (x==0)
            {  
                dis[i]=arr[i]; 
            }

            if(x==1)
            {
                break;
            }   

        count=i;    
        }


    }
    while (x=0);

making these changed fixed it.使这些更改修复它。 i don't know whether this is due to replacing while (condition=false) with while(x=0) or because of adding the if conditions.我不知道这是由于将while (condition=false)替换为while(x=0)还是由于添加了if条件。 Any explanation on that would be appreciated:)对此的任何解释将不胜感激:)

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

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