Thread: Would like Help with Continue and Break statements

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    1

    Would like Help with Continue and Break statements

    My code below accepts input up to 5 numbers only. It only counts the odd numbers then sums up the total. What I need to add is a continue statement that will skip and not count the number 7, if entered. I need to take another copy of that code but instead of putting in the continue statement, use a break that will exit the program if the number 7 is entered. I also must use a for loop which I have below. (Any help is appreciated)

    I have tried inserting:
    Code:
    if(counter==7)
    continue;
          and
    if(counter==7)
    break;

    Code:
        #include <iostream>
        using namespace std;
    
            int main() {
    
        int sum = 0;
        int num[5];
    
        for (int counter = 0; counter < 5; counter++) {
    
        cout << "Enter a Number: ";
        cin >> num[counter];
    
        if (num[counter] % 2 != 0)
            sum = sum + num[counter];
            
        }
    
        cout << "Sum of Odd Numbers is: " << sum << endl; 
    
        system("pause");
        return 0;
            }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you really need to indent your code properly. This was not an issue before you started using if statements and loops, but now that you have started on these, you need proper indentation so that the flow of control through your code becomes easier to see. For example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
        int sum = 0;
        int num[5];
    
        for (int counter = 0; counter < 5; counter++) {
            cout << "Enter a Number: ";
            cin >> num[counter];
    
            if (num[counter] % 2 != 0)
                sum = sum + num[counter];
        }
    
        cout << "Sum of Odd Numbers is: " << sum << endl; 
    
        system("pause");
        return 0;
    }
    Quote Originally Posted by drewstar13
    What I need to add is a continue statement that will skip and not count the number 7
    (...)
    I have tried inserting:
    Code:
    if(counter==7)
    continue;
    It sounds like what you tried should work. Perhaps you should actually insert it into your code and then tell us how does it not work, assuming that it actually does not work.

    Quote Originally Posted by drewstar13
    I need to take another copy of that code but instead of putting in the continue statement, use a break that will exit the program if the number 7 is entered.
    Likewise, it sounds like what you tried should work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Nested FOR Loops & Break / Continue
    By calebsmith in forum C Programming
    Replies: 1
    Last Post: 04-13-2015, 08:47 PM
  2. Trying to understand Break and Continue w/ sample code
    By matthayzon89 in forum C Programming
    Replies: 8
    Last Post: 04-25-2010, 01:15 PM
  3. Can i use break and continue at the same time?
    By noobprogrammer in forum C Programming
    Replies: 4
    Last Post: 12-15-2009, 11:15 AM
  4. illegal break and continue?
    By Bajanine in forum C Programming
    Replies: 6
    Last Post: 11-06-2006, 01:14 AM
  5. continue and break!
    By Nectron in forum C++ Programming
    Replies: 4
    Last Post: 07-04-2003, 09:01 PM

Tags for this Thread