Menu

Why won't this definite while loop work?

2009-02-04
2012-09-26
  • Andrew Charbonneau

    include <stdio.h>

    int main()
    { int another = 1;
    while(another = 1)
    printf("Hello\n")'
    printf("Press 1 to recieve another greeting or zero to terminate.\n");
    scanf("%f",&another);
    }
    system("pause");
    return(0);

    it keeps repeating even if I press 0.!?!?

    Thanks!

     
    • cpns

      cpns - 2009-02-04

      What is a "definite while loop"?

       
      • Wayne Keen

        Wayne Keen - 2009-02-04

        definitely going to loop forever?

        ;)

        Wayne

         
    • Musa

      Musa - 2009-02-04

      %f is to read floats
      %i-integer
      %d-decimal integer
      http://cplusplus.com/reference/clibrary/cstdio/scanf.html

       
    • Wayne Keen

      Wayne Keen - 2009-02-04

      Note also that

      while (another = 1)

      Is probably NOT doing what you think it is. = is the assignment
      operator, not the comparison operator, which is ==

      Your line of code is putting 1 in the variable another, and if it does it successfully, returns true

      Consider what this does instead:

      while (another == 1)

      Don't feel too bad, this is probably my most often done mistake.

      Makes me mis Ada, where = is the comparison operator, and := is the assignment operator.

      Wayne

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.