Thread: Which loop!!????

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    vermont
    Posts
    9

    Which loop!!????

    Hey everyone!

    I'm new to this forum and figured out that it would be a good place to get some help. Most of what I will be posting will be in regards to homework help...if you dont mind. I'm very very new to programming and would love your help. So, I'm having an issue with a very simple program which simulates a bomb explosion.

    here is the problem:

    Write a program that will simulate a bomb explosion. Your program should ask the user for a number of seconds to count down. Using this value, print a line that says "Tick" to the user for each second. After your program finishes ticking for the correct number of seconds, the program should end with a "Boom!" An example of what your program may look like is below, where the $ is the cygwin prompt. The user types in the '5' -- this could be any value. [Hint: what part of this problem repeats again and again]

    $./explode
    What should I set the timer to? 5
    Tick.....
    Tick.....
    Tick.....
    Tick.....
    Tick.....
    BOOM!
    $

    Here is my program:

    /* tick tick boom! Thedore Rhoades */
    #include <stdio.h>
    int main(void)
    {

    int tick;



    printf("what should I set the timer to?");
    scanf ("%i", tick);
    {
    for ( tick = 1; tick <= 1; ++tick )

    printf ("tick...\n");
    }

    printf ("Boom!");




    return 0;

    }

    do i want to be using a for statement? I'm very close to solving this!

    thanks everyone

    T.J

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, T.J!

    Your program is close, but I suggest:

    *adding a & before tick, in the line with scanf(). Scanf() always needs the ADDRESS of the variable, so use the & anytime you don't have an array name to give you the address you need.

    For your for loop, what about using delay() or sleep() inside the loop, to give you the timing you need. for(i=0;i<tick;i++). i is the customary C iterator. Inside (not outside) the for loop, you want to have the sleep() or delay, and the BOOM! print.

    Give that a shot!
    Last edited by Adak; 02-01-2011 at 10:36 PM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Location
    vermont
    Posts
    9
    thank you!

    i havn't yet been introduced to delay or sleep yet in class. just how to use statements. i need to ask the user "what should i set the timer to?" and have it print tick the number of times the user calls for. I will work at it. thanks again

    Quote Originally Posted by Adak View Post
    Welcome to the forum, T.J!

    Your program is close, but I suggest:

    *adding a & before tick, in the line with scanf(). Scanf() always needs the ADDRESS of the variable, so use the & anytime you don't have an array name to give you the address you need.

    For your for loop, what about using delay() or sleep() inside the loop, to give you the timing you need. for(i=0;i<5;i++). You can replace i with tick, but i is the customary C iterator. Inside (not outside) the for loop, you want to have the sleep() or delay, and the BOOM! print.

    Give that a shot!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This looked good, just add the ampersand: & to the front of tick. Then:

    Code:
    printf("what should I set the timer to?");
    scanf ("%i", tick);  //add the ampersand: &
    
    for(i=0;i<tick; i++) {
      print tick
      delay() or sleep() One uses millisecond's, one uses seconds.
      To use delay() or sleep() you need to include a header file. Look in your help files to see
      which one (I use #include <dos.h>, but this is on a very old compiler. Yours may be    
      under a different header file name.
    }
    Always put your code between the forum [CODE ] [/CODE ] tags. Otherwise your code gets squished all to the left hand side, and put in a bad font to have to study.

    You get the code tags by clicking on the # icon, at the top of the forum's editor box.
    Last edited by Adak; 02-01-2011 at 10:39 PM.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Location
    vermont
    Posts
    9
    I don't want to use sleep or delay quite yet, until I can understand it. Im having a problem though i set it up like you said

    for ( tick = 0; tick <= tick; tick ++)

    however the <=tick; is making it so it repeats endlessly. How do you just have it print what the user asks for? Thanks again adak for helping me out I missed a a week and a half of class and still catching up. Oh and im using cygwin

    Quote Originally Posted by Adak View Post
    This looked good, just add the ampersand: & to the front of tick. Then:

    Code:
    printf("what should I set the timer to?");
    scanf ("%i", tick);  //add the ampersand: &
    
    for(i=0;i<tick; i++) {
      print tick
      delay() or sleep() One uses millisecond's, one uses seconds.
      To use delay() or sleep() you need to include a header file. Look in your help files to see
      which one (I use #include <dos.h>, but this is on a very old compiler. Yours may be    
      under a different header file name.
    }
    Always put your code between the forum [CODE ] [/CODE ] tags. Otherwise your code gets squished all to the left hand side, and put in a bad font to have to study.

    You get the code tags by clicking on the # icon, at the top of the forum's editor box.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to use a different variable for the current count and the total desired count -- you can't use tick for both.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Location
    vermont
    Posts
    9
    this is getting me so frustrated. I'm sorry but I'm over thinking what im doing

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's amazing how dumb the computer is - and frustrating when you're not used to it.

    Relax, and think of how you'd do it by hand, in the simplest terms, with paper and pencil.

    Not tick<tick. That makes no sense as a test for this. You want i < tick.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Location
    vermont
    Posts
    9
    yes!!!!!!!!!!!!!!!! hahahha wow ok this make sense.
    adak thank you so much. this is easy haha but my next assignment is going to scare me.
    i have to write a program to do tax calculations, im screwed!

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome, TJ.

    ...tax calculations, im screwed!
    Since gov't get's every penny it spends from the taxpayers, and gov't is growing at a fantastic rate - we're all going to be in a bad way.

    Fortunately, once you get the arithmetic down right, tax programs are not tough to make.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM