Thread: while loop giving me a hard time

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    9

    while loop giving me a hard time

    Hi I'm new to C and programming generally and have run into an obstacle with an easy while loop.

    I want to add the same value to the variable spoj1min until I get the remainder of division 7.

    spoj1min variable has an initial value of 17
    spoj2min variable has an inital value of 12

    could you tell me what is wrong with the below code that it doesn't increment the value by 17 for each cycle but keeps outputing 0 infinetly?

    Thanks a lot for any help

    Code:
    while(x != 7){
        x = spoj1min % spoj2min;
        spoj1min = spoj1min + spoj1min;
        printf("%d\n", spoj1min,);
    Last edited by Snowflake; 11-15-2014 at 05:15 PM.

  2. #2
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    The value that is stored in spoj1min is modified during each iteration, so the third line of your code snippet is equivalent to "spoj1min = spoj1min * 2", rather than the desired "spoj1min = spoj1min + 17".

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    First, your code is not complete. I assume that it look like this:
    Code:
    #include <stdio.h>
    
    int main (void) {
    
        int spoj1min = 17;
        int spoj2min = 12;
        int x = 0;
    
        while(x != 7) {
               x = spoj1min % spoj2min;
            spoj1min = spoj1min + spoj1min;
            printf("%d\n", spoj1min);
        }
    
        return 0;
    }
    The problem is in this line:
    Code:
    spoj1min = spoj1min + spoj1min;
    This will double the value of spoj1min in every iteration and this will result in an overflow.
    You can check this with an emergency break:

    Code:
    #include <stdio.h>
    
    int main (void) {
    
        int spoj1min = 17;
        int spoj2min = 12;
        int x = 0;
        int em_brk = 0;
    
        while(x != 7) {
               x = spoj1min % spoj2min;
            spoj1min = spoj1min + spoj1min;
            printf("%d\n", spoj1min);
            if (++em_brk > 50) break;
        }
    
        return 0;
    }
    The program will stop and you can scroll up and see was happens.
    I think you want increase spoj1min by 17 in every iteration.
    The best way is to assing a new variable (call it 'add_on') and give it the value (in your case: 17).
    In the loop add 'add_on' to 'spoj1min' in every iteration.
    Then calculate the reminder, print booth out and finish.
    If the reminder (x) is 7, the loop will stop.

    Leave the emergency break in plaece. It could be that you give numbers that never fulfilled the break contion of the loop.
    Last edited by WoodSTokk; 11-15-2014 at 06:10 PM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    I'm not going to tell you why, but show how to figure out why.
    First, if you have an infinite loop, use something else that is finite
    so you can slowly increment the loop your self to see what is happening.
    x is zero, but not for the very first few iterations.
    Then throw in some printf's to see what is happening to your variables.
    Here is the code to do so.
    After running this, you should be able to figure out why.


    Code:
     int x, y=0, spoj1min = 17, spoj2min = 12;
        while(y != 35){
            x = spoj1min % spoj2min;
            printf("x is %d\n", x);
            spoj1min = spoj1min + spoj1min;
            printf("spoj1min is %d\n", spoj1min);
            y++;
        }

  5. #5
    Registered User
    Join Date
    Nov 2014
    Posts
    9
    Hi everyone,

    thanks a lot everyone for the help, now the code looks like this:
    Code:
     addon = spoj1min;
        while(x != spoj2min - spoj2jelmin -1)
    {
        x = spoj1min % spoj2min;
        spoj1min = spoj1min + addon;
    
    }
        printf("%d.", spoj1min);
    And the issue is when the condition is met ( the value of x is 7) it runs the loop one more time giving me my desired result plus 17 (not the 187 I want but 204).
    Could you tell me why that is?

    Thanks a lot

  6. #6
    Registered User
    Join Date
    Nov 2014
    Posts
    9

    figured it out

    sorry, ignore the question figured it out by switching the addon lines with the dividing (obviously ) thanks again guys, you have been tremendously helpfull

  7. #7
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28
    Unrelated question , which is faster
    X += X;
    Or X = 2*X;

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by RagingGrim
    Unrelated question , which is faster
    X += X;
    Or X = 2*X;
    Next time post in a new thread.

    Anyway, after optimisations have been applied by the compiler, it is plausible that the resulting executable code might be exactly the same.
    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

  9. #9
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Code:
    for(spoj1min = 17; (spoj1min % spooj2min) != 7; spoj1min += 17)
    Do that instead.
    It was hard for me to manage what you actually wanted, but I guess its that ^

    PHP Code:
    spoj1min17 spoj1min 123
    spoj1min
    34 spoj1min 126
    spoj1min
    51 spoj1min 122
    spoj1min
    68 spoj1min 125
    spoj1min
    85 spoj1min 121
    spoj1min
    102 spoj1min 124
    spoj1min
    119 spoj1min 120
    spoj1min
    136 spoj1min 123
    spoj1min
    153 spoj1min 126
    spoj1min
    170 spoj1min 12
    Last edited by Al3; 11-18-2014 at 08:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-08-2012, 06:12 PM
  2. Replies: 2
    Last Post: 10-26-2011, 01:21 AM
  3. I'm having a hard time making a .exe from a .cpp
    By manugarciac in forum C++ Programming
    Replies: 10
    Last Post: 05-13-2009, 04:40 PM
  4. I am having a hard time understanding this...
    By EvilPickles in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2006, 05:26 AM