Settiing a loop to skip by number input

This is a discussion on Settiing a loop to skip by number input within the C Programming forums, part of the General Programming Boards category; I'm a little confused, I know how to get input form a user but how do I use it as ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Settiing a loop to skip by number input

    I'm a little confused, I know how to get input form a user but how do I use it as a variable for a loop?
    What I need to is ask for a number and if its 6 it will output 6, 12, 18..and so on and if its 7 it will output 7, 14, 21...etc or whatever number they put in, all the way to 150.
    I've tried a few things and normally would ask but I'm getting frustrated,
    any help you can offer would be appreciated.
    Thanks,
    extro

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Lets see your code.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    68
    This has a few obvious errors but my hope is that I should be using a while statement.
    The program asks if I were to open x mailboxes out of 150 which mailboxes are open?

    Code:
    #include <stdio.h>
    
    main ()
    
    {
      int	boxes;
    
      int	open; /*which boxes are open */
    
      int skipped; /* number being skipped by */
       
      printf ("How many mail boxes does he skip?\n");
      
      scanf("%d" , &boxes);
    
    
      while (boxes <= 150){
    	  open = boxes;
    
    	  skipped = skipped + boxes;
      }
      printf("The following mailboxes are open %d\n" , open);
    	
    
    	return 0;
    
    }

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I am not really sure what you are trying to do, but you have not initilalized open or skipped, which means you will get whatever crap happens to be on the stack at the time. Additionally, you never increment boxes, so your loop will never terminate.

    BTW, this "skipped = skipped + boxes;" is more clearly written "skipped += boxes;". Also, you need to declare that main() returns an int.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    68
    ya I changed my code a bit:

    Code:
    #include <stdio.h>
    
    main ()
    
    {
      int	open; /*which boxes are open */
    
      int	skipped; /* number being skipped by */
       
      printf ("How many mail boxes does he skip?\n");
      
      scanf("%d" , &open);
    
    
      while (open <= 150){
    	  open = skipped;
    
    	  skipped = open + 1;
      
      printf("The following mailboxes are open %d\n" , skipped);
      }
    
    	return 0;
    
    }
    I'm getting there, this is supposed to output something like this if the user inputs 3

    1 = closed 2 = closed 3 = open 4= closed 5 = closed 6 = open ...and so on to 150

  6. #6
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I am guessing, but is this what you want?

    Code:
    #include <stdio.h>
    
    int main (){
        int	open=0, skip;
        int	cnt = 0, skipped=0;
           
        printf ("How many mail boxes does he skip?\n");
        scanf("%d" , &skip);
    
        while (cnt < 150){
            cnt++;
            if (cnt % skip == 0){
                open++;
                printf("%d = closed\n", cnt);
            }else{
                skipped++;
                printf("%d = open\n", cnt);
            }
        }
        printf("Opened a total of %d boxes\nSkipped a total of %d boxes\n", open, skipped);
    
        return 0;
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    68
    his is it mostly, I just have format the output into nice neat columns thanks, you've been a big help, you have no idea how confused I was getting lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Program that requests input of an integer number
    By theejuice in forum C Programming
    Replies: 6
    Last Post: 04-30-2008, 02:18 AM
  3. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. Using loops for check a roman number input.
    By eryell in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2006, 11:04 AM

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