2015-09-24 36 views
-4

我不斷收到以下錯誤:密碼程序 - 錯誤:未使用的變量

clang -ggdb3 -O0 -std=c99 -Wall -Werror vigenere.c -lcs50 -lm -o vigenere 
vigenere.c:29:14: error: unused variable 'key' [-Werror,-Wunused-variable] 
string key = GetString(); 
     ^
1 error generated. 

我的代碼如下。我很困惑,爲什麼我得到一個錯誤。如果我從while循環中取出變量「key」,那麼它運行良好。只有在放入do while循環時纔會出錯。

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

     //Note 97 thru 122 are lowercase letters and 65 thru 90 are uppercase letters 
     //They are separated by exactly 32. A is 65 and a is 97 


int checkIfAlphaAndCase(char argument); //This is a prototype 
int changeLetterToNumber(char argument, int upperOrLower); //This is a prototype 


int main(int argc, string argv[]){ 

    printf("\n"); 

    if(argc < 2){  //If the user fails to enter something 

     //printf("%d\n", argc); 
     printf("You failed!!!! You must enter an argument to be encrypted after the program name \n"); 
     return 1; 

    }else{ 

     do{ 

     printf("Please enter a key at least 7 letters long: "); //Let's start by getting a key from the user 
     string key = GetString(); 

     } while(strlen(key) < 7); 


     //printf("this is the first char: %c", key[0]); 

     int keysLength = strlen(key); 
     int keyChangedToNumbersArray[keysLength]; 
     int alphaAndCaseInt; 
     int letterToNumber; 

     for(int i = 0; i < keysLength; i++){ //By using <= I'll add a null character at the end of the array to know when it ends. 
              //This null character will turn into zero after going through the funtion 
              //changeLetterToNumber and storing the return value in letterToNumber 
     alphaAndCaseInt = checkIfAlphaAndCase(key[i]); 
     //printf("%i", alphaAndCaseInt); //return types are 1 is lower, 2 is upper, and 0 is not a letter 

     letterToNumber = changeLetterToNumber(key[i], alphaAndCaseInt); 
     //printf("This is the char turned into a number: %i", letterToNumber); 

     keyChangedToNumbersArray[i] = letterToNumber; 

     //printf("This is the letter to Number: %i \n", letterToNumber); 
     //printf("\n\n"); 
     //printf("%i\n", keyChangedToNumbersArray[i]); 


     }//This loop changes each letter of the key into a number 

     printf("\n\n"); 
     //printf("Your encrypted data is: "); 
     //printf("%i\n", argc); 
     //printf("%c\n", argv[1][0]); 

     for(int i = 1; i < argc; i++){ //If a user enters [program] then a name or a string we want to skip over the program name 
             //and go straight to the string(s) to encrypt them. argc counts the program name in 
             //its count so we start at 1 to ensure we don't encrypt the program name also. This loop will 
             //run depending on how many words or characters with spaces there are as each one adds 1 to 
             //the argc count 
             //i will start out as 1 

      printf("%i\n", strlen(argv[i])); 

      //printf("%i\n", i); 

       for(int n = 0; n < strlen(argv[i]); n++){ 

       //printf("%c", argv[i][n]); 

        int argLetterInt = checkIfAlphaAndCase(argv[i][n]);//return types are 1 is lower, 2 is upper, and 0 is not a letter 
        //printf("%i", argLetterInt); 

        if(argLetterInt == 1){  //return types are 1 is lower, 2 is upper, and 0 is not a letter 
               //This if statement is checking to see if the first letter of the argument is a lowercase letter and a letter. 

        if(keyChangedToNumbersArray[n] > 0 && argv[i][n] + keyChangedToNumbersArray[n] > 122){ 

         printf("%c\n", argv[i][n]); 

         }else if(keyChangedToNumbersArray[n] > 0 && argv[i][n] + keyChangedToNumbersArray[n] < 122){ 

         printf("%c\n", keyChangedToNumbersArray[n] + argv[i][n]); 


         } 

        } 

        if(argLetterInt == 2){  //return types are 1 is lower, 2 is upper, and 0 is not a letter 
               //This if statement is checking to see if the first letter of the argument is a lowercase letter and a letter. 

        if(keyChangedToNumbersArray[n] > 0 && argv[i][n] + keyChangedToNumbersArray[n] > 90){ 

         printf("%c\n", argv[i][n]); 

         }else if(keyChangedToNumbersArray[n] > 0 && argv[i][n] + keyChangedToNumbersArray[n] < 90){ 

         printf("%c\n", keyChangedToNumbersArray[n] + argv[i][n]); 

         } 
        } 



     }//end for loop 

     //printf(" "); 
     //printf("%s ", argv[i]); 
     printf("\n"); 
     return 0; 

    }//End Main Function 
    } 
    } 






int checkIfAlphaAndCase(char argument){ 

    if(argument >= 97 && argument <= 122){ 

     return 1; //must be lowercase 

    }else if(argument >= 65 && argument <= 97){ 

     return 2; //must be uppcase 

    }else{ 

     return 0; //must not be a letter 


    } 
    } 


int changeLetterToNumber(char argument, int upperOrLower){ //Change the char to a number a and A both = 0 

int returnValue = 0; 

    if(upperOrLower == 1){ 

     int lowerCaseLetters[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 

     for(int i = 0; i < 26; i++){ 

      if(argument == lowerCaseLetters[i]){ 
       returnValue = i; 
       break; 

      } 

     } 

    }else if(upperOrLower == 2){ 

     int upperCaseLetters[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; 

     for(int i = 0; i < 26; i++){ 

      if(argument == upperCaseLetters[i]){ 
       returnValue = i; 
       break; 

      } 

     } 

    } 

    return returnValue; 
} 

回答

2

那些怪誕的編譯器作家和他們的瘋狂毫無意義的錯誤...

哦。等一等,也許他們是有用的:

do { 
    printf("Please enter a key at least 7 letters long: "); //Let's start by getting a key from the user 
    string key = GetString(); 

} while(strlen(key) < 7); 

key的範圍是{ }對被內聲明中限於(即do-while循環)和從未使用過其他地方。你知道些什麼 - 就像編譯器說error: unused variable 'key'

試試這個:

string key; 
do { 
     printf("Please enter a key at least 7 letters long: "); //Let's start by getting a key from the user 
     key = GetString(); 

    } while(strlen(key) < 7); 

現在string是不是本地的,而只是該做和可以在其他地方使用。 (注:仍然不是100%清楚什麼字符串是...)

PS - 是這個C或C++ - C沒有string類型。如果它是C++,那你爲什麼使用strlen()

+0

使用特定於該類的字符串庫。所以我使用C並且也使用了一個字符串,我知道它是一個字符數組。 – ndjustin20

+0

C也沒有類......如果'string'是某種'struct',那麼將它傳遞給'strlen()'不會按預期工作。如果它只是'char *'的'typedef'或'#define',那麼它就更有意義。 – John3136

+0

密鑰直接在while循環下使用。約翰我不知道你在說什麼。以下代碼行直接在while循環之後:int keysLength = strlen(key); – ndjustin20