Strcpy versus Strncpy in arrays

S

SK

Hi
I appreciate all of the feedback I have recieved. I am enjoying
working on my program and I hope that it can be appreciated. I have my
program compiling now and I am continuing to work out the bugs. I have
two problems that I cannot seem to resolve:

1. I want to have someone enter in as many employees as they want
to..and for each employee entered I would like to save the data from
that entry to print out at the end of the program. I continue to run
into a problem with the loop. I actually did not intend to use arrays
however I cannot seem to figure outany other way to approach the
problem.

2.This second problem is probably due to using arrays
intheprogram,however that being said I still want to figure this
out....I am trying to connect two strings together, concatenation. I
understandthat strncpy refers to n number of characters versus strcpy
is the actual character but the difficulty I am running into has to do
with the syntax. It is evident that I am somehow not correctly dealing
with putting things into memory and then extracting them. Any
thoughts??

//Specs to be added later

//C Libraries
#include <stdio.h>
#include <math.h>
#include <string.h>


//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000


//Global Defined Constant
const float OT = 1.5;

//Global Variable Declaratives
FILE*inp;

//Global Constants
# define FULLNAME 20
# define EMPLOYEES 1000

char fn[FULLNAME];
char ln[FULLNAME];
char department[20];
char again;


int count_EMP;
int number_EMP;

float wage;
float OTwage;
float hours;
float RegHr;
float RegHrPay;
float OTHrPay;
float OTHr;
float GrossPay;




int main(void)
{
/*Define the structure.*/
struct EMP_WeeklyPay
{
char first_name[FULLNAME];
char last_name[FULLNAME];
float RegHr;
float wage;
float OTHr;
float OTHrPay;
float GrossPay;
};


/*Rename the structure syntax.*/
typedef struct EMP_WeeklyPay EWP;


/*Create an array of structures.*/
EWP emp[EMPLOYEES];

/*Counters*/
int n, numemp;
int count = 0;



printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");
printf("Please enter the name of the department: ");
scanf("%s", department);


/*Loop to read in employee wage data*/
count_EMP = 0;
count_EMP++;

for (n = 0; n < EMPLOYEES; ++n){
do{
printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &fn, &ln);

printf("\nPlease enter the hourly wage for the employee:
");
scanf("%f", &wage);

printf("\nPlease enter the number of hours worked this"
" week: ");
scanf("%f", &hours);

printf("\nThank you. Process another employee?");
scanf("%s", &again);



}while(again == 'Y' || again == 'y');

/*Read in the input*/

numemp = scanf("%11s%11s%f%f%f%f%f", &fn, &ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);

/*Check if user is done*/




if(again != 'Y' && again !='y');
printf("End of processing\n\n\n");


/*Process the input*/
if(numemp == 6)
{

if (RegHr > 40)
{
OTHr = hours - 40;
OTHrPay = OT * OTHr * wage;
RegHrPay = 40.0 * wage;
}

else
{
RegHrPay = hours * wage;
OTHrPay = 0.0;

}

GrossPay = RegHrPay + OTHrPay;



strncpy(emp[n].first_name, fn, FULLNAME);
emp[n].first_name[FULLNAME] = '\0';

strncpy(emp[n].last_name, ln, FULLNAME-1);
emp[n].last_name[FULLNAME-1] = '\0';

emp[n].RegHr = RegHr;
emp[n].wage = wage;
emp[n].OTHr = OTHr;
emp[n].OTHrPay = OTHrPay;
emp[n].GrossPay = GrossPay;


++count;
}



/*Print Table*/

printf("\n\nMountain Pacific Corporation\n");
printf("Department Salary Program\n\n");

printf("Employee Reg Hrs "
"Overtime Hrs Gross\n");

printf("-----------------------------------------"
"-------------------------\n\n");




for(n=0; n < count; ++n) {
printf("%-35s%-17s%12f%10f%12f%10f%%5f",
emp[n].first_name,
emp[n].last_name, emp[n].RegHr,
emp[n].wage, emp[n].OTHr, emp[n].OTHrPay,
emp[n].GrossPay);
}


}


}
 
U

Ulrich Eckhardt

SK said:
2.This second problem is probably due to using arrays
intheprogram,however that being said I still want to figure this
out....I am trying to connect two strings together, concatenation. I
understandthat strncpy refers to n number of characters versus strcpy
is the actual character but the difficulty I am running into has to do
with the syntax. It is evident that I am somehow not correctly dealing
with putting things into memory and then extracting them. Any
thoughts??

Stop working on your program here and start another one just for testing
out how strcpy and strncpy work. Also, read the documentation of both
functions. If you have problems with some very specific thing, post a
minimal example - your growing main project doesn't qualify as minimal
example.

Lastly, in order to handle arbitrary strings, you will need
malloc/realloc/free for the memory handling and strlen, strcpy and strcat
for the string operations. The *n* variants are usually only necessary if
you operate on fixed size arrays, for dynamic strings you better resize
them beforehand.

Uli
 
M

Mark McIntyre

//Global Defined Constant
const float OT = 1.5;

You really don't want to use float - use double.
scanf("%s", department);

As I said before, scanf is a bad function to use for this. Consider
what happens if someone enters 22 characters.
for (n = 0; n < EMPLOYEES; ++n){

usual to do n++
do{
printf("\nEnter employee # %d: ", count_EMP);
scanf("%s %s", &fn, &ln);

fn and ln are not defined. Assuming they're supposed to be strings,
you don't need the ampersands.
scanf("%s", &again);

}while(again == 'Y' || again == 'y');

again is a string, not a single char so you need "Y" and "y". Or
better yet, declare again as a character and use the %c format.
/*Read in the input*/

you never asked the user to enter any...
numemp = scanf("%11s%11s%f%f%f%f%f", &fn, &ln, &RegHr,
&wage, &OTHr, &OTHrPay, &GrossPay);

again, fn and ln don't seem to be defined.


You also seem to have some big problems with loops.

Take a step back. Write down _in english_ (or your native language), a
description of what the programme has to do. Consider carefully how
each piece of data needs to be obtained and stored.

Then, once you have a design, start writing code.
 
P

pete

Mark McIntyre wrote:
fn and ln are not defined.

He's got a bunch of globals, and those are two of them.
again is a string, not a single char so you need "Y" and "y". Or
better yet, declare again as a character and use the %c format.

'again' was declared as a char. another global
 
D

Default User

Mark said:
On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"


usual to do n++


Ummm, what? Usual for whom? K&R uses the prefix notation. It's so
common as to be completely unremarkable.



Brian
 
M

Mark McIntyre

Ummm, what? Usual for whom?

Not for whom, for what.

int array[10];

for(i=0;i<10;++i)
array = i;

In this context particularly, I find pre-increment especially
counterintuitive.
 
M

Mark McIntyre

He's got a bunch of globals, and those are two of them.

not in the version I was commenting on - I checked.
'again' was declared as a char. another global

Not in the verison I was commenting on - it was a character array.
 
K

Keith Thompson

Mark McIntyre said:
Ummm, what? Usual for whom?

Not for whom, for what.

int array[10];

for(i=0;i<10;++i)
array = i;

In this context particularly, I find pre-increment especially
counterintuitive.


I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring, and my guess is that most other C programmers feel the same
way. And of course they're completely equivalent in that context (as
they are in statement context). I hardly even notice which one is
used; I think I mentally translate both to "increment i" if the result
isn't used.

But then we all have our little quirks, like my own strong distaste
for "42 == x" rather than "x == 42" (and yes, I know the arguments).
So I won't criticize you for disliking ++i, but you should be aware
that your dislike is (probably) a minority viewpoint.
 
J

Jordan Abel

Ummm, what? Usual for whom?

Not for whom, for what.

int array[10];

for(i=0;i<10;++i) array = i;

In this context particularly, I find pre-increment especially
counterintuitive.


Why? The final result [i.e. what ++i evaluates to] is what is tested
for being less than 10. I tihnk the real issue is that it's not what
you're used to.
 
D

Default User

Keith said:
Mark McIntyre said:
Mark McIntyre wrote:

On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"

for (n = 0; n < EMPLOYEES; ++n){

usual to do n++


Ummm, what? Usual for whom?

Not for whom, for what.

int array[10];

for(i=0;i<10;++i)
array = i;

In this context particularly, I find pre-increment especially
counterintuitive.


I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring, and my guess is that most other C programmers feel the same
way. And of course they're completely equivalent in that context (as
they are in statement context). I hardly even notice which one is
used; I think I mentally translate both to "increment i" if the result
isn't used.


Right. I personally use i++ most of the time, but find ++i so
unremarkable that I, well, don't remark upon it. I would expect that
most compilers would render the same machine code for either in that
context.

As I said, K&R 2 uses preincrement. Someone pointed that out to me some
time back, which surprised me. I literally had never noticed that they
didn't use postincrement, and would have sworn that they did.
But then we all have our little quirks, like my own strong distaste
for "42 == x" rather than "x == 42" (and yes, I know the arguments).

I agree with you. I don't like it, so I don't use it.
So I won't criticize you for disliking ++i, but you should be aware
that your dislike is (probably) a minority viewpoint.

I don't even know that preference for i++ is a minority opinion, but
most likely CARING would be a minority opinion.




Brian
 
M

Mark McIntyre

I agree with "Default User". I suppose I have a slight preference for
postincrement, but I don't find either one counterintuitive or
jarring,

A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.
and my guess is that most other C programmers feel the same
way.

And my guess is that most are relatively agnostic. Or think its a daft
debate.
So I won't criticize you for disliking ++i, but you should be aware
that your dislike is (probably) a minority viewpoint.

I disagree that it is, but its largely irrelevant. :)
 
M

Mark McIntyre

Why? The final result [i.e. what ++i evaluates to] is what is tested
for being less than 10. I tihnk the real issue is that it's not what
you're used to.

Because unless you know the C grammar, it can read as
"for i equals zero to ten, pre-increment i, and do some stuff. "
 
J

Jordan Abel

Why? The final result [i.e. what ++i evaluates to] is what is tested
for being less than 10. I tihnk the real issue is that it's not what
you're used to.

Because unless you know the C grammar, it can read as
"for i equals zero to ten, pre-increment i, and do some stuff. "

How is that any less clear than using i++ ?
 
M

Mabden

Except for for() loops.
A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.

Who gives a **** about trainees?! ;-)

That said, we are professional _writers_ here. We should write in such a
way that others understand. The _hackers_ newsgroup is over there --->
And my guess is that most are relatively agnostic. Or think its a daft
debate.

I read that as "draft debate" and thought there was a new standard
coming out... ;-)
I disagree that it is, but its largely irrelevant. :)


I like ++i as a stand-alone statement, but i++ in my loops. For
instance:


for (i=0; i < 10; i++)
++j;


This reads (to me) as "for i equals zero, and while i is less than 10,
do the loop and then increment i; [the loop]increment j".
 
J

Jordan Abel

A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.

Do what? the ++ is evaluated at the end of the loop body, and the
variable's new value is used both for the comparison and for the next
instance of the loop body. Where after the increment is the "original
value" used?

for(A;B;C) { D; }

becomes [barring continue and break]

A; while(B) {
D;
C;
}

for(i=0;i<3;i++) { printf("%d",i); }

i=0; [0] i<3 prt i++ [1] i<3 prt i++ [2] i<3 prt i++ [3] !(i<3) done

output: 0123
 
P

pete

Jordan said:
for(i=0;i<3;i++) { printf("%d",i); }

That means exactly the same thing as:
for(i=0;i<3;++i) { printf("%d",i); }

I have no idea of what Mark McIntyre thinks he's trying to say.
 
M

Mark McIntyre

Why? The final result [i.e. what ++i evaluates to] is what is tested
for being less than 10. I tihnk the real issue is that it's not what
you're used to.

Because unless you know the C grammar, it can read as
"for i equals zero to ten, pre-increment i, and do some stuff. "

How is that any less clear than using i++ ?

Because that reads
"for i equals zero to ten, and do some stuff and increment i
afterwards. "
 
M

Mark McIntyre

Do what? the ++ is evaluated at the end of the loop body, and the
variable's new value is used both for the comparison and for the next
instance of the loop body. Where after the increment is the "original
value" used?

Inside the body of the loop. Are you hard of reading?
 
M

Mark McIntyre

On Tue, 15 Nov 2005 12:23:12 GMT, in comp.lang.c , pete

This is after the typographical location of the statement "++i"
I have no idea of what Mark McIntyre thinks he's trying to say.

Luckily I do, but the folks who've commented on this thread are so
incredibly wrapped up in the "well its obvious it doesn't mean that"
attitude, that I fail to see any point trying to explain again.

Its like trying to explain why one finds the rules of baseball
inexplicable. People who *know* the rules think you're an idiot
because *obviously* the rules are simple. I imagine I'm much the same
when some yank complains that cricket is complicated.
 
P

pete

Mark McIntyre wrote:
A heck of a lot of learners do however, IME. YMMV etc. You know and I
know that the ++ is evaluated, but the original value of i used in the
loop body. My experience of trainees is that they find that odd.

In a for statement, expr-3 is evaluated as a void exprssion.

It is evaluated for side effects only and
the value of expr-3 is never used.

(++i) and (i++) have the same side effect.
There is no difference between using either form,
as expr-3 in a for statement.
 

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

Similar Threads

Help with a C program 27
Help with a simple c program 8
Working with the for loop 2
Change of program 1
Pointers 16
scanf for char input getting skipped 0
scanf for char input getting skipped 9
Newbie with an error 6

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top