#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11

    Unhappy Need help stumped


    I know any beginner programmer can figure this out... just stumps me where the problem is prob be better if i spaced it corectly :P

    the output needs to be like a clock like if its 12:00:00 or 12:01:01 or 12:34:34 you get the idea... if you can help me out that would be great

    thx in advanced :)

    Code:
    					if (Hour == 00)
    					Hour = 12;
    
    						//	if (premin == 00)
    						//	{
    						//		printf("%.0lf:0%.0lf:0%.0lf\n", Hour, premin, presec);
    							
    							if (premin <9)
    							{
    								printf("%.0lf:0%.0lf:%.0lf\n", Hour, premin, presec);
    							}			
    										else if (premin <9 && presec <9)
    										{
    										
    										printf("%.0lf:0%.0lf:0%.0lf\n", Hour, premin, presec);
    										}
    									else if (presec == 00)
    									{
    									printf("%.0lf:%.0lf:0%.0lf\n", Hour, premin, presec);
    									}
    
    									//	 else if (premin == 00 && presec <9)
    											
    									//		printf("%.0lf:%.0lf:0%.0lf\n", Hour, premin, presec);
    								//	}
    //												else
    
    
    						printf("%.0lf:%.0lf:%.0lf\n", Hour, premin, presec);
    Last edited by Angelo; September 20th, 2003 at 08:49 PM.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    First, something that you'll get hammered on in this forum: use code tags. They preserve the indentation of your code. Without that indentation, your code is much harder to read, which makes it harder for us to help you. They are very much like HTML tags, only they use square brackets [] . At the beginning of your code, place the word "code" (without the quotation marks) within square brackets. After the end of your code, do the same only with "/code" (again without the quotation marks). Read the sticky note at the top of the forum, something like "read this first".

    Second, you are not telling us what the problem is that you're having. Is it not compiling? Is the output wrong? Giving us a fairly specific problem or question helps us to help you.

    My understanding is that you have hours, minutes, and seconds available as integer values and you want to output them as a colon-delimited time with leading zeros. The format specifier for outputting an integer in decimal is %d . %f is for floating-point numbers and %l is for long and is used to modify the following specifier (eg, %ld for long signed decimal, %lu for long unsigned decimal).

    To have the number output to a specific width, put the width before the specifier: a 2-digit decimal would be %2d . If it takes more characters than you specify, then C will use whatever number it takes, so:
    Code:
    int x = 4;
    printf("The first value of x is %2d\n",x);
    x = 1024;
    printf("The second value of x is %2d\n",x);
    would produce the following two lines:
    The first value of x is 4.
    The second value of x is 1024.

    It might not be evident without a monospace font, but in the first line there are two spaces before the "4". If the number does not take up the entire width specified, then it pads with leading blanks.

    Now, if you want to pad with leading zeros, then you put a zero in front of the width. In that case, our code would be:
    Code:
    int x = 4;
    printf("The first value of x is %02d\n",x);
    x = 1024;
    printf("The second value of x is %02d\n",x);
    would produce the following two lines:
    The first value of x is 04.
    The second value of x is 1024.


    Now you can use that same principle of specifying a leading zero in your code.

    We will help point the way and sometimes offer sample code, but we wouldn't be helping you if we just gave you the answer for your homework.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11
    my if statement isn't working correctly it just avoids working as i want it too... lol like if i had 12:00:01 it would work but if i had 12:58:00 it would display 12:58:0 its driving me nuts and can't figure out why can i get some help ;)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11
    do you tink a do while would be better?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11
    i got my math of it down right its just needs an extra 0 at the seconds part for anything from 0-9 seconds
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by Angelo
    my if statement isn't working correctly it just avoids working as i want it too... lol like if i had 12:00:01 it would work but if i had 12:58:00 it would display 12:58:0 its driving me nuts and can't figure out why can i get some help ;)
    OK, here's what your code looks like (commented lines deleted and printf's numbered for identification):
    Code:
    if (Hour == 00)
        Hour = 12;
    
    if (premin <9)
    {
          /* #1 */
        printf("%.0lf:0%.0lf:%.0lf\n", Hour, premin, presec);
    }			
    else if (premin <9 && presec <9)
    {
          /* #2 */
        printf("%.0lf:0%.0lf:0%.0lf\n", Hour, premin, presec);
    }
    else if (presec == 00)
    {
          /* #3 */
        printf("%.0lf:%.0lf:0%.0lf\n", Hour, premin, presec);
    }
    
          /* #4 */
    printf("%.0lf:%.0lf:%.0lf\n", Hour, premin, presec);
    OK, if premin < 9, then you print #1 and you print #4. But if premin >= 9 and presec != 0, then you only print #4. There is no path for handling when premin >= 9 and presec <9; in short #2 will never get called. and if #1 or #3 gets called, you're still going to output a second line with #4; #4 always gets called!. You need to have an else before it if you intend it to be the catch-all handling of any time that does not meet any of the previous conditions. There are a few times when you would not have a catch-all else at the end of a chain of if-else's, but those times are very few and extremely rare.

    Also, why are you testing for <9 ? What if the value is 9? Nine is still a single-digit number that would need a leading zero. You should test either for <= 9 or, more succinctly and more C-ish, <10 .

    And please explain to me exactly what you are trying to do with the format specifier: %.0lf -- ie, what each symbol there is supposed to accomplish. Why are you using floating-point formatting with what should be integer variables?

    And lastly, why are you writing literal numbers with leading zeros? Eg: if (Hour == 00). You're lucky that you've only done it with zero. What do you think the value of 020 is? Twenty? Wrong! It's sixteen. If you write a literal number with a leading zero, you are telling the compiler that it is in octal, AKA "base 8". Similarly, prefacing a literal number with "0x" says that it's in hexadecimal, base 16. Be aware of what you are actually telling the compiler and break that habit.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11
    i have %.0lf b\c that way it will drop the decimal points.. i know i could use %d but i prefer lf :)
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by Angelo
    i have %.0lf b\c that way it will drop the decimal points.. i know i could use %d but i prefer lf :)
    So you end up doing in 8 or more lines with complex logic that you cannot get to work what you could do quite simply with one line. Why make it unnecessarily much more complicated? De gustibus non disputandum est -- there's no accounting for taste. But when you start working for somebody, you will find that they will not share your tastes in such things and will insist that you write code that other programmers could read and maintain.

    Besides, if the variables are of type int, then you have no need to worry about a decimal point.

    Your problem is that your tests are messed up AND that they do not take all cases into account. Why not just deal with one field at a time? You do realize that you can write a single line with multiple printf's.

    Here's what I mean:
    First print the hour and the minutes based on your test for whether minutes needs a leading zero. Use an if-else statement.
    Then print the seconds based on its test for needing a leading zero. Also include the '\n'. Use a second if-else statement completely separate from the first.

    That way, you can avoid having to test for every possible combination of minutes and seconds.
    Last edited by dwise1_aol; September 20th, 2003 at 09:36 PM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11

    clueless teach me wise one's


    Code:
    ok i took your sudjestion into consideration if i use else if it doesn't work is there another combination i could use or should i double bracket it? 
    if (Hour == 00) {
    Hour = 12;
    
    if (Hour <9)
    							
    	printf("0%d:\n", Hour);
    	else
    	printf("0%d:\n", Hour);
    							}
    							if (premin < 9) {
    							printf("0%d:\n", premin);
    	else
    							printf("%d:\n", premin);
    							}
    
    							if (presec <9) {
    								printf("0%d:\n", presec);
    								else
    								printf("%d:\n", presec);
    							}
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by Angelo
    Code:
    ok i took your sudjestion into consideration if i use else if it
    doesn't work is there another combination i could use or 
    should i double bracket it? 
    
    if (Hour == 00) 
    {
        Hour = 12;
    
        if (Hour <9)
            printf("0%d:\n", Hour);
        else
            printf("0%d:\n", Hour);
    }
    
    if (premin < 9) 
    {
        printf("0%d:\n", premin);
        else
        printf("%d:\n", premin);
    }
    
    if (presec <9) 
    {
        printf("0%d:\n", presec);
        else
        printf("%d:\n", presec);
    }
    Please only put code within the code tags; long lines just keep heading off the right side of the screen causing everything else to follow suit. And stripping off a few of those leading tabs will likewise prevent the code from marching off the right side of the screen.

    Your braces ("curly brackets") are wildly misplaced. Now you will print the hours only if hours == 0 (please, please, please lose that leading zero in (Hour == 00); what part of "you're using an entirely different numbering system" don't you understand?). Likewise, the way you've written it, you will only attempt to print minutes if premin < 9 or seconds if presec < 9 and otherwise you will print nothing at all.

    If it would even compile, which it won't. The compiler will complain about those misplaced else's; there's no if associated with them because of the way you placed your braces. Just placing an else between statements without any if statement is a syntax error.

    And why are you testing for < 9? What do you want it to do when the value is 9? If it's the same thing as when it's <9, then that's not going to happen.

    Here's pseudo-code for what should work:
    Code:
    if the hours is zero, make it 12
    
    print hours
    
    if premin is less than 10 then
        print premin with a leading zero
    else
        print premin
    end if
    
    if presec is less than 10 then
        print presec with a leading zero
    else
        print presec
    end if
    Have you studied any foreign language? I don't mean picking one up by hearing it as a child. I mean studied the grammar and structure of a language. Because those skills are almost directly applicable to computer languages. I started out a foreign language major before I went into computer science. In my very first programming course (FORTRAN), I approached it as a foreign language. While my classmates were trying to fix their bugs with random changes, I was reading my code to see what I was telling the computer to do and immediately seeing what the bug was.

    When you've written some code that doesn't work, then read it for its meaning. You are trying to tell the computer to do something and it will do exactly what you tell it to do even though that's not what you want it to do -- computers never do what you want them to do, only what you tell them to do (old adage, even appears in "Short Circuit").

    So what are you telling it to do? Play the role of the computer. Write down on a piece of paper what the values of the variables are and what the output is. Go through the code line-by-line and do precisely what the code tells you to do. Change the variables and generate output precisely as your code instructs you to. And you will see why the code is not producing the output you want.
    Last edited by dwise1_aol; September 21st, 2003 at 11:21 AM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11
    That code you posted with the fixed brackets give me this error where i orginally was getting

    C:\Documents and Settings\Laptop\Desktop\C Code\a1\a1.c(157) : error C2181: illegal else without matching if
    C:\Documents and Settings\Laptop\Desktop\C Code\a1\a1.c(166) : error C2181: illegal else without matching if
    C:\Documents and Settings\Laptop\Desktop\C Code\a1\a1.c(175) : fatal error C1004: unexpected end of file found

    (thats where that code i posted was)
  22. #12
  23. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by Angelo
    That code you posted with the fixed brackets give me this error where i orginally was getting

    C:\Documents and Settings\Laptop\Desktop\C Code\a1\a1.c(157) : error C2181: illegal else without matching if
    C:\Documents and Settings\Laptop\Desktop\C Code\a1\a1.c(166) : error C2181: illegal else without matching if
    C:\Documents and Settings\Laptop\Desktop\C Code\a1\a1.c(175) : fatal error C1004: unexpected end of file found

    (thats where that code i posted was)
    No, Angelo, that's your code. All I did was to reformat it (yanked out all those leading tabs and replaced them with spaces; most editors do have the option to replace tabs with spaces) to make it more readable so that you could see that those else's had no if's associated with them. And you got precisely the errors that I told you that you would. Now, don't try to tell me that your code didn't also get those very same errors before you thought it was somebody else's.

    Since you don't want to do it the nromal way with one line (which you'll have to learn eventually anyway if you ever plan on programming for a living), I did give you pseudo code that should simply the problem that you are making overly complicated.

    And you still haven't explained why you are testing for <9 and what results you expect should the minutes or seconds ever be equal to 9.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11
    the problem with the code is i can't have it simple b\c the other code i got is needed depending on those numbers.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    11
    you have errors in your curlie brackets. end of story. Every else statement must have a coresponding if. for instance your code:
    Code:
     if (Hour <9)
            printf("0%d:\n", Hour);
        else
            printf("0%d:\n", Hour);
    the else has a preceeding if. you need to close out the if statements curly then do the else. It may be that it can't be simple because of whatever reason but i believe the simplicity referred to by dewise1 is getting the grammar correct. Re read proper grammar rules for if and else statements.

    Good Luck
    Thanks,

    Jonathan Cobb
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Toronto
    Posts
    65
    Rep Power
    11
    i solved it i'll post it later after i get changed the damn rain socked me lol

IMN logo majestic logo threadwatch logo seochat tools logo