2014-11-01 89 views
-3

我對c編程語言很陌生。我寫了這段代碼,我想知道是否有任何方法 寫這個代碼更短,更有效?變量和scanf函數的問題

代碼:

#include <stdio.h> 

int main(){ 

    printf("This is my first program.\nPlease put in your name...\n"); 

    char letter[5]; 


    scanf("%c%c%c%c%c%c", &letter[0], &letter[1], &letter[2], &letter[3], &letter[4], &letter[5]); 


    if(letter[0] == 't' && letter[1] == 'r' && letter[2] == 'a' && letter[3] == 'v' && letter[4] == 'i' && letter[5] == 's'){ 
     printf("Access Granted\nWelcome Travis.\n"); 
     return 0; 
    } 



    else{ 
     printf("You are not autorized.\nThis program will exit now...\n"); 
     getchar(); 

    } 

    }if(letter[0] == 'b' && letter[1] == 'o' && letter[2] == 'b'){ 
     printf("Access Granted\nWelcome Bob.\n"); 
     return 0; 
    } 
+0

對於工作代碼和代碼審查 - 請參閱codereview.stackexchange.com – 2014-11-01 14:50:06

+1

修復後一對大括號:http://coliru.stacked-crooked.com/a/ac471e129d93ece1 – chris 2014-11-01 14:50:06

+0

你讀過[scanf(3)]的文檔嗎(http://man7.org/linux/man-pages/man3/ scanf.3.html)(你應該測試它的結果)?你是否編譯過所有警告和調試信息('gcc -Wall -Wextra -g')?你**使用調試器**('gdb')嗎? – 2014-11-01 15:49:04

回答

1
scanf("%c%c%c%c%c%c", &letter[0], &letter[1], &letter[2], &letter[3], &letter[4], &letter[5]); 

是錯誤的,因爲letter是5元長,指標0-4。所以,在上面的scanf中,letter[5]不是有效的位置。你要走出陣列的界限。爲了修正它,只要申報尺寸7的該陣列(6個字符爲「特拉維斯」 1爲\0在末端)代替5:

char letter[7]; 

然後,scanf可以縮短使用%s

scanf("%6s",letter); 

接下來,

if(letter[0] == 't' && letter[1] == 'r' && letter[2] == 'a' && letter[3] == 'v' && letter[4] == 'i' && letter[5] == 's') 

而且

if(letter[0] == 'b' && letter[1] == 'o' && letter[2] == 'b') 

可以利用從string.hstrcmp功能縮短,使用邏輯或運算合併:

if(strcmp(letter,"bob")== 0 || strcmp(letter,"travis") == 0) 
{...} 

全部放在一起,

#include <stdio.h> 
#include <string.h> 

int main(){ 

    printf("This is my first program.\nPlease put in your name...\n"); 

    char letter[7]; 


    scanf("%6s", letter); 


    if(strcmp(letter,"bob")== 0 || strcmp(letter,"travis") == 0) 
     printf("Access Granted\nWelcome %s.\n",letter); 
    else 
     printf("You are not autorized.\nThis program will exit now...\n"); 

    getchar(); 
    return 0; //you don't need these in every if and else. 
    } 
+0

很快回復!我真的很喜歡這個論壇:) – travisjayday 2014-11-01 16:15:55

0

在這裏你去

#include <stdio.h> 
    #include <stdlib.h> 

    int main() 
    { 
     unsigned int len_max = 128; 
     unsigned int current_size = 0; 

     char *pStr = malloc(len_max); 
     current_size = len_max; 

     printf("\nEnter your string value:\n"); 

     if(pStr != NULL) 
     { 
     int c = EOF; 
     unsigned int i =0; 
     //accept user input until hit enter or end of file 
     while ((c = getchar()) != '\n' && c != EOF) 
     { 
      pStr[i++]=(char)c; 

      //if i reached maximize size then realloc size 
     if(i == current_size) 
     { 
      current_size = i+len_max; 
      pStr = realloc(pStr, current_size); 
     } 
     } 

     pStr[i] = '\0'; 

     printf("\nLong String value:%s \n\n",pStr); 
     //compare pStr with your defined string for authentication. 
     //e.g if(pStr=="myString"){printf("Access granted!");} 
     //free memory 
     free(pStr); 
     pStr = NULL; 


     } 
    return 0; 
}