While loop that displays no negative numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #16
    Originally posted by Greatness
    We can you use do-while also...
    Can you show me that way also???
    Offcourse,it would be like:

    Code:
    int n;
    
    do{
           //cin number
    
    
    }while(n<0);//No need for those if-else statments at all
    Savage

    Comment

    • Greatness
      New Member
      • Apr 2007
      • 16

      #17
      whenever i try to use your code i get a runtime error saying that n has not been defined

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #18
        Originally posted by Greatness
        whenever i try to use your code i get a runtime error saying that n has not been defined
        And how did u exactly implemented my code?

        PS:Post just do-while loop or while which u have used.

        Savage

        Comment

        • Greatness
          New Member
          • Apr 2007
          • 16

          #19
          well when i put the while in it say i, n have not been defined
          but with the dowhile it just says n have not been defined

          Comment

          • nirJianWu
            New Member
            • Apr 2007
            • 7

            #20
            Originally posted by Greatness


            int i = 0;

            cout<< "Please enter a number" << endl;
            cin >> i;

            while(i == 0)
            {
            if(i>0)
            esle
            }
            ur, it will print nothing...

            in your code, the code inside while loop will be executed only when i ==0 .

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #21
              A while loop that doesn't display negative numbers:
              Code:
              for(;;) cout << 42;
              kind regards,

              Jos ;-)

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #22
                Originally posted by Greatness
                well when i put the while in it say i, n have not been defined
                but with the dowhile it just says n have not been defined
                But in my code n is defined:

                int n;

                Post that part here.

                Savage

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #23
                  Originally posted by JosAH
                  A while loop that doesn't display negative numbers:
                  Code:
                  for(;;) cout << 42;
                  kind regards,

                  Jos ;-)
                  Yes offcourse.
                  How can i forget this.This is briliant.

                  He,he.

                  Good one Jos ;-)

                  How about this:

                  Code:
                  forever(;;) cout <<42;
                  Savage

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #24
                    Originally posted by JosAH
                    A while loop that doesn't display negative numbers:
                    Code:
                    for(;;) cout << 42;
                    Strictly speaking that is a for loop not a while loop, when I saw this post I had

                    Code:
                    while(1)
                        ;
                    in mind. The point these posts Greatness is that your original question (specification) is so loose (poorly specified) that many many constructs will fit what you have said although most of them are unlikely to do anything close to what you want.

                    When you post your next question please try to be a little more precise.

                    Comment

                    Working...