Thread: Pointer problem... i think

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    24

    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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Oh i just noticed i have

    convertString2Decimal
    and
    convertString2float
    which would seem a little silly

    by convertString2Decimal i mean convert to base 10.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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).

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Ahhh cool, should of thought of that.. i think ill go with option one... Thanks for the help.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    *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.?

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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]).

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Aahh okay cool thanks =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM