我对编码很新,对C来说很新。
我试图在c中编写一个程序,要求用户输入并根据用户输入,将特定文本打印到.txt文件。请查看我的代码,让我知道我做错了什么。
请查看下面的代码。非常感谢您的帮助。
马特
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE * fptr;
int main(){
char name[32];
char partNum[32];
char desc[250];
char ans1;
char ans2;
char sn[50];
char ansRep;
fptr = fopen("C:\\Users\\mgreene\\Documents\\EmailTemplates\\QCIssue.txt", "w");
if (fptr ==0)
{
printf("Error--file could not be opened\n");
exit(1);
}
printf("What is the customer's first name?\n");
scanf("%s", name);
printf("With which Part number is the user experiencing difficulties?\n");
printf("Enter Part Number:");
scanf("%s", partNum);
printf("Please enter a brief description of the problem\n");
scanf("\n%s", desc);
printf("Does the product have a serial number?\n");
scanf("%c", &ans1);
if (!strcmp(&ans1, "y")== 0)
{
printf ("Do you know the serial number?\n");
scanf("\n%c", &ans2);
if (!strcmp(&ans2, "y")==0)
{
printf ("Please enter the SN:\n");
scanf("\n%s", sn);
fprintf(fptr, "\nHi %s,\n\nI hope this message finds you well.\n\nI write to you today as the Quality Manager at Blank. I received a report that you're experiencing difficulties with part: %s, %s; specifically, the report indicates that %s. Is that an accurate description of the problem? Firstly please accept my apologies on behalf of Blank for the difficulties you're experiencing with this part. As an ISO9001:2008 compliant organization, Blank takes all issues related to customer satisfaction very seriously. It is our intention to resolve this matter as soon as is possible.\n\n", name, partNum, sn, desc);
}
else
{
fprintf(fptr, "\nHi %s,\n\nI hope this message finds you well.\n\nI write to you today as the Quality Manager at Blank. I received a report that you're experiencing difficulties with part: %s; specifically, the report indicates that %s. Is that an accurate description of the problem? Firstly please accept my apologies on behalf of Blank for the difficulties you're experiencing with this part. As an ISO9001:2008 compliant organization, Blank takes all issues related to customer satisfaction very seriously. It is our intention to resolve this matter as soon as is possible.\n\nBefore I can begin an investigation into this problem, I'll need the serial number from that unit. Can you please forward me the serial number as soon as you're able? Once I have that, I can begin the investigation on our end. Thanks.\n\n", name, partNum, desc);
}
}
else if (strcmp(&ans2, "y")==0)
{
printf("Will Blank be sending the customer a replacement? Please enter y or n\n");
scanf("\n%c", &ansRep);
if (!strcmp(&ansRep, "y")==0)
{
fprintf(fptr, "Blank can send you a replacement product as soon as is possible. In order to ensure that the replacements are shipped to the correct address, will you please confirm you shipping address via email? Thanks.\n\n");
fprintf(fptr, "Thank you for your assistance in resolving this matter. Please let us know if you have any additional questions, comments, or concerns about this specific issue or any issues related to products distributed by Blank.\n\n");
fprintf(fptr, "Have a great day!");
}
else
{
fprintf(fptr, "Thank you for your assistance in resolving this matter. Please let us know if you have any additional questions, comments, or concerns about this specific issue or any issues related to products distributed by Blank.\n\nHave a great day!");
}
}
fclose (fptr);
return (0);
}
答案 0 :(得分:2)
if (!strcmp(&ans1, "y")== 0)
错误,可能会修改为if(ans1 != 'y')
。您应该对代码中的if (!strcmp(&ans2, "y")==0)
,if (strcmp(&ans2, "y")==0)
,if (!strcmp(&ansRep, "y")==0)
进行类似的修改。
对于scanf("%c", &ans1);
,您可以按如下方式重写:
scanf(" %c", &ans1);
答案 1 :(得分:0)
首先要做的事情是:ans1和ans2是字符;你正在做strcmp(&amp; ans2,“y”) - 这里,“y”是一个字符串(以NULL结尾的字符数组),因为它是双引号。在其他任何事情之前,你应该用
之类的东西替换所有这些变量的比较if(ans1 == 'y')
另外,当你阅读一个字符时,你应该在%c前面添加一个空格,以防万一有空格或前一个输入的新行 - scanf(“%c”,&amp; ans1); < / p>
答案 2 :(得分:0)
删除\n
中的scanf("\n%s" [...]
并在%c
和scanf("%c"
scanf("\n%c"
之前添加空格(删除\n
)可能有所帮助。
答案 3 :(得分:0)
你可以创建一个简单的函数来捕捉来自scanf
void clear_input_buffer(void)
{
char buffer[100];
fgets(buffer, 100, stdin);
}
然后在scanf
scanf("%s", name);
clear_input_buffer();
答案 4 :(得分:0)
简短的回答:我认为你的字符串包含垃圾!
C中的字符串必须以空字节(&#39; \ 0&#39;)结束。当您在堆栈(或堆)上创建一个char数组时,该数组的内容可能会填充您的内存当时所遇到的任何垃圾。尝试运行这个小程序,看看我的意思。有人可能会预测这个程序将打印3个空白行,但它应该打印3行随机垃圾(每次运行此程序时,垃圾应该是不同的)。
#include <stdio.h>
int main(){
char name[32];
char partNum[32];
char desc[250];
printf("%s\n", name);
printf("%s\n", partNum);
printf("%s\n", desc);
}
您可以在已发布的程序中改进一些内容,但要专门解决包含垃圾的字符串,这是一个简单的解决方案:
将char name[32];
之类的行替换为char *name = calloc(32, sizeof char);
,并确保在程序结束时释放这些内存分配,例如free(name)
。
calloc
将它分配的所有内存设置为NULL,这样你的字符串就不再包含垃圾了。
如果您在输出文件中遇到垃圾,那应该可以解决问题。
另请注意
您正在使用固定大小的char数组。如果用户决定输入的内容超出预期,会发生什么?输入将渗入内存的其他部分,并可能影响其他变量的值。