While loop that displays no negative numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Greatness
    New Member
    • Apr 2007
    • 16

    While loop that displays no negative numbers

    How would i write a while loop that displays no negative numbers???
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by Greatness
    How would i write a while loop that displays no negative numbers???
    It's quite simple.All that u need is simple if-else statment inside the loop if exppresion is TRUE don't print anything else print that number.


    Savage

    Comment

    • Greatness
      New Member
      • Apr 2007
      • 16

      #3
      ok so like this......



      int n;
      while (n = 0)
      if (n>0)
      cin >> n;
      else (n < 0)
      cout << " Please enter another number";


      ???? lol

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by Greatness
        ok so like this......



        Code:
        int n;
        while (n = 0)
        if (n>0)
        cin >> n;
        else (n < 0)
        cout << " Please enter another number";

        ???? lol
        Not quite...

        Let's look at what your code is doing.

        Code:
        int n;
        while (n = 0)
           if (n>0)
              cin >> n;
           else (n < 0)
              cout << " Please enter another number";
        First, you declare an integer variable, n. Cool. Then you have a while loop, with a condition setting n to 0? Maybe you meant n == 0 which would loop while x is 0, but even then your loop will fail because you haven't given a value to n.

        Now, inside the loop, you test if n is greater than 0. This would be great if n had a value...but you only cin >> a value if it's greater than 0. Huh? That doesn't make sense! How can you test its value before you get it?

        Now, your else statement is incorrectly written. You cannot have else (condition) - you can only say else, or else if (condition). Also, the condition is not necessary - if a number is not greater than 0, then it must be less than 0 (or must it...what if n = 0? Consider this possibility).

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by Greatness
          ok so like this......



          int n;
          while (n = 0)
          if (n>0)
          cin >> n;
          else (n < 0)
          cout << " Please enter another number";


          ???? lol
          I do see what u want to do but this is all wrong.Let us go line by line and fix this:

          1.while(n=0)

          I do see lots of errors here.If i get the program right it's supposed to be a control of input.

          a.Couple of errors are here: while(n=0)

          solution:instea d of using number to identify when loop will break i would rather use identifer,in this case let it be i.Secound error u cant use a single = when testing expression,u must use ==.So here is fisrt part of it:

          int i=0;
          while(!i)//this is nearly same as while(i==0).

          I see more errors in loop construction part:(e.g)

          It should be this:

          while(!i)
          {
          //loop body
          }

          When u have done all this 'repairment' post so that we can move forward.

          Savage

          Comment

          • Greatness
            New Member
            • Apr 2007
            • 16

            #6
            hmm ok so let me try this

            int n;
            cout << "Enter a Number"<<endl;
            cin>> n;
            while (n)
            if (n>0)
            elseif

            Comment

            • Greatness
              New Member
              • Apr 2007
              • 16

              #7
              sorry posted that before i saw the last post

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by Greatness
                hmm ok so let me try this

                int n;
                cout << "Enter a Number"<<endl;
                cin>> n;
                while (n)
                if (n>0)
                elseif
                It's not elseif it's else if(ur expression) and u are missing those{ } compund statments in ur loop

                Savage

                Comment

                • Greatness
                  New Member
                  • Apr 2007
                  • 16

                  #9
                  ok i understand the first part please continue

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by Greatness
                    ok i understand the first part please continue
                    Well as we can see it's already continued.Do u have any Q on those new posts?


                    Savage

                    Comment

                    • nirJianWu
                      New Member
                      • Apr 2007
                      • 7

                      #11
                      Originally posted by Greatness
                      How would i write a while loop that displays no negative numbers???

                      I don't understand your requirement very much...

                      do you wanna display all the non negative numbers or you just want to display the non negative numbers you received.

                      if you wanna display all by using a while loop, just do it as follows..

                      int n = 0;
                      while(1)
                      std::cout<<n++< <std::endl;

                      but if you wanna display the non negative numbers you received, using a if-else statement is good idea.

                      Comment

                      • Greatness
                        New Member
                        • Apr 2007
                        • 16

                        #12
                        nah i'm still working on this one lol

                        so from what you have taught me today:


                        int i = 0;

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

                        while(i == 0)
                        {
                        if(i>0)
                        esle
                        }

                        Comment

                        • Greatness
                          New Member
                          • Apr 2007
                          • 16

                          #13
                          For this part of the program i want to create a while loop that will not display negative numbers.

                          Comment

                          • Savage
                            Recognized Expert Top Contributor
                            • Feb 2007
                            • 1759

                            #14
                            No,no u still have number n defined, i is just identifer.

                            int n,i;

                            //loop and it's expression

                            //loop body

                            if(n>0) i=1;
                            else i=0;

                            } //loop compound statement.

                            Note:this task can be done much more easier if u use do-while.Do u must use while?

                            Savage

                            Comment

                            • Greatness
                              New Member
                              • Apr 2007
                              • 16

                              #15
                              We can you use do-while also...
                              Can you show me that way also???

                              Comment

                              Working...