0

Why is my while loop not ending? It is giving the correct result, but it runs in an infinite loop. Is it due to operator precedence?

void equate()
{
    int i = 0, n = 0;
    while (((a[i] != '\0') && (b[i] != '\0')) || (n != 1))
    {
        if (a[i] > b[i])
        {
            std::cout << "\n" << a << " string is greater";
            n = 1;
        }
        else if (a[i] < b[i])
        {
            std::cout << "\n" << b << " string is greater";
            n = 1;
        }
        else
            i++;
    }
    if (n == 0)
    {
        std::cout << "\n" << "Both strings " << a << " and " << b << " are equal";

    }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel May 20 '21 at 21:20
  • 3
    Unrelated: many loop and `if` problems can be traced back to poor code indentation practices. If you apply a regular indentation style you'll find whole families of bugs become immediately apparent if not impossible. – user4581301 May 20 '21 at 21:22
  • 1
    Thanks Danny. That's a lot easier on the brain. – user4581301 May 20 '21 at 21:23
  • Note instead of `n`, consider using a `break` statement to prematurely exit the loop. – user4581301 May 20 '21 at 21:25
  • Generally, you should end outputs with a newline to maximize the chance that they'll appear in a timely manner. Printing a newline at the start and not at the end is generally not a good idea. Consider using `<< endl;` to end the output — but simply printing `<< "\n";` is often sufficient (note the semicolons!). Using `endl` does have performance implications. – Jonathan Leffler May 20 '21 at 21:35
  • *"Is it due to operator precedence?"* -- this is your hypothesis. You can improve your question by showing an experiment to test your hypothesis. For example, if `a` is `"A"` and `b` is `"B"`, what do you believe will cause your loop to end? What are the values of the relevant variables (`i`, `a[i]`, `b[i]`, and `n`) at that point, and how would you evaluate your conditional by hand? – JaMiT May 21 '21 at 01:10

1 Answers1

3

Within these if statements

   if (a[i] > b[i])
   {
       std::cout << "\n" << a << " string is greater";
       n = 1;
   }
   else if (a[i] < b[i])
   {
       std::cout << "\n" << b << " string is greater";
       n = 1;
   }

the index i is not being incremented. So the first subexpression in the condition of the while loop

((a[i] != '\0') && (b[i] != '\0'))

is always evaluates to true if the control was passed to these if statements. As a result you will have an infinite loop.

At least rewrite the condition like

while ( a[i] != '\0' && b[i] != '\0' && n != 1 )

Though the result of the loop (of the comparison of two strings) can be incorrect in the case when lengths of compared strings are unequal each other.

That is, it is not enough only to check whether n is equal to 0 as you are doing in this statement.

   if (n == 0)
   {
       std::cout << "\n" << "Both strings " << a << " and " << b << " are equal"; 
   }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335