Like Tree1Likes
  • 1 Post By stahta01

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

This is a discussion on I am an infant with programming...Help..I need critics about this code. within the C Programming forums, part of the General Programming Boards category; 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() { ...

  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
    612
    Look into using "else if" statements when appropriate.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    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
    612
    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
    2,608
    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.
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    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, 05: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, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21