hi there... im tring to write a program that basicly adds time together
The first time is a intereger in 24h format ie (1301) would be 1:01 pm
the second time is in the same format but the amount of hours you want to add. So 101 would add 1 hour and 1 min.
The problem is when i get both inputs the first input is always the same as the last even though they are sperate variables. Now i figure that must be something ive dont wrong with a pointer somewhere but i cant figure out where (though i supect it is in my input function.. of course heh)
Code:
/***************************************************************************
Exercise 12279
Write a program to read two integers with the following significance.
The first integer value represents a time of day on a 24 hour clock,
so that 1245 represents quarter to one mid-day, for example.
The second integer represents a time duration in a similar way,
so that 345 represents three hours and 45 minutes.
This duration is to be added to the first time,
and the result printed out in the same notation,
in this case 1630 which is the time 3 hours and 45 minutes after 12.45.
Typical output might be Start time is 1415. Duration is 50. End time is 1505.
There are a few extra marks for spotting.
Start time is 2300. Duration is 200. End time is 100.
©2005 Mark Crick
***************************************************************************/
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const float inches = 2.54;
const int numberOfInchesInFoot = 12;
char *getInput(char *question)
{
char *retVal;
fputs(question,stdout);
fflush(stdout);
if(fgets(retVal,BUFSIZ,stdin) != NULL)
{
char *newline = strchr(retVal,'\n');
if(newline != NULL)
{
*newline = '\0';
}
}
return retVal;
}
int convertString2Decimal(char *string)
{
return strtol(string,NULL, 10);
}
int convertString2Float(char *string)
{
return atof(string);
}
int main()
{
int add2time,add2timeHours,add2timeMins,time,timeHours,timeMins;
char *input = getInput("Please enter time (as whole int 24h i.e 1301) :");
char *input2 = getInput("\nPlease enter time to add same format as above :");
time = convertString2Decimal(input);
timeHours = time / 100;
timeMins = time - (timeHours * 100);
printf("Time: %i",time);
add2time = convertString2Decimal(input2);
add2timeHours = add2time / 100;
add2timeMins = add2time - (add2timeHours * 100);
printf("Time: %i",time);
timeMins += add2timeMins;
if(timeMins > 59)
{
timeHours++;
timeMins -= 59;
}
printf("Time: %i",time);
time += (timeHours * 100) + timeMins;
if(time >= 2400)
{
time -= 2400;
}
printf("\nNew time = %i , %i, %i %i,%i,%i",time,timeHours,timeMins,add2time,add2timeHours,add2timeMins);
return 0;
}