newbie question

This is a discussion on newbie question within the C++ Programming forums, part of the General Programming Boards category; Here's my body: Code: { int i; int loopcount; loopcount = 5; { while (loopcount-- > 0); cout << "Please ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    15

    newbie question

    Here's my body:

    Code:
    {
     int i;
     int loopcount;
     loopcount = 5;
      {
         while (loopcount-- > 0);
         cout << "Please insert a number.\n";
         cin >> i;
             {
             if (i % 2 == 0)
             cout << i << " is even.\n";
         
             else
             cout << i << " is odd.\n";
             }
      }
     cout << "Alright that's 5, come back later for more.\n";  
     system("PAUSE");
     return 0;
    }
    Here's what it produces:

    Please insert a number.
    2
    2 is even.
    Alright that's 5, come back later for more.
    Press any key to continue . . .

    What I want it to do:

    I 'm trying to making it loop 5 times before quitting but it seems to be ignoring my while command. What am I missing here?

  2. #2
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    I think it's because your decrementing it in the wrong place.

    Try this:

    Code:
    {
     int i;
     int loopcount;
     loopcount = 5;
      {
         while (loopcount > 0, loopcount--);
         cout << "Please insert a number.\n";
         cin >> i;
             {
             if (i &#37; 2 == 0)
             cout << i << " is even.\n";
         
             else
             cout << i << " is odd.\n";
             }
      }
     cout << "Alright that's 5, come back later for more.\n";  
     cin.get();
     return 0;
    }
    I'm not sure this will work, i'm still a newby myself, and also try to use cin.get(); instead of system("PAUSE");

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    This:

    Code:
    while (loopcount-- > 0);
    Is equivalent to:

    Code:
    while(loopcount-- > 0)
    {
    	;
    }
    That's obviously silly what for you want. Don't put a semicolon after a while loop condition unless you know what you're doing and why you need to do it. Put braces around the statements you want to be a part of the loop.

    Code:
    while (loopcount > 0, loopcount--);
    The above makes even less sense, but I think you're trying to form a for loop. Either way, his loop should work.

    What I would rather do is use a for loop like this:

    Code:
    int i;
    ...
    for(i=0;i<5;i++)
    {
    	// loop body here....
    }

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    while (loopcount > 0, loopcount--);
    What's after the closing bracket and before the semicolon is all the loop body (nothing). All that follows is outside the loop.

    What you meant to be in the loop body, needs to go inside curly braces.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    while (loopcount > 0, loopcount--)
    this compares loopcount to 0, then throws that result away, and then stores loopcount in a temp variable, then decrements loopcount, then evaluates the temp variable in the while loop. So while it will iterate five times, it's probably not what you wanted.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
      {
         while (loopcount-- > 0);
    The error is simply that you got these the wrong way round. It's supposed to be
    Code:
    while(condition) {
      // body
    }
    Note: no semicolon after the while.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    15

    Thumbs up

    Thank you guys very much, you all are true friends in the world of programming. Here's my final working result.

    Code:
    {
     int i;
     int loopcount;
     loopcount = 5;
      {
         while (loopcount > 0,loopcount--)
         {
         cout << "Please insert a number.\n";
         cin >> i;
             {
             if (i % 2 == 0)
             cout << i << " is even.\n";
         
             else
             cout << i << " is odd.\n";
             }
         }    
      }
     cout << "Alright that's 5, come back later for more.\n";  
     system("PAUSE");
     return 0;
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> Here's my final working result.
    Now that it's working, try to go back and understand some of the advice given in the thread on how to make it better.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yes, and for situations such as this in the future you will find that for() loops are much easier to implement and are also much more efficient and useful.

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The operator , returns the value of the last statement so
    loopcount > 0,loopcount--
    returns the result of
    loopcount--
    It n=means - you can safely delete all you have before , and the effect will be the same

    For the loop with known number of iterations for loop suits better.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21