Thread: for loop

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    Question for loop

    I am having a problem with the loop. I have added the comments to this code as I understand it. It appears that the code should loop after the if test to see if
    the answer is yes. If the answer is yes it should go back into the loop, if no then it should exit via the return. The code runs, takes the values and displays
    them correctly, But when I enter Y or y it takes the bvalues and then exits. Why is the loop not working when the value of more is y or Y?


    #include <stdio.h>

    void main()
    {
    int start=0; /* integer for the starting number */
    int end=0; /* integer for the ending number */
    int i =0; /* increment counter to the value of &end */
    char more; /* variable to continue with the loop y||Y */




    printf(" Please enter the starting and ending values\n"); /* statement to user */
    scanf(" %d" " %d", &start, &end); /* getting the values */

    printf(" dec hex char \n"); /* printing the display header */




    for( i = start; i <= end; ++i) /* looping through the numbers adding 1 to end */
    {
    printf(" %d %x %c \n", i, i, i); /*printing the values of each type */
    }
    printf("Would you like to try some more values? (N/Y)?: \n"); /* ask for more input values */
    scanf(" %c", &more); /* look for value to be yes or no */



    printf("Please enter the values.\n"); /* ask for values */
    scanf(" %d" " %d", &start, &end); /* take input values */
    if(more == 'y' || more == 'Y' ); /* if test for yes loop */

    else
    {(more == 'n' || more == 'N' ); /* else exit program */

    return;}


    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640

    Re: for loop

    How about changing this
    for( i = start; i <= end; ++i)
    To this
    for( i = start; i <= end; i++)
    Oh and by the way, YOU USED VOID MAIN! CHANGE IT NOW!

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I did not work I get the same thing.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Just put the code in a function and return 0 or 1 (dependig on the choice of the user to continue or not):
    Code:
    #include <stdio.h>
    
    int do_something(void);
    
    int main(void)
    { 
       while(do_something());
       return 0;
    }
    
    int do_something(void)
    {
       int start=0; /* integer for the starting number */
       int end=0; /* integer for the ending number */
       int i =0; /* increment counter to the value of &end */
       char more; /* variable to continue with the loop y||Y */
    
       printf(" Please enter the starting and ending values\n"); /* statement to user */
       scanf(" %d" " %d", &start, &end); /* getting the values */
    
       printf(" dec hex char \n"); /* printing the display header */
    
       for( i = start; i <= end; ++i) /* looping through the numbers adding 1 to end */
       {
          printf(" %d %x %c \n", i, i, i); /*printing the values of each type */
       }
       printf("Would you like to try some more values? (N/Y)?: \n"); /* ask for more input values */
       scanf(" %c", &more); /* look for value to be yes or no */
    
       if(more == 'y' || more == 'Y' )
          return 1; /* continue */ 
    
       return 0; /* stop */
    }

  5. #5
    booyakasha
    Join Date
    Nov 2002
    Posts
    208

    Re: Re: for loop

    Originally posted by Travis Dane
    How about changing this

    To this

    those are equivalent

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I think that I understand a little better now. The for loop was just for
    > the iteration of the the values from start to end. Then I needed another
    > loop to encompass
    > the whole thing. I create a do while loop which held the for loop telling
    > the code to repeat if the value of more was y or Y. The if else statement
    > was just to get the value of more. to make a decision on wether to
    continue
    > or not. ANd again at the end the while code checks to see if more is yes
    > if so continue to the for loop.
    >
    > Thanks,
    > John
    >
    >
    >
    > #include <stdio.h>
    > void main()
    >
    >
    > {
    > int start=0; /* integer for the starting number */
    > int end=0; /* integer for the ending number */
    > int i =0; /* increment counter to the value of &end */
    > char more; /* variable to continue with the loop y||Y */
    >
    >
    >
    >
    > printf(" Please enter the starting and ending values\n"); /*
    > statement to user */
    > scanf(" %d" " %d", &start, &end); /* getting the
    > values */
    >
    > do
    > {
    > printf(" dec hex char \n"); /* printing the
    > display header */
    >
    >
    >
    >
    > for( i = start; i <= end; ++i) /* looping through the
    > numbers adding 1 to end */
    > {
    > printf(" %d %x %c \n", i, i, i); /*printing the
    > values of each type */
    > }
    > printf("Would you like to try some more values? (N/Y)?: \n"); /*
    ask
    > for more input values */
    > scanf(" %c", &more); /* look for value to be yes or no */
    > if(more == 'y' || more == 'Y')
    > {printf("Please enter the values.\n"); /* ask for values */
    > scanf(" %d" " %d", &start, &end);} /* take input values
    > */
    > else
    > { printf("Goodbye!");
    > return;}
    > } while(more == 'y' || more == 'Y');
    >
    > }

Popular pages Recent additions subscribe to a feed