Need help

This is a discussion on Need help within the C Programming forums, part of the General Programming Boards category; I am not getting the desired output from this program. It should print out the entered Month and Day after ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Need help

    I am not getting the desired output from this program.
    It should print out the entered Month and Day after the user enters at the prompt.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define LSIZE 81
    
    void seperate();
    int main()
    
    
    {
    	char date[LSIZE];
    	char tempmonth[15];
    	int tempday;
    	
    	printf("Enter a Month and Day (ex. Februaury 15) :");
    	
    	sscanf(date, "%s %d", &tempmonth, &tempday);	
    	  
    	seperate (date);
    	
    	printf("The Month you entered was %c\n", tempmonth);
    	printf("The Day you entered was %d\n", tempday);
    	
    	return 0;
    	
    }
    
    	void seperate(char strng[])
    
    {
    
    	int i = 0;
    	char c;
    	
    	while (i< (LSIZE-1) && (c = getchar()) != '\n')
    	
    	{
    	
    		strng[i] = c;
    		i++;
    	}
    	
    	strng[i] = '\0';
    	
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Use &#37;s, not %c to print.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    I tried that and got symbols on the first printed line.

    Thank you

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    What does your separate() function do, since you don't print the answer it returns.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    It prints an up arrow for the tempmonth

    it prints the 2293496 for the tempday

    I get the same results regardless of the user input.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    OIC, you're using sscanf(), to read from an UNINITIALISED string, rather than scanf() from stdin.

    But it seems that perhaps you should call seperate() to initialise date BEFORE calling sscanf to parse it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Thank you.

    I tried that and the %s in place of %c and that did it.

    I appreciate your help.

    JP

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21