Thread: Problem with scanf input

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    Problem with scanf input

    With this program, I need to have the user input 7 strings, and the function will sort them alphabetically and print them. Here's what I've got.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define N_STRINGS 7
    
    void main()
    {
         char *input[25];
         char *temp;
         int x,a,b;
    
         printf("Please type %i words: ",N_STRINGS);
         scanf("%s", &input);
    
         for (a=0;a<N_STRINGS-1;a++)
              for(b=a+1;b<N_STRINGS;b++)
              {
                   x=0;
                   
                   while(*(*(input+a)+x))
                   {
                         if(*(*(input+a)+x)>*(*(input+b)+x))
                         {
                               temp = *(input +a);
                               *(input+a) = *(input+b);
                               *(input+b) = temp;
                               break;
                         }
                         else if(*(*(input+a)+x) < *(*(input+b)+x))
                               break;
                         else
                               x++;
                   }
              }
              for(x=0;x<N_STRINGS;x++)
              printf("\n Here's the sorted words: \n%s\n",*(input+x));
    }
    The problem is, when i delete the scanf portion and change
    Code:
    char *input[25];
    to
    Code:
    char *input[] = "this","is","my","very","short","sample","sentence";
    this program will work, but it won't work with the user's input.
    Any ideas, please respond.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *input[25];
    This is an array of character pointers. You have to allocate space for each pointer, if you want to have that pointer actually point to a string. (Or have some other string some place which you make this pointer point to.) The point, no pun intended, is that you've got pointers that point to nothing.

    The second one is in effect an array of seven character pointers, which point to string literals.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    Thanks. Any ideas on how to make this work, or how I can use the strcmp() function to do this easier?

  4. #4
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    To make it work you have a number of options. The easiest route is to declare your input as: char input[N_STRINGS][25]; Now the scanf - You can change it to scanf("%s %s %s %s %s %s %s",input[0],input[1],input[2],....etc). Another way and a better method is to use fgets(str,BUFSIZ,stdin); You would have to declare str as char str[25] at top and then use strtok to get each individual word and stick it into the input array. Then in your loop instead of using pointer math you could simplify it by using normal array indexes into input. Just my two cents there are a bunch of different ways you could accomplish this like using malloc and keeping your input declaration the same. I still believe your scanf is wrong though because it's going to stop at the first space it encounters in the string.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets(str,BUFSIZ,stdin); You would have to declare str as char str[25]
    That would most definately be a BadThingTM.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf doesn't like order of input
    By yougene in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 12:59 PM
  2. Problem with printf and scanf!
    By davewang in forum C Programming
    Replies: 9
    Last Post: 11-21-2008, 09:35 PM
  3. scanf problem
    By Alexthunder in forum C Programming
    Replies: 6
    Last Post: 10-04-2006, 09:55 PM
  4. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 PM
  5. Input Operations Problem
    By 1rwhites in forum C Programming
    Replies: 3
    Last Post: 10-03-2005, 05:43 PM