Thread: strcpy()&strcmp()

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    strcpy()&strcmp()

    i can't pin-point where i'm really going wrong but i would like to design aprogramme that takes 5 names as inputs from keyboard ,arranges the names in alphabetical order.my programme goes like below:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
    {
    int i,j;
    char temp[20];
    char names[5][20];
    clrscr();
    printf("Enter names:\n");
    for(i=0;i<5;i++)
    scanf("%s",names);
    for(i=0;i<4;i++)
     for(j=i+1;j<5;j++)
     if(strcmp(names[i],names[j])>0)
      {
      strcpy(temp,names[i]);
      strcpy(names[i],names[j]);
      strcpy(names[i],temp);
      }
      getchar();
      return;
      }
    Please Help!
    The programme is just outputting the names as they were just entered.

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    >scanf("%s",names);

    This should be
    Code:
    scanf("%s", names[i]);
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And:

    >if(strcmp(names[i],names[j])>0)
    >{
    >strcpy(temp,names[i]);
    >strcpy(names[i],names[j]);
    >strcpy(names[i],temp);

    This should be:
    strcpy(names[j],temp);

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    @sean345:
    Correct me if I am wrong but I think the "address of" operator '&' is still missing. This should work:
    Code:
    scanf("%s", &names[i]);
    @mr alison:
    Code:
    char names[5][20];
    Are you sure you really want to use a 2D array? In this case it's just a waste of memory. Structures or Arrays of Structures can make life much easier.
    Code:
    struct test
    {
        char name[20];
    }list[5];
    
    int main()
    {
        int i;
        char temp[20];
        clrscr();
        printf("Enter names:\n");
        for(i=0;i<5;i++)
            scanf("%s", list[i].name);
        ...
        ...
        return (0);
    }
    And btw what are you trying to acomplish with this?
    Code:
    getchar();
    Just delete the line.
    Last edited by Sargnagel; 10-10-2002 at 02:35 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Correct me if I am wrong but I think the "address of" operator '&' is still missing.
    The address of operator is not required for items that are already pointers, names[i] is an array of char, so there's no problem.

    >In this case it's just a waste of memory.
    And an array of padded structures is smaller? At best the array of structures will be the same size as the two dimensional array.

    -Prelude
    Last edited by Prelude; 10-10-2002 at 02:13 PM.
    My best code is written with the delete key.

  6. #6
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Prelude
    The address of operator is not required for items that are already pointers, names is an array of char, so there's no problem.
    Just tried it and it works, too. Thanks for the hint, Prelude.

    Originally posted by Prelude
    And an array of padded structures is smaller? At best the array of structures will be the same size as the two dimensional array.
    Now, that's interesting! I didn't know that. I guess I will have to learn a little more about the internals of structures.
    Last edited by Sargnagel; 10-10-2002 at 02:29 PM.

Popular pages Recent additions subscribe to a feed