what is wrong with the 'strcpy'

Discussion in 'C Programming' started by jim, May 7, 2006.

  1. jim

    jim Guest

    i want to make a c file that i can 'scanf ' students scores of 2
    classes and their names , and i want it to get the sum of the 2 scores
    and make them in order .at last 'printf'
    /*am sorry,my english is not very good ,so u may have some difficulties
    in understanding my meaning ,but u can see the follow c file .
    thank u !*/




    int sum(int a[10][3])
    {
    int i;
    for(i=0;i<10;i++)
    {
    a[2]=a[1]+a[0];
    }
    }
    int px(int a[10][3],char s[10][6])

    {
    int i,j,t;
    char c[6];
    for(i=0;i<10;i++)
    {
    for(j=0;j<9-i;j++)
    {
    if(a[j][2]>a[j+1][2])
    {
    t=a[j][2];
    a[j][2]=a[j+1][2];
    a[j+1][2]=t;
    t=a[j][0];
    a[j][0]=a[j+1][0];
    a[j+1][0]=t;
    t=a[j][1];
    a[j][1]=a[j+1][1];
    a[j+1][1]=t;
    strcpy(c,s[j]);
    strcpy(s[j],s[j+1]);
    strcpy(s[j+1],c);

    }
    }
    }
    }
    #include<string.h>
    void main()
    {
    int i,a[10][3];
    char s[10][6];
    for(i=0;i<10;i++)
    {
    printf("%d,name,maths,engli,sumhe\n",i );
    scanf("%s,%d,%d",s[0],&a[0],&a[1]);
    }
    sum(a);
    px(a,s);
    printf("mc ,name,maths,engli,sumhe\n");
    for(i=0;i<10;i++)
    {
    printf("%3d,%5s,%5d,%5d,%5d\n",i,s,a[0],a[1],a[2]);
    }
    }
     
    jim, May 7, 2006
    #1
    1. Advertising

  2. jim

    Michael Mair Guest

    jim schrieb:
    > i want to make a c file that i can 'scanf ' students scores of 2
    > classes and their names , and i want it to get the sum of the 2 scores
    > and make them in order .at last 'printf'
    > /*am sorry,my english is not very good ,so u may have some difficulties
    > in understanding my meaning ,but u can see the follow c file .
    > thank u !*/


    Your C is not very good, either.
    Please compile your programmes at the highest possible warning/error
    level and make sure that the warnings vanish.


    >


    You are using printf and scanf, so you need to
    #include <stdio.h>

    10, as well as 6, is a magic number. Replace 10 and 6 by symbolic
    constants and parameters where appropriate.
    #define NUMBER_OF_STUDENTS 10
    #define BUF_LEN 6

    > int sum(int a[10][3])
    > {


    The 10 is meaningless in the parameter.
    Pass the size, too.

    > int i;
    > for(i=0;i<10;i++)
    > {
    > a[2]=a[1]+a[0];
    > }


    You are not returning anything from a function you
    claimed to return int.

    > }


    void sum(int (*a)[3], int numStud)
    {
    int i;
    for(i = 0; i < numStud; i++)
    {
    a[2] = a[1] + a[0];
    }
    }


    > int px(int a[10][3],char s[10][6])
    > {
    > int i,j,t;
    > char c[6];
    > for(i=0;i<10;i++)
    > {
    > for(j=0;j<9-i;j++)
    > {
    > if(a[j][2]>a[j+1][2])
    > {
    > t=a[j][2];
    > a[j][2]=a[j+1][2];
    > a[j+1][2]=t;
    > t=a[j][0];
    > a[j][0]=a[j+1][0];
    > a[j+1][0]=t;
    > t=a[j][1];
    > a[j][1]=a[j+1][1];
    > a[j+1][1]=t;
    > strcpy(c,s[j]);


    You use strcpy() before you declared it.
    #include <string.h>
    at the top of the file.

    > strcpy(s[j],s[j+1]);
    > strcpy(s[j+1],c);
    >
    > }
    > }
    > }


    Otherwise, the same holds. In addition, "px" is not a sensible
    name for a function trying to sort data.
    t is not a good name for an auxiliary variable, neither is c.
    > }


    void px(int (*a)[3], char (*s)[BUF_LEN], int numStud)
    {
    int i,j,temp;
    char tempString[BUF_LEN];
    for(i = 0; i < numStud; i++)
    {
    for(j = 0; j < numStud-1-i; j++)
    {
    if(a[j][2] > a[j+1][2])
    {
    temp = a[j][2];
    a[j][2] = a[j+1][2];
    a[j+1][2] = temp;
    temp = a[j][0];
    a[j][0] = a[j+1][0];
    a[j+1][0] = temp;
    temp = a[j][1];
    a[j][1] = a[j+1][1];
    a[j+1][1] = temp;
    strcpy(tempString,s[j]);
    strcpy(s[j],s[j+1]);
    strcpy(s[j+1],tempString);
    }
    }
    }
    }

    > #include<string.h>


    Too late.

    > void main()


    Now you are using void -- in the only place where you must not use it.
    main() returns int.
    int main (void)
    is what you want.
    > {
    > int i,a[10][3];
    > char s[10][6];

    int num = NUMBER_OF_STUDENTS;

    > for(i=0;i<10;i++)
    > {
    > printf("%d,name,maths,engli,sumhe\n",i );
    > scanf("%s,%d,%d",s[0],&a[0],&a[1]);


    You do not understand scanf(). That is no problem as scanf is
    not a very good input function. Your mistakes:
    - You do not specify what the user should give you in the way
    of information
    - You do not check whether you actually got 3 distinct objects
    from the scanf() call
    - You did not pass the address of a string but an uninitialized
    character -- undefined behaviour
    - %s, does not make sense as %s will gobble ',' as happily as
    anything else, so an input of "mike,100,42" will all go to
    s, happily overflowing your buffer.

    Much has been written in the comp.lang.c FAQ and in comp.lang.c
    itself about safe input. Go read it.
    > }
    > sum(a);
    > px(a,s);
    > printf("mc ,name,maths,engli,sumhe\n");
    > for(i=0;i<10;i++)
    > {
    > printf("%3d,%5s,%5d,%5d,%5d\n",i,s,a[0],a[1],a[2]);
    > }


    return 0;

    > }


    Restructure your programme, so that you have a chance to check what
    happens.
    You need
    void outputList(char (*s)[BUF_LEN], int (*a)[3], int numEntries);
    so you can output everything whenever you want.
    You need
    int getString(char *buffer, int buflen);
    int getNumber(int *pNum);
    which return one number on success and another on failure.
    You need
    int getEntry(char *buffer, int buflen, int *pNums)
    {
    int ret;
    buffer[0] = '\0';
    pNums[0] = pNums[1] = -1;

    puts("name:");
    ret = getString(buffer, buflen);
    if (ret)
    return ret;
    puts("maths:");
    ret = getNumber(&pNums[0]);
    if (ret)
    return ret;
    puts("engli:");
    ret = getNumber(&pNums[1]);
    if (ret)
    return ret;

    return 0;
    }
    and
    int getList(char (*bufList)[BUF_LEN], int (*a)[3], int numEntries)
    {
    int i, ret;
    for (i = 0; i < numEntries; i++) {
    if (ret = getEntry(bufList, BUF_LEN, a)) {
    fprintf(stderr, "Reading entry %d failed\n", i);
    return ret;
    }
    }

    return 0;
    }

    Use these functions instead from main().
    This should be enough to get you started.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 7, 2006
    #2
    1. Advertising

  3. jim

    jim Guest

    thank u very much , i am a bit clear !
    thank u !
     
    jim, May 8, 2006
    #3
  4. jim

    Michael Mair Guest

    jim schrieb:
    > thank u very much , i am a bit clear !
    > thank u !


    You are welcome; my answer was not full code, so you still have
    to find your way to the solution.

    If you reply to someone, please quote enough context that your message
    stands for itself -- usenet is an asynchronous medium so not everyone
    may have got the message you respond to by the time he or she reads
    it; if you are new to comp.lang.c, please also have a look at
    http://clc-wiki.net/wiki/Introduction_to_comp.lang.c


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 8, 2006
    #4
  5. jim wrote:
    >
    > i want to make a c file that i can 'scanf ' students scores of 2
    > classes and their names , and i want it to get the sum of the 2 scores
    > and make them in order .at last 'printf'
    > /*am sorry,my english is not very good ,so u may have some difficulties
    > in understanding my meaning ,but u can see the follow c file .
    > thank u !*/
    >

    How good does your English have to be...to know that "u" is *not* word???

    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond richmond at plano dot net |
    +----------------------------------------------------------------+
     
    Charles Richmond, May 9, 2006
    #5
  6. jim

    jim Guest

    i am sorry!
    i just want to use the 'u' to stand for you so that i can type the word
    quiker!
    i am sorry,and i will change in the futuer!
     
    jim, May 9, 2006
    #6
  7. jim

    jim Guest

    of course i am new. And i am a begainer of c ,so i have some problems
    during study,and with your help,i have ran the c.exe rightly!
    Thank you again!
     
    jim, May 9, 2006
    #7
  8. jim said:

    > i am sorry!
    > i just want to use the 'u' to stand for you so that i can type the word
    > quiker!
    > i am sorry,and i will change in the futuer!


    Translation:

    "I am sorry. I just wanted to use the 'u' to stand for 'you' so that I can
    type the word more quickly. I am sorry, and I will change in the future."


    I suggest you strive to slow down rather than speed up, so that you make
    fewer mistakes and so that you have more time to think about how to make
    your meaning more clear.

    Typing is done just once, but reading is done many times, because there are
    many readers. It makes more sense for a writer to save their time than to
    save his or her own time, because the overall saving is greater - and it
    shows you care enough about your readers that you are prepared to spend
    time making it easy for them to read what you write.

    Those who don't care about their readers will eventually have no readers to
    care about.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 9, 2006
    #8
  9. On 2006-05-09, jim <> wrote:
    > of course i am new. And i am a begainer of c ,so i have some problems
    > during study,and with your help,i have ran the c.exe rightly!
    > Thank you again!
    >


    Good job. A few pointers:
    1) Try to post clear English. It appears that you don't speak it as a
    first language, but make a small effort if you can.
    2) /Always/ quote your context, that is, what you are replying to. That
    way, those of use who aren't using Google and don't have the previous
    post immediately above us can read it.
     
    Andrew Poelstra, May 9, 2006
    #9
  10. jim

    jim Guest

    thank you and i have known more about how to do and what to do !
     
    jim, May 10, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Mimic

    strcpy

    Mike Mimic, May 16, 2004, in forum: C++
    Replies:
    9
    Views:
    812
    Peter Koch Larsen
    May 17, 2004
  2. Paul Sheer
    Replies:
    7
    Views:
    494
    Paul Sheer
    Sep 10, 2004
  3. Paul Sheer
    Replies:
    4
    Views:
    644
    Paul Sheer
    Sep 14, 2004
  4. RonHiler
    Replies:
    8
    Views:
    523
    John Harrison
    Oct 19, 2004
  5. Replies:
    11
    Views:
    928
    Alex Vinokur
    Jan 20, 2005
Loading...

Share This Page