breaking out of a loop problem

This is a discussion on breaking out of a loop problem within the C Programming forums, part of the General Programming Boards category; So my homework is: 1. Your program will accept a sequence of positive integers (no greater than 2000000000, though your ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    breaking out of a loop problem

    So my homework is:

    1. Your program will accept a sequence of positive integers (no greater than 2000000000, though your program does not need to check) until it sees an input of 100 for the second time. It will then print the number of odd numbers, even numbers and multiples of 3 entered (including 100s). Example follows:
    Enter a sequence of integers (second 100 to quit)
    25
    36
    100
    20
    98
    200
    3
    86
    100

    Number of odd numbers is 2
    Number of even numbers is 7
    Number of multiples of 3 is 2



    This is what I got but its not turning out the way I want to. The only part Im stuck on is how to break the loop after "the second 100" and for some reason my "mutiple of three count is always 0.

    please help. Thanks in advance
    .................................................. ..
    .................................................. ...
    ------------------------------------------
    ------------------------------------------


    hey guys, thanks for all the suggestions. i managed to get the program to break out of the loop after the "second 100." Everything seems to work fine but now my even number(which includes the 100s) is incorrect by one(ie. 8 instead of 7)

    Heres my new code.

    Code:
    #include <stdio.h>
    
    int main()
    {
            int num;
            int oddcount = 0;
            int evencount = 0;
            int hundredcount = 0;
            int multiplethreecount = 0;
    
            printf("Enter a sequence of integers(second 100 to quit)\n");
            scanf("%d", &num);
            while (1) {
                    if (num == 100) {
                            hundredcount++;
                            if (hundredcount == 2)
                                    break;
                    }
                    if (num%2 == 0) {
                            evencount++;
                    }
                    if (num%3 == 0)
                            multiplethreecount++;
                    if (num%2 != 0)
                            oddcount++;
                    scanf("%d", &num);
            }
    
            evencount += hundredcount;
    
            printf("Number of odd numbers is %d \n", oddcount);
            printf("Number of even numbers is %d \n", evencount);
            printf("Number of multiples of 3 is %d \n", multiplethreecount);
            return 0;
    }
    Last edited by dbzx; 04-03-2009 at 03:06 AM.

  2. #2
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    You will want to move your num % 3 conditional up the list. Since you are checking if the number is even or odd first, it will never get there.

    Also, you can use the keyword "break" to break out of a loop.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    an odd no or an even no can be a multiple of three.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Kudose View Post
    You will want to move your num % 3 conditional up the list. Since you are checking if the number is even or odd first, it will never get there.

    Also, you can use the keyword "break" to break out of a loop.
    If he does that, and the number is a multiple of 3, the code won't check if the number is even or odd


    my suggestion is to change the else-if statements into if statements

    you aren't updating the x_count inside your program, and it looks like the loop will only execute when the list begins with 100
    remove the first while loop and place an if (num == 100) x_count++; inside the loop
    Last edited by ಠ_ಠ; 04-03-2009 at 12:31 AM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by ಠ_ಠ View Post
    If he does that, and the number is a multiple of 3, the code won't check if the number is even or odd


    my suggestion is to change the else-if statements into if statements

    you aren't updating the x_count inside your program, and it looks like the loop will only execute when the list begins with 100
    remove the first while loop and place an if (num == 100) x_count++; inside the loop
    Yep, my bad.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems that a do ... while loop would be appropriate:

    Code:
    hundreds_count = 0;
    
    do {
        get input
        logic
        if (x == 100) ++hundreds_count;
    } while (hundreds_count < 2);
    Or alternatively a do loop in a for loop:

    Code:
    for (i = 0; i != 2; ++i) {
        do {
            get input
            logic
        } while (x != 100);
    }
    Last edited by anon; 04-03-2009 at 02:07 AM.
    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).

  7. #7
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I wonder why break is so often seen as such a terrible monster.
    Sometimes it is handy, and allows you to avoid strange looking workarounds, which are actually way harder to interpret than using break. In this case, when we use break, things will look almost like pseudocode - no need to introduce loops inside loops etc. (Although anaon's do-while looks good too - but I perssonally am not used to do-while).

    I would write
    Code:
    while(1)   //repeat
    {
           scan...   //read value
           different %checks + increasing a variable  //store statistics
           if(100==scanned)    //check if 100 was entered
                   hundreds++;
           if(hundreds>=2)     //if two 100's is entered
               break;                //stop repeating.
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by Maz
    Although anaon's do-while looks good too - but I perssonally am not used to do-while
    A do while loop is exactly the appropriate loop to replace your own example with. I would rather reserve controlled infinite loops where the loop condition is more complex and/or some action needs to be taken when the loop condition is false and I do not think that writing a function for it is necessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. 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. fgets and breaking out of while loop
    By pollypocket4eva in forum C Programming
    Replies: 25
    Last Post: 01-05-2009, 05:50 PM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. while loop problem
    By tortan in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2006, 11:11 AM

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