Thread: scanf is not working

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    scanf is not working

    The below program don't work. I don't know why. Please help
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    int main ()
    {
    char str1[30];
    char str2[30];
    clrscr ();
    printf ("Please enter string1 and then string2\n");
    scanf ("str1 = %s \n str2 = %s", &str1, &str2);
    getch ();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It should be, scanf ("str1 = %29s \n str2 = %29s", &str1, &str2);, but that's probably not what you mean. How's it not working? What do you expect it to do?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    scanf ("str1 = %s \n str2 = %s", &str1, &str2);
    The %s converter takes an argument of type char * (you're passing in a char **). Drop the '&'.
    Consider this post signed

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Do you actually plan on the user typing:

    str1 = foo
    str2 = bar

    Or do you want them to type:

    foo
    bar
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    24
    @bernt,

    Thank you but I cannot understand what you mean.

    @king mir
    I tried putting 29 as you suggested but still it didn't work.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Any string literal you specify to scanf, is expected as input, exactly as you specified it.

    So you expect a user to type <str1 = something> literally. If the user enters anything outside of your specification, scanf will fail.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    24
    No, the input entered is just two words like "room", "table". It was showing some weird output....but when I replaced scanf with gets(str1) and gets (str2). It works perfect.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by learning_grc View Post
    No, the input entered is just two words like "room", "table". It was showing some weird output....but when I replaced scanf with gets(str1) and gets (str2). It works perfect.
    Did you try...
    Code:
    scanf(" %s %s",str1,str2);
    scanf() does pattern matching. I you put a bunch of junk into the formatter string, then your user has to type that stuff to make it work.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    24
    @commontater
    yes it's working but I never thought about to try in that way because I learned in scanf we must use the symbol "&" after comma.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by learning_grc View Post
    @commontater
    yes it's working but I never thought about to try in that way because I learned in scanf we must use the symbol "&" after comma.
    Yes Scanf() expects pointers for it's conversions.

    However, an array's name is a pointer to the array, thus a string array is already a pointer so you don't need the address of operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with strings [using scanf()]
    By Luponius in forum C Programming
    Replies: 4
    Last Post: 04-26-2011, 10:27 AM
  2. why scanf() dont working?
    By RAJJUMOTE in forum C Programming
    Replies: 10
    Last Post: 12-16-2007, 10:29 PM
  3. Every other scanf not working
    By Rob20050 in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 12:48 AM
  4. char pointer working in scanf but not in cin.
    By sawer in forum C++ Programming
    Replies: 14
    Last Post: 06-15-2006, 02:15 AM
  5. Why isn't this working? using scanf and stuff
    By jumboman in forum C Programming
    Replies: 2
    Last Post: 07-17-2003, 01:56 PM