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] =''\''' ; emp [n] .RegHr = RegHr; emp [n] .wage =工资; emp [n ] .OTHr = OTHr; emp [n] .OTHrPay = OTHrPay; emp [n] .GrossPay = GrossPay; ++数量; } / *打印表格* / printf(" \ n \ nMountain Pacific Corporation \ nn); printf(部门薪资计划\ n \\ nnn); printf(" Employee)Reg Hrs" " Overtime Hrs Gross \ nn"); printf(" ----------- ------------------------------" " -------- ----------------- \ n \ n"); for(n = 0; n<计数; ++ n){ printf("% - 35s%-17s%12f%10f%12f%10f %% 5f", \\ temp [n] .first_name , emp [n] .last_name,emp [n] .RegHr, emp [n] .wage,emp [n] .OTHr,emp [n]。 OTHrPay, emp [n] .GrossPay); } } }HiI appreciate all of the feedback I have recieved. I am enjoyingworking on my program and I hope that it can be appreciated. I have myprogram compiling now and I am continuing to work out the bugs. I havetwo problems that I cannot seem to resolve:1. I want to have someone enter in as many employees as they wantto..and for each employee entered I would like to save the data fromthat entry to print out at the end of the program. I continue to runinto a problem with the loop. I actually did not intend to use arrayshowever I cannot seem to figure outany other way to approach theproblem.2.This second problem is probably due to using arraysintheprogram,however that being said I still want to figure thisout....I am trying to connect two strings together, concatenation. Iunderstandthat strncpy refers to n number of characters versus strcpyis the actual character but the difficulty I am running into has to dowith the syntax. It is evident that I am somehow not correctly dealingwith putting things into memory and then extracting them. Anythoughts??//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 Constantconst float OT = 1.5;//Global Variable DeclarativesFILE*inp;//Global Constants# define FULLNAME 20# define EMPLOYEES 1000char 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);}}}推荐答案 SK写道: 2.第二个问题可能是由于在程序中使用数组正在说我还是想把这个拿出来....我试图将两个字符串连接在一起,连接。我理解strncpy指的是n个字符而不是strcpy 是实际的字符,但我遇到的困难必须用语法来完成。很明显,我在某种程度上没有正确处理将内容放入内存然后提取它们。任何想法?? 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?? 在这里停止处理您的程序并启动另一个仅用于测试 如何strcpy和strncpy工作。另外,阅读 函数的文档。如果您遇到一些非常具体的问题,请发布一个 最小的例子 - 您不断增长的主要项目并不符合最低限度 的例子。 最后,为了处理任意字符串,你需要 malloc / realloc / free用于内存处理和strlen,strcpy和strcat for字符串操作。 * n *变体通常只有在你使用固定大小的数组运行时才需要,对于动态字符串,你最好先调整它们的大小。 UliStop working on your program here and start another one just for testingout how strcpy and strncpy work. Also, read the documentation of bothfunctions. If you have problems with some very specific thing, post aminimal example - your growing main project doesn''t qualify as minimalexample.Lastly, in order to handle arbitrary strings, you will needmalloc/realloc/free for the memory handling and strlen, strcpy and strcatfor the string operations. The *n* variants are usually only necessary ifyou operate on fixed size arrays, for dynamic strings you better resizethem beforehand.Uli 2005年11月13日12:04:08 -0800,在comp.lang.c,SK < ba ****** @ aol.com>写道:On 13 Nov 2005 12:04:08 -0800, in comp.lang.c , "SK"<ba******@aol.com> wrote: //全局定义常数 const浮动OT = 1.5; 你真的不想使用浮动 - 使用双倍。 scanf("%s",department); 正如我之前所说,scanf是一个很糟糕的功能。考虑 如果有人输入22个字符会发生什么。 for(n = 0; n< EMPLOYEES; ++ n){ 通常做n ++ 做{ printf(&\\;输入员工#%d:",count_EMP); scanf("%s%) s",& fn,& ln); fn和ln未定义。假设它们应该是字符串, 你不需要&符号。 scanf("%s",& again); } while(再次==''Y''||再次==''y''); 又是一个字符串,而不是一个字符,所以你需要Y。和y。或者 更好,再次声明为角色并使用%c格式。 / *读入输入* / 你从未要求用户输入任何... numemp = scanf("%11s%11s%f%f%f%f%f",& fn,& ln ,& RegHr,& wage,& OTHr,& OTHrPay,& GrossPay);//Global Defined Constantconst 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. Considerwhat 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". Orbetter 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); 再次,fn和ln don''似乎是被定义的。 你似乎也有一些关于循环的大问题。 后退一步。写下_in english_(或您的母语), 描述程序必须做什么。仔细考虑每个数据需要获取和存储的方式。 然后,一旦你有了设计,就开始编写代码。 - Mark McIntyre CLC FAQ< http://www.eskimo.com/~scs/C-faq/top.html> CLC自述文件:< http://www.ungerhu.com/jxh/clc.welcome.txt> ---- ==发表于Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet新闻== ---- http:/ /www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组 ---- = East和West-Coast服务器场 - 通过加密实现全隐私= ----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), adescription of what the programme has to do. Consider carefully howeach piece of data needs to be obtained and stored.Then, once you have a design, start writing code.--Mark McIntyreCLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups----= East and West-Coast Server Farms - Total Privacy via Encryption =---- Mark McIntyre写道:Mark McIntyre wrote: fn和ln未定义。 他有一堆全局变量,这些都是其中两个。He''s got a bunch of globals, and those are two of them. scanf("%s",& ;;); } while(再次= =''Y''||再次==''y''); scanf("%s", &again); }while(again == ''Y'' || again == ''y''); 再次是一个字符串,而不是一个字符,所以你需要Y。和y。或者更好的是,再次声明为角色并使用%c格式。 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. ''再次''被声明为char。另一个全球 - pete''again'' was declared as a char. another global--pete 这篇关于数组中的Strcpy与Strncpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 08:05