Thread: Windows return error when input is made

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    Windows return error when input is made

    Hi

    I have this very simple program :
    Code:
    #include <stdio.h>
    main(){
           char name;
           printf("what is your name?");
           scanf("%s",&name);
           printf("Your name is %s",name);
           }
    After I run it, and after I type in some name and press 'Enter', M/Windows return error:
    "name.exe has encountered a problem and needs to close. We are sorry for the inconvenience."

    The filename is name.exe. What can be the problem? And how should I solve it? Thanks for your kind help.

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    while you're at it, main returns int - why can't people seem
    to get this into their head as opposed to every other variant?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Because of bad books/tutorials and while were at it, bad professors
    Woop?

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Code:
    #include <stdio.h>
    main()
    {
           char name[100];
           printf("what is your name?");
           scanf("%s",&name);
           printf("Your name is %s",name);
    }

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by limitmaster
    Code:
    #include <stdio.h>
    main()
    {
           char name[100];
           printf("what is your name?");
           scanf("%s",&name);
           printf("Your name is %s",name);
    }

    Then the question must be asked - what happens if someone has a name with 101+ characters in it?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    fgets to the rescue! See my sig
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by limitmaster
    Code:
    #include <stdio.h>
    main()
    {
           char name[100];
           printf("what is your name?");
           scanf("%s",&name);
           printf("Your name is %s",name);
    }
    You can remove the & since an array's name can be passed to a function which expects a pointer.

  9. #9
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    thanks for correcting my wrong code......("/)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM