Thread: Loop a block of code

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    Loop a block of code

    Hi,

    I am trying to get the program to request and convert the time in seconds 3 times. If i use and IF statement ie IF(time>) then "execute code" it makes the code look really long and was wondering if there was a quicker (cleaner) way of doing it?



    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
        int hours, mins, secs,time;
      printf( "Enter current time in the format <hours minutes seconds> \n");
        scanf("%d %d %d", &hours, &mins, &secs);
        time = (60 * hours + mins) * 60 + secs;
       printf( "Time converted to seconds is: %d", time);
    getchar();
    
    
    }
    thanks

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    You might want to look at a basic tutorial to get you started off with C programming. You can use a for() loop or a while() loop.
    Code:
    int i;
    for(i=0;i<3;i++)
    {
       //your block of code
    }
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you're interested in cleaner code, might I suggest you begin by tidying up your random indentation.
    Next, if you could post your attempt at using a loop that would help us see both what you've tried (so as to not suggest the same thing again) and what you may be doing wrong (if anything).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. backward debugging in Visual Studio??
    By George2 in forum Tech Board
    Replies: 12
    Last Post: 11-05-2006, 02:17 AM
  2. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 05:15 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM