# Pointer problem... i think

This is a discussion on Pointer problem... i think within the C Programming forums, part of the General Programming Boards category; hi there... im tring to write a program that basicly adds time together The first time is a intereger in ...

1. ## 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.

***************************************************************************/
#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()
{
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);

printf("Time: %i",time);
if(timeMins > 59)
{
timeHours++;
timeMins -= 59;
}
printf("Time: %i",time);
time += (timeHours * 100) + timeMins;
if(time >= 2400)
{
time -= 2400;
}
return 0;
}```

2. Oh i just noticed i have

convertString2Decimal
and
convertString2float
which would seem a little silly

by convertString2Decimal i mean convert to base 10.

3. 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).

4. Ahhh cool, should of thought of that.. i think ill go with option one... Thanks for the help.

5. *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);