2014-08-29 19 views
-1

我是新来的一般编码,并且很新的C.Ç - 如果其他scanf函数无法正常运行

我想用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

你还需要描述你的问题。 – 2014-08-29 19:31:51

+0

为什么你在'scanf()'中有'\ n'? – TerryG 2014-08-29 19:33:58

+1

使用'scanf(“%c”,&ans1);'。Better:'char ans1 [2],scanf('%1s',&ans1);'best:drop all'scanf()'并使用'fgets()' – chux 2014-08-29 19:42:40

回答

0

首先第一件事情:ANS1和ANS2是炭灰;你正在做strcmp(& ans2,“y”) - 在这里,“y”是一个字符串(由NULL结尾的字符数组),因为它用双引号引起来。别的之前,您应该更换的东西这些变量的所有比较喜欢

if(ans1 == 'y') 

此外,当你万一看你应该在%C的前面添加一个空格字符存在一些空白或偏离之前输入的新行 - scanf(“%c”,& ans1);

+0

感谢您的回复。我最初的设置类似,但没有测试字符。这就说得通了。我根据你的建议编辑了该程序。现在发生了什么是scanf不会在用户被问到“产品是否有序列号?”之后等待答案。非常感谢。 – Matt 2014-08-29 19:39:22

+0

@Matt关于你最后一个问题,你可以参考我的答案。 – ACcreator 2014-08-29 19:44:49

+1

发生这种情况是因为上一个答案中有一个新行。当你阅读一个字符时,你应该在%c之前添加一个空格,以防在前面的输入中出现一些空格或新的一行 - scanf(“%c”,&ans1); – RockOnRockOut 2014-08-29 19:45:03

0

卸下在scanf("\n%s" [...]\nscanf("%c"scanf("\n%c"(除去\n)加入%c之前的空间中可能会有帮助。

+1

'scanf(“\ n%s”'与scanf(“%s”''执行相同。 – chux 2014-08-29 19:41:22

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); 
+0

谢谢,我确实将strcmp更改为if(var =='char')。 \ n来自scanf函数......谢谢,虽然我不明白%c之前的空间用途,但我很感激您的帮助 – Matt 2014-08-29 19:45:35

+0

%c之前的空格有助于省略所有空格,包括'\ n',' \ r','\ t',''等等.. @Matt – ACcreator 2014-08-29 19:48:01

0

你可以创建一个简单的函数从scanf

void clear_input_buffer(void) 
{ 
    char buffer[100]; 
    fgets(buffer, 100, stdin); 
} 

捉流浪狗则包括它scanf

scanf("%s", name); 
clear_input_buffer(); 
+0

嗯....谢谢,我不确定这个函数是干什么的,我很高兴使用它,但是我想理解在这两种情况下都非常感谢您的帮助。我会在每次scanf发生后添加该功能吗? – Matt 2014-08-29 19:44:01

+0

'scanf'做当你敲入回车键时,它不会读入返回字符,通常只是挂起直到下次输入时,这个函数希望清除输入缓冲区中剩下的任何东西。 – TerryG 2014-08-29 19:46:03

+1

“scanf”不会在返回字符中读取点击进入“不完整。什么'scanf()'做高度依赖于它的格式。例如:'scanf(“%s%* 1 [\ n]”,name);'会消耗训练'\ n'。 IAC,最好检查'scanf()'的返回值。由于难以处理意外输入,强大的代码避免了'scanf()'。使用'fgets()'后跟'sscanf()/ strtok()/ strtoi()'等。 – chux 2014-08-30 02:57:14

0

简答:我觉得你的st戒指包含垃圾!

C中的字符串必须以空字节('\ 0')结尾。当您在堆栈(或堆)上创建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,这样您的字符串将不再包含垃圾。

如果您在输出文件中遇到垃圾,应该这样做。

还要注意

您正在使用固定大小的字符数组。如果用户决定输入比预期更长的内容,会发生什么情况?输入将流入内存的其他部分,并可能影响其他变量的值。