gpt4 book ai didi

C 语言 - 将数组打印到文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 02:06:35 24 4
gpt4 key购买 nike

我正在尝试用 C 语言编写一个程序,该程序可以接收数组结构(非并行)形式的员工信息并将其输出到文本文件中。该程序将比较职位代码并计算员工的佣金率。我有两个问题:

1 - 即使我输入 TEL、SAL、SSR 之一,销售人员的佣金始终为 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);
}*/

}

最佳答案

要回答问题的第二部分(关于程序崩溃,以及将文件保存在同一目录中):您打开同一个文件进行输入和输出,但中间没有关闭;不知道为什么输入甚至在那里。假设(为简单起见)您只想将键盘输入的内容保存到文件中,您可以删除所有对 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 - 它只是添加到输出中 - 但我决定不删除它。

祝编码顺利!

关于C 语言 - 将数组打印到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20085695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com