Having problem with loop!!

This is a discussion on Having problem with loop!! within the C++ Programming forums, part of the General Programming Boards category; I am trying to make an easy program but I have it done except it prints off extra data. Code: ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    83

    Having problem with loop!!

    I am trying to make an easy program but I have it done except it prints off extra data.
    Code:
    while (cin) {
          cout << "Enter an even number: "; 
           cin >> num; 
      if (num % 2 == 0 && num >= 0) 
         cout << "The number " << num 
               << " is a positive even number." << endl; 
                                                         
        else  
         cout << "The number " << num 
              << " is not a positive even number!" << endl; 
              
         } // end while
    But when i print it looks like this:

    Code:
    Enter an even number: 23
    The number 23 is not a positive even number!
    Enter an even number: -6
    The number -6 is not a positive even number!
    Enter an even number: 4
    The number 4 is a positive even number.
    Enter an even number:
    As you can see, I only put in 3 numbers but 4 things pop up. How can I prevent this? I am sorry that I am fairly new to this.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Code:
    while (cin) {
          cout << "Enter an even number: "; 
           cin >> num; 
      if (num % 2 == 0 && num >= 0) 
      {
         cout << "The number " << num 
               << " is a positive even number." << endl; 
         break;
      }                                                     
        else  
         cout << "The number " << num 
              << " is not a positive even number!" << endl; 
              
         } // end while

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM

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