Thread: loops not working, dont know why

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    3

    Post loops not working, dont know why

    hey y'all, right now my code is returning the number of scanned days for the second print line instead of starting at 1, then print line 2 is running an infinite loop. I'm not understanding why this is happening; any suggestions would be much appreciated. I'm very very new to C.

    int days, success, adjustments, simulations, day, trial=0;

    Code:
        int i,j;
        printf("How many days did we use the simulator?\n");
            scanf("%d", &days);
        for (i=0; i=days;i++){
            printf("How many simulations did we run on day %d?\n", i);
                scanf("%d", &simulations);
            for(j=0; j=simulations; j++)
                printf("How many adjustments were needed for run #%d?\n", simulations);
                    scanf("%d", &adjustments);}

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    690
    Quote Originally Posted by cgrady3 View Post
    hey y'all, right now my code is returning the number of scanned days for the second print line instead of starting at 1, then print line 2 is running an infinite loop. I'm not understanding why this is happening; any suggestions would be much appreciated. I'm very very new to C.

    int days, success, adjustments, simulations, day, trial=0;

    Code:
        int i,j;
        printf("How many days did we use the simulator?\n");
            scanf("%d", &days);
        for (i=0; i=days;i++){
            printf("How many simulations did we run on day %d?\n", i);
                scanf("%d", &simulations);
            for(j=0; j=simulations; j++)
                printf("How many adjustments were needed for run #%d?\n", simulations);
                    scanf("%d", &adjustments);}
    You are doing assignments not tests for equivalencies! '==', NOT '='!

    Also, you fail to test if the scanf() calls were successful! What if I typed "abc" in response to one of the prompts?

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    230
    > for (i=0; i = days; i++)

    Your test condition is an assignment and not a test condition as pointed out by @rstanley already.

    Perhaps what you want is this

    Code:
    for (i = 0; i < days; i++)
    {
        //...
    
        for (j = 0; j < simulations; j++)
        {
             //...
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with loops
    By ssquared in forum C Programming
    Replies: 0
    Last Post: 06-03-2015, 10:10 PM
  2. loops dont print on each indivual interation?
    By DMD85 in forum C Programming
    Replies: 1
    Last Post: 02-03-2013, 11:59 PM
  3. My loops not working and dont know what to do
    By ziggy786 in forum C Programming
    Replies: 7
    Last Post: 12-31-2012, 03:41 PM
  4. why scanf() dont working?
    By RAJJUMOTE in forum C Programming
    Replies: 10
    Last Post: 12-16-2007, 10:29 PM
  5. I dont get this working on gcc it works fine in windows
    By wise_ron in forum C Programming
    Replies: 8
    Last Post: 05-08-2006, 05:33 AM

Tags for this Thread