Program does not printf after spaces.

This is a discussion on Program does not printf after spaces. within the C Programming forums, part of the General Programming Boards category; This program is suppose to be able to display the full address when inputed. It can only display what is ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    Program does not printf after spaces.

    This program is suppose to be able to display the full address when inputed.
    It can only display what is entered before the user hits the space bar.
    For example:
    When the program ask for the address i'll enter:
    254 Marios Castle Lala Land 93884

    And only 245 will be displayed.

    Here is the code:

    PHP Code:

    //Write program to display name, address, email address, and with a border.

    #include <stdio.h>
    void main(void)
    {
        
    char name[50];
        
    char address[50];
        
    char email_address[50];

        
    printf("\nPlease type your name:\n");
        
    scanf("%s", &name);
        
    printf("%s \n"name);
        
        
    printf("\nPlease enter your address:\n:");
        
    scanf("%s", &address);
        
    printf("%s \n"address);
        
        
    printf("\nPlease enter your email address:\n:");
        
    scanf("%s", &email_address);
        
    printf("%s\n"email_address);
        


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    The problem lies with your input method: it would be easier to read correctly if you used something like fgets instead of scanf, since %s causes the read to stop at whitespace.

    Incidentally, you should not be taking of address of name, address and email_address when using them with scanf. Those arrays would be converted to pointers to the first characters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    im still having problems can you show an example?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Use this.

    Code:
    #include <stdio.h>
    
    int main(void) {
        char name[50];
        char address[50];
        char email_address[50];
        
        printf("\nPlease type your name:\n");
        fgets(name, sizeof(name), stdin);
        printf("%s \n", name);
        
        printf("\nPlease enter your address:\n:");
        fgets(address, sizeof(address), stdin);
        printf("%s \n", address);
        
        printf("\nPlease enter your email address:\n:");
        fgets(email_address, sizeof(email_address), stdin);
        printf("%s\n", email_address);
    }
    scanf stops reading input when it reads a space. fgets doesn't.

    Nice syntax coloring, by the way.
    Last edited by Babkockdood; 09-18-2010 at 03:29 PM.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    also gets(address); is used when working with strings, and is white space friendly, but is very dangerous!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  4. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 12:32 PM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM

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