# Pointer problem... i think

• 11-28-2005
fatdunky
Pointer problem... i think
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; }```
• 11-28-2005
fatdunky
Oh i just noticed i have

convertString2Decimal
and
convertString2float
which would seem a little silly

by convertString2Decimal i mean convert to base 10.
• 11-28-2005
cwr
Quote:

Originally Posted by fatdunky
Code:

```char *getInput(char *question) {         char *retVal; /* Not initialised, you are just writing to "random" memory */         fputs(question,stdout);         fflush(stdout);         if(fgets(retVal,BUFSIZ,stdin) != NULL)         {                 char  *newline = strchr(retVal,'\n');                 if(newline != NULL)                 {                         *newline = '\0';                 }         }         return retVal; }```

When you call getInput twice, you are probably writing to the same random memory twice.

Either pass getinput an additional pointer for the answer (recommended), or have getinput malloc space for the answer then have your calling routine free when done (a bit silly).
• 11-29-2005
fatdunky
Ahhh cool, should of thought of that.. i think ill go with option one... Thanks for the help.
• 11-29-2005
fatdunky
*sigh* sorry guys but im still getting a program crash... it worked for a while then stop..
(unkown reason i was changing something to do with the maths no the string)

Code:

```void *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; } char *input, *input2;         getInput("Please enter time (as whole int 24h i.e 1301) :", input);         getInput("\nPlease enter time to add same format as above :",input2);```
If i quote out those 2 lines (which are the first 2 in main) the proggy works.?
• 11-29-2005
cwr
All you've done now is moved the problem from getInput to main...

You are still just allocating a pointer (char *input, input2) and not allocating any space from them. Either allocate space with malloc, or pass an array (char input[BUFSIZ], input2[BUFSIZ]).
• 11-30-2005
fatdunky
Aahh okay cool thanks =)