Simple error i can't find /code

This is a discussion on Simple error i can't find /code within the C++ Programming forums, part of the General Programming Boards category; This should print 1 thru 4 and 6 thru 10 what have I mistyped? Code: #include <iostream> using std::cout; using ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    Simple error i can't find /code

    This should print 1 thru 4 and 6 thru 10 what have I mistyped?

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
       for ( count = 1; count < 10; count++ ) // loop 10 times
       { 
          if ( count = 5 ) // if count is 5,
             break;      // skip remaining code in loop
    
          cout << count << " ";
       } // end for
    
       cout << "Skipped printing 5" << endl;
       return 0; // indicate successful termination
    } // end main

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Code:
    if ( count = 5 )
    should be
    Code:
    if ( count == 5 )

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    You've got three problems, first off you mistyped the equality operator:

    This:
    Code:
    if ( count = 5 ) // if count is 5,
    Should be "==" for comparing two values:
    Code:
    if ( count == 5 ) // if count is 5,
    Second, you're misusing the "break" operator. The "break" operator will completely exit the inner-most loop-- meaning that once it hits "5", the loop will no longer run.

    What you're instead looking for is "continue", which will skip the rest of the current iteration of the loop but still finish out the loop.

    Lastly, your loop will only iterate 9 times (1 through 9). To make it go 1 through 10, you should modify the second parameter of your for loop to read "count <= 10".

    Final code (untested):

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
       int count;
       for ( count = 1; count <= 10; count++ ) // loop 10 times
       { 
          if ( count == 5 ) // if count is 5,
             continue;      // skip remaining code in loop
    
          cout << count << " ";
       } // end for
    
       cout << "Skipped printing 5" << endl;
       return 0; // indicate successful termination
    } // end main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  2. Replies: 18
    Last Post: 05-26-2008, 11:23 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM

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