我正在尝试使用C语言创建一个程序,该程序可以接收数组结构(非并行)形式的员工信息,并将其输出到文本文件中。 该计划将比较工作代码并计算员工的佣金率。 我有两个问题:
1 - 即使我输入TEL,SAL,SSR之一,salesPerson的佣金也总是为0。这是我的字符串比较的问题吗?
2 - 如何将结果输出到文本文件中(在项目文件夹中)?
编辑 - 我已经想出如何将文件打印到txt中。谢谢大家的帮助!
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
//job codes
char TEL[4] = "TEL";
char SAL[4] = "SAL";
char SSR[4] = "SSR";
//create a sales person structure
struct salesPerson
{
char name[31];
int salesID;
char jobCode[4];
double totalSales;
double commission;
}e[10]; //make an array of 10 employees
int _tmain(int argc, _TCHAR* argv[])
{
//get employee info
for(int i=0; i<3; i++)
{
printf("\nEnter the sales person's name: ");
scanf(" %s", &e[i].name);
printf("\nEnter the sales person's ID: \n");
scanf(" %d%*c", &e[i].salesID);
printf("\nEnter the sales person's job code: \n");
scanf(" %s", &e[i].jobCode);
printf("\nEnter the total sales of the sales person: ");
scanf(" %lf", &e[i].totalSales);
//determine commission based on jobCode
if(strcmp(e[i].jobCode, TEL) == 0)
{
e[i].commission = e[i].totalSales*0.02;
}
if(strcmp(e[i].jobCode, SAL) == 0)
{
e[i].commission = e[i].totalSales*0.05;
}
if(strcmp(e[i].jobCode, SSR) == 0)
{
e[i].commission = e[i].totalSales*0.07;
}
else
{
printf("\n----------");
}
}
printf("\n%d\n", e[0].commission);
printf("\n%d\n", e[1].commission);
printf("\n%d\n", e[2].commission);
//print stuff to txt file
FILE *fpOut, *fpIn;
/*
if ((fpIn = fopen("c:\\temp\\salesEmployees.txt", "w")) == NULL)
{
printf("Error opening the file for processing\n\n");
}
else
{
fpOut = fopen("c:\\temp\\salesEmployees.txt", "w");
for(int i=0; i<3; i++)
{
//fscanf(fpIn,"%[^\n]", &e[i].name);
//fscanf(fpIn,"%f%*c", &e[i].salesID);
fprintf(fpOut, "\nName: %s", e[i].name);
fprintf(fpOut, "\nSalesID: %d*c ", e[i].salesID);
fprintf(fpOut, "\nJob code: %s ", e[i].jobCode);
fprintf(fpOut, "\nTotal sales: %.2lf", e[i].totalSales);
fprintf(fpOut, "\nCommission earned: %.2lf", e[i].commission);
fprintf(fpOut, "\n\n");
}
//fclose(fpOut);
}*/
}
答案 0 :(得分:2)
回答问题的第二部分(关于程序崩溃,并将文件保存在同一目录中):打开相同的文件进行输入和输出,而不是在它们之间关闭;不确定为什么输入就在那里。假设(为简单起见)您只想保存到从键盘输入的文件,您可以删除对fpIn
的所有引用。至于“将文件保存在同一文件夹中” - 只需使用相对路径。当您从c:\my\dir
开始运行时,打开文件salesOutput.txt
会将其写入c:\my\dir\salesOutput.txt
。
完整且有效的代码(一些更改以适应我的编译器设置......):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//job codes
char TEL[4] = "TEL";
char SAL[4] = "SAL";
char SSR[4] = "SSR";
//create a sales person structure
struct salesPerson
{
char name[31];
int salesID;
char jobCode[4];
double totalSales;
double commission;
}e[10]; //make an array of 10 employees
int main(int argc, char* argv[])
{
//get employee info
int i;
for(i=0; i<3; i++)
{
printf("\nEnter the sales person's name: ");
scanf(" %s", &e[i].name);
printf("\nEnter the sales person's ID: \n");
scanf(" %d%*c", &e[i].salesID);
printf("\nEnter the sales person's job code: \n");
scanf(" %s", &e[i].jobCode);
printf("\nEnter the total sales of the sales person: ");
scanf(" %lf", &e[i].totalSales);
//determine commission based on jobCode
if(strcmp(e[i].jobCode, TEL) == 0)
{
e[i].commission = e[i].totalSales*0.02;
}
if(strcmp(e[i].jobCode, SAL) == 0)
{
e[i].commission = e[i].totalSales*0.05;
}
if(strcmp(e[i].jobCode, SSR) == 0)
{
e[i].commission = e[i].totalSales*0.07;
}
else
{
printf("\n----------");
}
}
printf("\n%lf\n", e[0].commission);
printf("\n%lf\n", e[1].commission);
printf("\n%lf\n", e[2].commission);
//print stuff to txt file
FILE *fpOut;
{
if((fpOut = fopen("salesEmployees.txt", "w")) == NULL)
{
printf("Unable to open file - quitting\n");
return -1;
};
int i;
for(i=0; i<3; i++)
{
fprintf(fpOut, "\nName: %s", e[i].name);
fprintf(fpOut, "\nSalesID: %d*c ", e[i].salesID);
fprintf(fpOut, "\nJob code: %s ", e[i].jobCode);
fprintf(fpOut, "\nTotal sales: %.2lf", e[i].totalSales);
fprintf(fpOut, "\nCommission earned: %.2lf", e[i].commission);
fprintf(fpOut, "\n\n");
}
fclose(fpOut);
}
}
显然,添加I / O,输入错误检查等是可取的(也是必要的) - 但是这应该让你超越“它不工作,我不知道为什么”这一点。我确实想知道为什么你在销售ID的打印输出中有*c
- 它只是被添加到输出中 - 但我决定不删除它。
祝你好运!
答案 1 :(得分:1)
始终查看编译器的警告。
printf("\n%d\n", e[0].commission);
您给出整数格式说明符,但参数为double
。它的前几个字节可能为零 - 这就是结果打印为零的原因。尝试将其更改为:
printf("\n%.2lf\n", e[0].commission);
你将获得美元和美分的佣金。其他行也一样。