Thread: sentinel controlled do-while loop

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    sentinel controlled do-while loop

    In my code I need to have a sentinel controlled do-while loop for the number 0. I can get it to work when I use a while loop, but cant get it to work when I use a do-while.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int number, x;
      cout << "Enter an integer: ";
      cin >> number;
    
      x = 1;
      
      do
      {
        
        if (number % 2 == 0)
        cout << number << " is evener." << endl;
    
        if (number % 2 != 0)
        cout << number << " is odder." << endl;
        x++;
       
        cout << "Enter an integer: ";
        cin >> number;
        
        }while(number != 0);
    
      
      return 0;
    }
    For some reason it wont work if I enter 0 for the first input, but anytime after the first input it will terminate the program. What am I doing wrong that it will terminate when I enter 0 anytime after the first input?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Using while does the check at the beginning of the loop, whereas do-while does the check at the end of the loop. A do-while loop always executes at least once.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Ok, so since it has to execute at least once there is no way make it end on the first time? Because in the exercise directions it says "even if 0 is entered the first time".
    Last edited by theCanuck; 03-22-2011 at 10:02 AM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by theCanuck View Post
    Ok, so since it has to execute at least once there is no way make it end on the first time? Because in the exercise directions it says "even if 0 is entered the first time".
    If the exercise says "even if 0 is entered the first time [show it is an even number instead of exiting]" it is correct.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by kmdv View Post
    If the exercise says "even if 0 is entered the first time [show it is an even number instead of exiting]" it is correct.
    It says even if the number is 0 for the first entry, it should still exit.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't ask for an integer from two different places. You've got code duplication. You only need that one one place, just inside the top of the loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by iMalc View Post
    Don't ask for an integer from two different places. You've got code duplication. You only need that one one place, just inside the top of the loop.
    Ok thanks. That almost fixes it. It prints out that 0 in even and then ends the program. How do I make it so that it just ends it without printing that 0 is even first?

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    cout << "Enter an integer: ";
    cin >> number;
    while(number != 0)
    {
      if (number % 2 == 0)
        cout << number << " is even." << endl;
      else
        cout << number << " is odd." << endl;
      cout << "Enter an integer: ";
      cin >> number;
    }

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by kmdv View Post
    Code:
    cout << "Enter an integer: ";
    cin >> number;
    while(number != 0)
    {
      if (number % 2 == 0)
        cout << number << " is even." << endl;
      else
        cout << number << " is odd." << endl;
      cout << "Enter an integer: ";
      cin >> number;
    }
    I know how to do it using a while loop, but I must use a do-while loop.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    In that case you'd need to add an extra number==0 check at the start of the loop and break if true. Not exactly the cleanest way of doing it but then again neither is using do-while where while is more appropriate.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    As you can see, the while loop results in duplicated code again.

    I would add a break statement in the middle of the loop, and consider using a for(;.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM