Thread: I am an infant with programming...Help..I need critics about this code.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Cebu City, Philippines
    Posts
    2

    I am an infant with programming...Help..I need critics about this code.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    //THIS IS A PROGRAM THAT WILL CALCULATE MILITARY TIME TO STANDARD TIME
    
    
    void convertTime(int,int);
    int main()
    
    
    {
    int x,y;
    printf("Input Military Time \nhours :");
    scanf("%d",&x);
    printf("minutes :");
    scanf("%d",&y);
    
    
    convertTime(x,y);
    return 0;
    }
    
    
    void convertTime(int nhours, int nminutes)
    {
    int nMilitaryH,nMilitaryM;
         //output hours,output minutes
    
    
    if(nhours>12&&nhours<=23&&nminutes<60)    //PM calculation
     {
    nMilitaryH=nhours-12;   //hours
    nMilitaryM=nminutes-.00;    //minutes
    printf("%d:%.2d PM",nMilitaryH,nMilitaryM);
     }
    
    
    
    
     if(nhours==24&&nminutes==0)
     {
     nMilitaryH=nhours-12;   //hours         //midnight calculation
     nMilitaryM=nminutes-.00;    //minutes
     printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
     }
    
    
     if(nhours==0&&nminutes<60)              //calculates if hours is 00--its AM/Midnight up
     {
     nMilitaryH=12;            //hours
     nMilitaryM=nminutes-.00;    //minutes
     printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
     }
    
    
     if(nhours==12&&nminutes<60)              //calculates if hours is 12--its PM/noon up
     {
     nMilitaryH=12;            //hours
     nMilitaryM=nminutes-.00;    //minutes
     printf("%d:%.2d PM",nMilitaryH,nMilitaryM);
     }
    
    
     if(nhours<12&&nhours>0&&nminutes<60)      //AM calculation
     {
     nMilitaryH=nhours;         //hours
     nMilitaryM=nminutes-.00;    //minutes
     printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
     }
    
    
     if(nhours>=24&&nminutes>0||nminutes>59)          //error calculation--invalid input
     {printf("\n\n\nERROR!\nPlEASE TRY AGAIN!");}
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Look into using "else if" statements when appropriate.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by camel-man View Post
    Look into using "else if" statements when appropriate.
    I added the else if for you as the above poster mentioned

    Code:
    #include<stdio.h>
    //#include<conio.h>
     
     
    //THIS IS A PROGRAM THAT WILL CALCULATE MILITARY TIME TO STANDARD TIME
     
     
    void convertTime(int,int);
    int main()
     
     
    {
    int x,y;
    printf("Input Military Time \nhours :");
    scanf("%d",&x);
    printf("minutes :");
    scanf("%d",&y);
     
     
    convertTime(x,y);
    return 0;
    }
     
     
    void convertTime(int nhours, int nminutes)
    {
    int nMilitaryH,nMilitaryM;
         //output hours,output minutes
     
     
     if(nhours>12&&nhours<=23&&nminutes<60)    //PM calculation
     {
    nMilitaryH=nhours-12;   //hours
    nMilitaryM=nminutes-.00;    //minutes
    printf("%d:%.2d PM",nMilitaryH,nMilitaryM);
     }
     
     
     
     
     else if(nhours==24&&nminutes==0)
     {
     nMilitaryH=nhours-12;   //hours         //midnight calculation
     nMilitaryM=nminutes-.00;    //minutes
     printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
     }
     
     
     else if(nhours==0&&nminutes<60)              //calculates if hours is 00--its AM/Midnight up
     {
     nMilitaryH=12;            //hours
     nMilitaryM=nminutes-.00;    //minutes
     printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
     }
     
     
     else if(nhours==12&&nminutes<60)              //calculates if hours is 12--its PM/noon up
     {
     nMilitaryH=12;            //hours
     nMilitaryM=nminutes-.00;    //minutes
     printf("%d:%.2d PM",nMilitaryH,nMilitaryM);
     }
     
     
     else if(nhours<12&&nhours>0&&nminutes<60)      //AM calculation
     {
     nMilitaryH=nhours;         //hours
     nMilitaryM=nminutes-.00;    //minutes
     printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
     }
     
     
     else(nhours>=24&&nminutes>0||nminutes>59)          //error calculation--invalid input
     printf("\n\n\nERROR!\nPlEASE TRY AGAIN!");
    }

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    The last else does not need a condition.

    Code:
    else(nhours>=24&&nminutes>0||nminutes>59)          //error calculation--invalid input
     printf("\n\n\nERROR!\nPlEASE TRY AGAIN!");
    should be

    Code:
    else    
     printf("\n\n\nERROR!\nPlEASE TRY AGAIN!");

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Indent your code in according to some standard.
    Do not include headers you are not using; especially non standard headers like "conio.h".

    Tim S.

    You code indented per one of the common ways (Java style of indenting).
    Edit: Link http://sourceforge.net/apps/mediawik...le=Indentation
    Code:
    #include<stdio.h>
    // #include<conio.h>
    
    
    //THIS IS A PROGRAM THAT WILL CALCULATE MILITARY TIME TO STANDARD TIME
    
    
    void convertTime(int,int);
    int main()
    
    
    {
        int x,y;
        printf("Input Military Time \nhours :");
        scanf("%d",&x);
        printf("minutes :");
        scanf("%d",&y);
    
    
        convertTime(x,y);
        return 0;
    }
    
    
    void convertTime(int nhours, int nminutes) {
        int nMilitaryH,nMilitaryM;
        //output hours,output minutes
    
    
        if(nhours>12&&nhours<=23&&nminutes<60) {  //PM calculation
            nMilitaryH=nhours-12;   //hours
            nMilitaryM=nminutes-.00;    //minutes
            printf("%d:%.2d PM",nMilitaryH,nMilitaryM);
        }
    
    
    
    
        if(nhours==24&&nminutes==0) {
            nMilitaryH=nhours-12;   //hours         //midnight calculation
            nMilitaryM=nminutes-.00;    //minutes
            printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
        }
    
    
        if(nhours==0&&nminutes<60) {            //calculates if hours is 00--its AM/Midnight up
            nMilitaryH=12;            //hours
            nMilitaryM=nminutes-.00;    //minutes
            printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
        }
    
    
        if(nhours==12&&nminutes<60) {            //calculates if hours is 12--its PM/noon up
            nMilitaryH=12;            //hours
            nMilitaryM=nminutes-.00;    //minutes
            printf("%d:%.2d PM",nMilitaryH,nMilitaryM);
        }
    
    
        if(nhours<12&&nhours>0&&nminutes<60) {    //AM calculation
            nMilitaryH=nhours;         //hours
            nMilitaryM=nminutes-.00;    //minutes
            printf("%d:%.2d AM",nMilitaryH,nMilitaryM);
        }
    
    
        if(nhours>=24&&nminutes>0||nminutes>59) {        //error calculation--invalid input
            printf("\n\n\nERROR!\nPlEASE TRY AGAIN!");
        }
    }
    Last edited by stahta01; 04-19-2012 at 10:40 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have some confusing wording and variable names in your code. In main, you have the user enter military time. You pass that into convert time as nhours and nminutes. You then make nMilitaryH = nhours - 12, which makes no sense for two reasons: first, nhours already represents military time hours, so another military time hours variable is unnecessary, and second, you are making nMilitaryH represent non-military-time hours.

    Not to be insulting, but I feel like you don't have a full understanding of the actual conversion process. You must understand the problem and how to solve it on paper before you can write a program to do it. That goes for any problem you ever try to code a solution for.

    Here are the basic rules for 24-hour to 12-hour clock conversion:

    1. Valid inputs consist of hours from 0-23 inclusive, and minutes from 0-59 inclusive. Check for that and have the user re-enter input before you call your convert function.
    2. If the hour is less than 12, it is AM. If it is 12-23, it is PM.
    3. If the hour is 0, set it to 12.
    4. Otherwise, if the hour is greater than 12, subtract 12 from it.
    5. Otherwise do nothing to the hour (i.e. the hour is already valid)
    6. Do nothing to the minutes


    Rule #2 is a stand-alone if statement
    Rule #3 and #4 form an if/else if. Rule #5 would be the final else statement here, except it's a "do nothing" else, so we can omit it.

    Hope that clears it up.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Location
    Cebu City, Philippines
    Posts
    2

    Nice...

    good criticisms and corrections. tnx so much. i love this site.. some of these variable names for some reasons are just given by our professor. But they do make things more difficult to understand. Anyway, I will make this code simpler and easier to understand...now that you guys have helped me....

    Quote Originally Posted by anduril462 View Post
    You have some confusing wording and variable names in your code. In main, you have the user enter military time. You pass that into convert time as nhours and nminutes. You then make nMilitaryH = nhours - 12, which makes no sense for two reasons: first, nhours already represents military time hours, so another military time hours variable is unnecessary, and second, you are making nMilitaryH represent non-military-time hours.

    Not to be insulting, but I feel like you don't have a full understanding of the actual conversion process. You must understand the problem and how to solve it on paper before you can write a program to do it. That goes for any problem you ever try to code a solution for.

    Here are the basic rules for 24-hour to 12-hour clock conversion:

    1. Valid inputs consist of hours from 0-23 inclusive, and minutes from 0-59 inclusive. Check for that and have the user re-enter input before you call your convert function.
    2. If the hour is less than 12, it is AM. If it is 12-23, it is PM.
    3. If the hour is 0, set it to 12.
    4. Otherwise, if the hour is greater than 12, subtract 12 from it.
    5. Otherwise do nothing to the hour (i.e. the hour is already valid)
    6. Do nothing to the minutes


    Rule #2 is a stand-alone if statement
    Rule #3 and #4 form an if/else if. Rule #5 would be the final else statement here, except it's a "do nothing" else, so we can omit it.

    Hope that clears it up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with programming code!!!
    By Manny01 in forum C Programming
    Replies: 3
    Last Post: 01-08-2012, 06:25 PM
  2. Can someone help me with my C programming lottery code?
    By OvdoMan9 in forum C Programming
    Replies: 2
    Last Post: 09-02-2010, 11:07 PM
  3. Does C++ be the code behind of DotNet programming for Web
    By rchiu5hk in forum C++ Programming
    Replies: 4
    Last Post: 09-03-2009, 06:10 AM
  4. In need of critics, suggestions, feeback and ideas.
    By aroticoz in forum C++ Programming
    Replies: 6
    Last Post: 01-07-2006, 11:44 AM