Reply embedded...

> -----Original Message-----
> From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of
> Yuda
> Sent: Monday, February 05, 2007 3:31 AM
> To: [email protected]
> Subject: [c-prog] time calculator code
> 
> I have done my "time calculator" code for my assignment. This code
> calculates time to find out the total time after we give input. This
> code works, but for several input, it doesnt show accuracy. For
> example, when i input 545(means 5 hours and 45 minutes)+545(5 hours
> and 45 minutes), its output was = 1129(11 hours and 29 minutes). It
> supposed to be 1130(11 hours and 30minutes). anyone know what is wrong
> with this code? plz give understandable explanation!!
> 
> I made this code with borland C++, so you had better compile it with
> borland. Here is the code:

It is better if you trimmed off trivial compiler specific codes.  Not
everybody has the compiler that you have.

> 
> #include <stdio.h>
> #include <conio.h>
> #include <ctype.h>
> 
> void main()
> {
>       int a,b,hour,minute;
>       float x,y,z1,z2,i,j;

No reason to use float in this situation.

Beside, seriously consider using more meaningful identifier name.


>       char push;
>       _setcursortype(_NOCURSOR);
>       textmode(3);
> 
> 
>       do
>       {
> 
>       clrscr();
> 
>       gotoxy(21,1);printf("TIME CALCULATOR\n");
>       gotoxy(21,2);printf("---------------");
>       gotoxy(1,3);printf("Input time to calculate(HourMinute):\n");
>       printf("(Example: 0130 --> means (an hour and thirty minutes)\n\n");
>       printf("input 1: "); scanf("%d",&a);
>       printf("input 2: "); scanf("%d", &b);
> 
>       x=(float)a/100;
>       y=(float)b/100;

    x = a / 100;
    y = b / 100;

will get the "hours" part.


>       z1=(x-(int)x);
>       z2=(y-(int)y);
> 
>       i=z1*100;
>       j=z2*100;

    i = a % 100;
    j = b % 100;

will get the "minutes" part.


> 
>       hour=(int)x+(int)y;
>       minute=i+j;
> 
>       if(minute>=60 && minute<120)
>       {
>               hour+=1;
>               minute%=60;
> 
>       }
>       else
>       {
>               if(minute>=120 && minute<180)
>               {
>                       hour+=2;
>                       minute%=60;
> 
>               }
>               else
>               {       if(minute>=180)
>                       {
>                               hour+=3;
>                               minute%=60;
>                       }
>               }
>       }

Your entire nested if-else block above is essentially this:

    hour += minute / 60;
    minute = minute % 60;



> 
>       printf("\nYOUR TOTAL TIME: ");
>       printf("%d",hour);
>       printf(":%d",(int)minute);
> 
>       printf("\nTry again?: (Y/N)");
>       scanf("%c",push);
>       push=getch();
>       }
>       while(push=='y' || push=='Y');
> 
> }
> 
> plz, give me a solution!!!

HTH
Shyan



Reply via email to