military_time //SOLVED

0 Derek Welch · July 5, 2015
Morning all,

 I have been able to work around the problems I have had in this simple code by myself so far but I can't figure out how to make it  run the code block that displays pm instead of am when the outcome is 12:00 - 12:59, it seems to be working fine in every other instance. I'm using code-blocks on windows 8.1. Please be gentle with me.
 
kind regards,
Derek

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    float minutes = 0;
    float military = 0.01666666667;
    float hours = 0;
    float hAnswer = 0;
    float mAnswer = 0;
    char again = 'y';


    while(again == 'y')
    {
    printf("This program converts military time.\n");
    printf("\nPlease enter the hour: ");
    scanf(" %f",&hours);

    printf("\nPlease enter the minutes: ");
    scanf(" %f",&minutes);

    if(minutes > 100)
    {
        hours++;
        minutes = 0;
    }

    if(hours > 12 )
    {
        system("CLS");
        hours = hours - 12;
        hAnswer = hours;
        mAnswer = (minutes / military);

        if(minutes <= .09)
           {
            printf("Your answer is: %.f:0%.f pm",hAnswer,mAnswer);
            }else
            {
            printf("Your answer is: %.f:%.f pm",hAnswer,mAnswer);
            }
            }else {
            system("CLS");
            hAnswer = hours;
            mAnswer = (minutes / military);

        if(minutes <= .09)
           {
               printf("Your answer is: %.f:0%.f am",hAnswer,mAnswer);
                }else{
                printf("Your answer is: %.f:%.f am",hAnswer,mAnswer);
    }
        }
    printf("\n\n(y) to continue, (n) to end. ");
    scanf(" %s", & again);
    system("CLS");

}
    return 0;
}




Post a Reply

Replies

Oldest  Newest  Rating
0 Derek Welch · July 5, 2015
  I got it, just need to take a break to clear my head. I add a simple if statement.


if(hours >= 12) // add the = in this line
    {
        system("CLS");
        if(hours != 12) // add this statement
        {
        hours = hours - 12; // so it ignores this line if it does = 12
        }
        hAnswer = hours;
        mAnswer = (minutes / military);
0 Cardinal Coog · July 5, 2015
Derek,

That happens to everyone. I can't count how many times I've stared at a problem for hours on end, only to walk away for a bit and come back and see that I was simply overlooking something rather minute. When those things happen, just walk away for a while. Get something to eat, take a nap, whatever. When you come back everything is suddenly crystal clear.
  • 1

C

107,024 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator