2014-08-29 18 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,這樣您的字符串將不再包含垃圾。

如果您在輸出文件中遇到垃圾,應該這樣做。

還要注意

您正在使用固定大小的字符數組。如果用戶決定輸入比預期更長的內容,會發生什麼情況?輸入將流入內存的其他部分,並可能影響其他變量的值。