Thread: Simple Scanf and printf

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    Question Simple Scanf and printf

    Hey guys... I am currently taking a C progarmming class, and I've been unable to contact my instructor and I've reached a little snag in code

    It seems as though this program randomly crashes. I can't input any hyphens or anything more than about 50 characters without it crasahing. Its terribly confusing, and I have no idea why it isn't working.

    Edit: Also the program ends prematurely if I enter long text for the two previous strings.

    Here it is:

    Code:
    
    //This program accepts input a greeting, name and email and then displays
    // that information to the user.
    
    
       #include <stdio.h>
    
       int main(void)
       {
    
           char inEmail[500];
           char inName[500];
           char inMessage[150];
           char exitKey;
    
           printf("Enter a greeting message: ");
           gets(inMessage);
    
    
           printf("Enter a name please: ");
           scanf("%s",&inName);
    
           printf("Enter your email address: ");
    
           scanf("%s",&inEmail);
    
    
           printf("%s\n",inMessage);
           printf("Your name is %s\n",inName);
           printf("Your email address is %s\n",inEmail);
    
           printf("Press return to exit program:");
           scanf("%c",&exitKey);
    
           getchar();
    
    
           return 0;
    
       }
    Last edited by xamlit; 08-30-2005 at 06:39 PM.

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Luckily I found a article that helped me from this website:

    http://www.cprogramming.com/tutorial/c/lesson9.html

    So here is my revised code using fgets :

    Code:
    //Luke Tillman-Young
    //08/31/05
    
    //This program accepts input a greeting, name and email and then displays
    // that information to the user.
    
    
       #include <stdio.h>
    
       int main(void)
       {
    
           char inEmail[500];
           char inName[500];
           char inMessage[150];
           char exitKey;
    
           printf("Enter a greeting message: ");
           fgets ( inMessage, 256, stdin );
    
    
    
           printf("Enter a name please: ");
           fgets ( inName, 256, stdin );
    
           printf("Enter your email address: ");
    
           fgets ( inEmail, 256, stdin );
    
    
           printf( "%s\n", inMessage  );
           printf("Your name is %s\n",inName);
           printf("Your email address is %s\n",inEmail);
    
           getchar();
    
    
           return 0;
    
       }

  3. #3
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    The only thing that's actually wrong with your code is how you put an ampersand before an array in your scanf() calls, here:
    Code:
    scanf("%s",&inName);
    and here:
    Code:
    scanf("%s",inEmail);
    Then of course, there's the usual argument that neither gets nor scanf when used like this can tell when the end of the arrays are, so it's possible to overrun the buffers. You can fix that by using fgets and specifying a field width:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char inEmail[500];
      char inName[500];
      char inMessage[150];
      char exitKey;
    
      printf("Enter a greeting message: ");
      fgets(inMessage, sizeof inMessage, stdin);
    
      printf("Enter a name please: ");
      scanf("%499s",inName);
    
      printf("Enter your email address: ");
      scanf("%499s",inEmail);
    
      printf("%s\n",inMessage);
      printf("Your name is %s\n",inName);
      printf("Your email address is %s\n",inEmail);
    
      printf("Press return to exit program:");
      scanf("%c",&exitKey);
      getchar();
    
      return 0;
    }
    It's also a good idea to test input for success. Anything that comes from outside of your program should be viciously validated.
    Just because I don't care doesn't mean I don't understand.

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    So here is my revised code using fgets :
    That's not any better. You use fgets, but still give a size bigger than the buffer for inMessage.
    Just because I don't care doesn't mean I don't understand.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Thumbs up

    fgets() also adds a newline to the end of your input string, something you might have to deal with.
    Code:
    // secure string input, fgets() replaces gets() and scanf()
    
    #include <stdio.h>   // BUFSIZ usually 512
    #include <string.h> 
    
    int main()
    {
      char buf[BUFSIZ] = "";
      char *p;
      
      printf("Please enter a line of text (max %d characters)\n", sizeof(buf));
      
      if (fgets(buf, sizeof(buf), stdin) != NULL)
      {
        // remove trailing \n 
        if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
        printf("you entered: %s\n", buf);      
      }
    
      getchar();  // wait  
      return 0;
    }
    Last edited by Ene Uran; 08-31-2005 at 08:47 AM.
    Ask smart questions - if I would be that smart, I wouldn't need to ask questions.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Ene Uran
    fgets() also adds a newline to the end of your input string...
    Not quite.

    fgets() reads in at most one less than size characters from stream and
    stores them into the buffer pointed to by s. Reading stops after an
    EOF or a newline. If a newline is read, it is stored into the buffer.
    A '\0' is stored after the last character in the buffer.
    If a newline is there, and there is room, fgets() will store it into the buffer.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Thumbs up

    You are absolutely hairsplitting right Kermit. In this case there will be most likely a newline in the string, unless your name is 500 characters long.
    Ask smart questions - if I would be that smart, I wouldn't need to ask questions.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Ene Uran
    You are absolutely hairsplitting right Kermit. In this case there will be most likely a newline in the string, unless your name is 500 characters long.


    I know, it is a small thing - I think some of the others on here are starting to rub off on me.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Thumbs up

    Glad you brought it up! I am the same way, just don't want to admit it!
    Ask smart questions - if I would be that smart, I wouldn't need to ask questions.

  10. #10
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    In this case there will be most likely a newline in the string, unless your name is 500 characters long.
    Wacky input happens. What if someone wants to play a joke and redirects stdin to a file? I say it's better to test than to get caught out in the rain, and in my experience, 'exceptional' doesn't mean 'uncommon'.
    Just because I don't care doesn't mean I don't understand.

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. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM