what is wrong with the 'strcpy'

J

jim

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]);
}
}
 
M

Michael Mair

jim said:
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.
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
 
M

Michael Mair

jim said:
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
 
C

Charles Richmond

jim said:
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???
 
J

jim

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!
 
J

jim

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!
 
R

Richard Heathfield

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.
 
A

Andrew Poelstra

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top