所以这是我在C中的第一个超初学者计算机编程问题。
我需要进行设置,以便有人可以在输入中输入他们的全名。这是规格的一部分 -
“你需要做一些思考才能弄清楚如何将打印的名字与所有其他列对齐。第一个提示是它涉及将字符串连接在一起,称为连接。请试一试,如果您无法弄明白,请查看此文件夹中的下一个文档;它包含其他提示。此分配的部分目的是隐式教您连接。请勿使用制表符(\ t)并确保您的C / C ++编辑器不生成制表符。
请勿在此程序中使用gets()。使用scanf()输入交互信息。如果你尝试使用gets(),你可能会非常沮丧。
实质上,报告中出现的所有数字都应该是右对齐和十进制对齐的。出现在摘要中的所有数字都应该没有前导空格(除了通常将数字与前一个单词分开的数字之外)。每小时工资金额可以低于10.00,所以要非常小心你的格式。样本输出看起来是正确的,但是如果事情与小于10.00美元的小时工资没有正确对齐,你仍然可以停靠半个点。“ 其他提示:
您可以假设员工姓名始终是两个名称,一个名字和一个以空格分隔的姓氏。还假设在名字或姓氏中从不存在任何空格。这允许您使用两个scanf()调用而不是一个gets()调用。 gets()会引入一些奇怪的东西,使得事情在以后不能正常工作。
您也可以假设这两个名称的长度都不超过10个字符。
来自Process的另一名员工的输入?问题应该是单个字符。假设N或n将停止循环,但任何其他字符将继续循环。
任何人都知道如何做到这一点?当我使用gets(他说不要做)时,第二次循环拧起来,它会在一行中询问名称和工资。如果我尝试使用2个scanf语句,我会发生崩溃或仅输入一个名称。
我认为唯一的方法是将名称输出到文本文件,然后再次读取它们。但还有其他方法吗?我不允许单独询问这些名字。用户可以输入一个带有一个空格的全名,如规范中所述。
这是我到目前为止编写的代码。我还需要所有总额,加班时间和正常工作时间的总数。
//stupid program
#include <stdio.h>
#include <strings.h>
#include <math.h>
//global variables
FILE *reportfile; //output file
char department[21];
int count;
char name[21];
float hoursworked;
float hourlywage;
float overtimehoursworked;
float overtimehourlywage;
float gross;
char again;
char firstname;
char lastname;
float tothoursworked;
float totovertimehoursworked;
float totgross;
const float overtimerate = 1.5;
const float overtimethreshold = 40; //hours needed to get overtime
//function prototypes
void GetInfo(void);
void Finalreport(void);
//main
int main(void)
{
reportfile = fopen("c:\\class\\kpaul-pay.txt","w"); //open output file
//////////////////////////////////////////////////
// initialize accumulating variables
/////////////////////////////////////////////////
count = 0;
tothoursworked = 0;
totovertimehoursworked = 0;
totgross = 0;
GetInfo();
fclose(reportfile); //close output file
return 0;
}
void GetInfo (void)
{
printf("Mountain Pacific Corporation\n");
printf("Department Salary Program\n\n");
printf("Please enter the name of the department: ");
gets(department);
fprintf(reportfile, "Mountain Pacific Corporation\n");
fprintf(reportfile, "Department Salary Program\n\n");
fprintf(reportfile, "%s\n\n", department);
fprintf(reportfile, "Employee Reg Hrs Overtime Hrs Gross\n");
fprintf(reportfile, "-----------------------------------------------------------------\n");
do {
printf("\nEnter employee #1: ");
gets(name);
printf("Enter the hourly wage of %s", name);
scanf("%f", &hourlywage);
printf("\nEnter total number of hours: ");
scanf("%f", &hoursworked);
if (hoursworked<=overtimethreshold)
overtimehoursworked = 0;
else if (hoursworked > overtimethreshold)
overtimehoursworked = hoursworked - overtimethreshold;
gross = (hoursworked*hourlywage) + (overtimehoursworked*overtimehourlywage);
fprintf(reportfile, "%s%16.2f(%4.2f)%12.2f(%4.2f) $%7.2f", name, hoursworked, hourlywage, overtimehoursworked, hourlywage * overtimerate, gross);
tothoursworked = tothoursworked + hoursworked;
totovertimehoursworked = totovertimehoursworked +overtimehoursworked;
totgross = totgross + gross;
printf("\n");
printf("Would you like another conversion? ");
scanf ("%s", &again);
printf("\n\n");
} while (again!='N' && again!='n');
}
答案 0 :(得分:1)
需要小心使用scanf
格式字符串。 %s
表示由空格分隔的字符串,在非技术术语中,scanf
将读取字符,直到找到空白字符(如制表符或空格)。
这会导致读取多个单词的问题,这可以通过在格式字符串中使用多个%s
来解决,您必须为每个单词提供一个单独的数组作为格式字符串之后的参数,例如
scanf("%s%s%s", string1, string2, string3)
此scanf
调用需要来自用户的三个单词,其中每个单词由空格分隔。确保为每个阵列分配了内存,否则您将写入程序不正常的内存,导致程序崩溃。通过在格式字符串中使用scanf
多次和%s
,可以实现与上述代码完全相同的效果。
如果您需要将所有内容作为一个字符串使用相同的数组,则可以使用strcat
将两个字符串连接在一起。该函数易于使用,但在调用strcat
之前,请再次确保您的参数是已分配的数组,否则可能会导致崩溃或意外行为。
以下是对这两个功能的引用:
scanf
- http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
strcat
- http://www.cplusplus.com/reference/clibrary/cstring/strcat/
希望这有助于=]
答案 1 :(得分:0)
scanf
的 %s
将接受以空格分隔的字符串。
崩溃很可能是由输入字符串溢出(或无法保留空间)引起的。