473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

what is wrong with the 'strcpy'

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[i][2]=a[i][1]+a[i][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[i][0],&a[i][0],&a[i][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[i],a[i][0],a[i][1],a[i][2]);
}
}

May 7 '06 #1
9 2624
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[i][2]=a[i][1]+a[i][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[i][2] = a[i][1] + a[i][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[i][0],&a[i][0],&a[i][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[i], 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[i],a[i][0],a[i][1],a[i][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[i], BUF_LEN, a[i])) {
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.
May 7 '06 #2
jim
thank u very much , i am a bit clear !
thank u !

May 8 '06 #3
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.
May 8 '06 #4
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 |
+----------------------------------------------------------------+
May 8 '06 #5
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!

May 10 '06 #6
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!

May 10 '06 #7
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)
May 10 '06 #8
On 2006-05-09, jim <ab********@gmail.com> 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.
May 10 '06 #9
jim
thank you and i have known more about how to do and what to do !

May 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: David. E. Goble | last post by:
Hi all; I have the following files; index.html, sigsheader.js, sigsboby.js, smilesbody.js and smiles.js. The sourse is below. The page displays two manual slide shows... Each slideshow has a set...
3
by: Prabh | last post by:
Hello, I am wondering whats wrong with the following code. It is crashing on Linux, but works fine on AIX. The function basically get DbNm@Srvr string and then return DbNm and Srvr back to...
2
by: atv | last post by:
Hi, is there something wrong with this code? It seems that it crashes at the else { clause, when allocating a second record. Excuse for the indentation, the g_warnings are from gtk. Also, i'm...
6
by: Daniel Rudy | last post by:
What is wrong with this program? When I try to compile it, I get the following error. Compiler is gcc on FreeBSD. strata:/home/dcrudy/c 1055 $$$ ->cc -g -oe6-3 e6-3.c e6-3.c: In function...
9
by: David. E. Goble | last post by:
Arrrh! some buttons work while others don't, but I can't see why. I have tried comparing the files that do work, with the ones that don't. But to no help. The funny thing is the parts that work...
1
by: Shark | last post by:
What could be the error?? i have a dll for converting vox files to Dll. the structure of the header looks like : ...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
1
by: me001 | last post by:
i know this is set up wrong but i didnt know how to do that--sorry. but this is my program that i have. it compiles and runs, but the values are completely wrong! can anyone help me?? #include...
7
by: fl | last post by:
Hi, I am learning C++ with C++ primer written by Lippman. The first output line: cout << s1 << endl; works right. The second is wrong. I find the problem is that s1 is destroyed in the call from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.