Thread: Program does not printf after spaces.

  1. #1
    Registered User
    Join Date
    Apr 2010

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003

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

    #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
    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, 03: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, 01:32 PM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM