Thread: Problem with printf and scanf!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    22

    Problem with printf and scanf!

    Hi everybody!
    I have a project just received by today. It is pretty simple. Basically, just write a program about filling an information form and then print it out. (Using scanf and printf only)
    Here is how i want it to be like: (notice the underlines are the input information)

    First Name and Last Name:
    Tommy John
    Birth Of Date:
    November 2 1987
    Phone Number:
    123456789
    Hobbies:
    Play soccer, reading and coding

    Tommy John
    November 2, 1987
    123456789
    Play soccer, reading and coding


    And here is what i code:

    Code:
    #include <stdio.h>
    
    int main()
    {
        
        char firstname, lastname;
        char month;
        char hobbies;
        int day, year;
        double phonenumber;
        
        printf("First Name and Last Name:\n");
        scanf("&#37;s %s", &firstname, &lastname);
        
        printf("Birth Of Date:\n");
        scanf("%s %i %i", &month, &day, &year);
        
        printf("Phone Number:\n");
        scanf("%d", &phonenumber);
        
        printf("Hobbies:\n");
        scanf("%s", &hobbies);
        printf("\n");
        
        printf("%s %s", firstname, lastname);
        printf("%s %i %i", month, day, year);
        printf("%d", phonenumber);
        printf("%s", hobbies);
        
        
    
        system( "Pause" );
        return 0;
    }

    the problem i have is i was able to input through the last information which is hobbies, but it got crash and was unable to print out the information. And here is how the program and the crash message look like:
    http://i35.tinypic.com/20hk0sy.jpg

    i hope that somebody could help me out with this, because i already try out many way to fix the problem, but useless!
    Last edited by davewang; 11-21-2008 at 05:44 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Do you think you have set aside storage for firstname, lastname, and hobbies?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    may i ask" what do mean by set aside strorage? " sorry, because i'm just a freshman to the C programming language!
    and can you point out what did i code wrong about it? cuz i'm wondering. I have try to fix it in many way but it's still not working.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    int is not the best sollution for phone number ^^ fist it wont store the zero up front, second the pfhone number is out of the integer range, try making it a double variable (still no zero at the start) or rather make it a simple char.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    I tried it, and it let me type in information for the hobbies, but this time it got crash again, and was unable to print out the form

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by davewang View Post
    may i ask" what do mean by set aside strorage? " sorry, because i'm just a freshman to the C programming language!
    and can you point out what did i code wrong about it? cuz i'm wondering. I have try to fix it in many way but it's still not working.
    The point is, why do you believe that firstname is a character? How many people do you know with single letter first names? And single letter last names? And single letter hobbies?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    Code:
    printf("Birth Of Date:\n");
    Shouldn't it be Date of birth?

    Have you learned about arrays in class yet? That's what youre missing.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Once you figure out this character array thing (that must be like page 2 of chapter one), you'll have two choices -- either allocate enough storage with your declarations, or you can go the more exiting route, which is to use a buffer and malloc the space. That gives you the opportunity to learn about functions too:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *assign (char *string) {
    	char *ptr=malloc(strlen(string)+1);
    	if (assign==NULL) {
    		puts("malloc failure!");
    		exit (-1);		// this is really unlikely
    	} 
    	strcpy(ptr,string);
    	return ptr;
    }
    
    
    int main()
    {
    	char *firstname, *lastname, *month, *hobbies, *phonenumber, buffer1[4096], buffer2[4096];
    	int day, year;
    
    	printf("First Name and Last Name:\n");
    	scanf("&#37;s %s", &buffer1, &buffer2);
    	firstname=assign(buffer1);
    	lastname=assign(buffer2);
    
    	printf("Birth Of Date:\n");
    	scanf("%s %i %i", &buffer1, &day, &year);
    	month=assign(buffer1);
    
    	printf("Phone Number:\n");
    	scanf("%s", &buffer1);
    	phonenumber=assign(buffer1);
    
    	printf("Hobbies:\n");
    	scanf("%s", &buffer1);
    	hobbies=assign(buffer1);
    
    	printf("\n%s %s\n", firstname, lastname);
    	printf("%s %i %i\n", month, day, year);
    	printf("%s\n", phonenumber);
    	printf("%s\n", hobbies);
    
    	free(firstname);	// remember to free char* that have
    	free(lastname);		// been malloc'd
    	free(month);
    	free(hobbies);
    	free(phonenumber);
    	return 0;
    }
    Since you are not using the phone number in any arithmetic, there is really no point in storing it as some form of int. It may seem simpler to just go
    Code:
    char firstname[20], lastname[20], phonenumber[9], //etc
    but you might as well learn about memory allocation as this is a crucial aspect of c programming. The buffers could be ten times smaller, but they are still only 4k of memory, and this way you have basically no chance of an overflow. If you seperate first and last name into two questions, you only need the one buffer.
    Last edited by MK27; 11-21-2008 at 08:17 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    wow! thank you for your example MK27. Seriously, i have never learned it in class. The instructor just told us to use scanf and printf to write this program! i never thought that it gonna turn in so deeply and complicated like this! but anyway, i should go ahead and learn about these stuff now.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    But you don't have to do it the way MK27 wrote. Handing in an assignment using stuff that hasn't been covered in class might hurt your score(Ask the instructor). You can do this assignment other ways. The chapter was probably about arrays, so check that out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  4. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 12:40 AM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM