2016-04-01 38 views
2

我的任務是寫一個只有小寫字母的醫生的姓氏,並以點('.')結束,用戶將用輸出打印。費用的任務是建立一個醫療預約系統:C:如何輸入名稱並以'。'結尾(dot)

#include<stdio.h> 
#include<stdlib.h> 
int main(){ 
    fflush(stdin); 
    int count = 0, flag = 0; 
    char Nameplate; //last name of the doctor 
    printf("Please enter the last name of your doctor(please type with only small letters):\n"); 
    Nameplate = getchar(); 
    do{ 
     Nameplate = getchar(); 
    } while (Nameplate >= 'a' && Nameplate <= 'z' && Nameplate == '.'); 
    if (!(Nameplate >= 'a' && Nameplate <= 'z' && Nameplate == '.')){ 
     flag = 1; 
    } 
    else if (Nameplate == '\n'){ 
     flag = 0; 
     count++; 
    } 
    if (flag == 1){ 
     printf("Invalid input,"); 
     fflush(stdin); 
     main(); 
    } 
    else if (flag == 0){ 
     printf("\n Your appointment has been successfully canceled.\n\n"); 
    } 
    return 0; 
} 

現在,這段代碼無法正常工作。如果我不使用點,它正在工作,但是當我輸入點時,問題就開始了。

+0

你讀過你自己的問題?你認爲它甚至可讀? – kaylum

+0

我正在努力解決並編輯那個抱歉的問題 –

+1

我試圖讓你的Q有點成形,但是上帝請在閱讀另一個問題之前先閱讀[help]和[ask]。 cc @kaylum – Magisch

回答

3

嘗試

while ((Nameplate >= 'a' && Nameplate <= 'z') || (Nameplate == '.')) 

,而不是

while (Nameplate >= 'a' && Nameplate <= 'z' && Nameplate == '.') 
+1

您可能已經注意到,在這個網站上,沒有人的代碼格式化爲您格式化您的方式。這是有原因的。 – dandan78

+0

我不知道。我是初學者。 – froghramar

+1

每天都有數百名初學者加入Stackoverflow。閱讀幫助,潛伏,熟悉網站_then_開始貢獻。對於平凡的人來說,這樣的痛苦要少得多。 – dandan78

2
即使使用小寫字母和週期測試中的變化

,你還有一個潛在的問題,因爲用戶可能會在一段時間名稱,但最後還是沒有一段時間。

此代碼僅檢查小寫字母,不包括最後一個字符,然後測試最後一個字符是句點。
也有一些名稱長度控制。

int main() 
{ 
    int flag, n; 
    char *Nameplate = malloc(22); //last name of the doctor 
    fflush(stdin); 

    while(1) { 
     printf("Please enter the last name of your doctor(please type with only small letters):\n"); 
     printf("End name with a period/full stop\n"); 
     fgets(Nameplate, 20, stdin); 

     /* test for all valid lower case letters */ 
     flag = 0; 
     for(n = 0; n < strlen(Nameplate) - 2; n++) { 
      if (!('a' <= Nameplate[n] && Nameplate[n] <= 'z')) { 
       /* not a lower case character */ 
       flag = 1; 
       break; 
      } 
     } 

     /* now test for terminating period */ 
     if(Nameplate[strlen(Nameplate) - 2] != '.') { 
      /* no period at end of name */ 
      flag = 2; 
     } 

     /* handle errors or accept */ 
     if(flag == 1) { 
      printf("small letters only\n"); 
     } else if (flag == 2) { 
      printf("remember to end with a period/full stop\n"); 
     } else { 
      /* name was all lower case with terminating period so exit input */ 
      break; 
     } 
    } 

    printf("\n Your appointment with %s has been successfully canceled.\n\n", Nameplate); 
    free(Nameplate); 
    return 0; 
} 
+0

爲什麼'strlen(銘牌)-2'?當然'-1'?我認爲你正在考慮「尾隨nul」,但這並不包含在strlen返回的結果中。 –

+0

@MartinBonner strlen(銘牌)-2返回從由Enter終止的stdin輸入的名稱末尾的句點。我確實檢查了這是''的地方。 '出現在字符串中 - 給它一個自己去確定 – anita2R

+0

明白了。它不是尾隨的nul,它是尾隨的'\ n'。 –

0

如果我知道你想進入一個醫生的名字來設置一個約會,只處理小寫字母,如果最後一個字符是一個'.'取消預約,你可以稍微重新排列你的邏輯和實現這一目標目標仍然保留中間初始標點符號。

此外,任何時候你正在輸入,提供一個提示給你的用戶,讓他們不坐在那裏看着閃爍的光標,想知道程序是否掛起。

在處理大寫/小寫轉換時,沒有理由強制用戶僅輸入一個或另一個。您可以簡單地檢查/轉換他們以透明方式提供的任何輸入。 ctype.h頭文件包含tolowertoupper字符轉換,或者您可以簡單理解7-bit ASCII中的6th-bit是案例位,並根據需要切換它以完成案例轉換。

這只是一個例子,有很多方法把碎片一起:

#include <stdio.h> 

#define MAXC 64 

int main (void) { 

    int c = 0, cnx = 0, i = 0; 
    char name[MAXC] = ""; 

    printf ("\n enter doctor's name (end with '.' to cancel): "); 
    while (i + 1 < MAXC && (c = getchar()) != '\n' && c != EOF) 
     if (('a' <= c && c <= 'z') || c == '.' || c == ' ') 
      name[i++] = c;      /* add to name  */ 
     else if ('A' <= c && c <= 'Z')   /* if upper-case  */ 
      name[i++] = c^(1u << 5);   /* convert to lower */ 
    if (i && name[i-1] == '.') {    /* last is '.'  */ 
     cnx = 1;        /* set cancel flag */ 
     name[--i] = 0;       /* overwrite last '.' */ 
    } 
    else 
     name[i] = 0;       /* nul-terminate name */ 

    if (cnx) /* appointment canceled */ 
     printf ("\n Your appointment with doctor '%s' has been canceled.\n\n", 
       name); 
    else  /* new appointment  */ 
     printf ("\n You have a new appointment with doctor '%s'.\n\n", name); 

    return 0; 
} 

使用/輸出

$ ./bin/appointment 

enter doctor's name (end with '.' to cancel): John J. Marks 

You have a new appointment with doctor 'john j. marks'. 


$ ./bin/appointment 

enter doctor's name (end with '.' to cancel): John J. Marks. 

Your appointment with doctor 'john j. marks' has been canceled. 
相關問題