密码程序 - 错误:未使用的变量

时间:2015-09-24 02:39:26

标签: c variables scope cs50 caesar-cipher

我一直收到以下错误:

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.

我的代码如下。我很困惑为什么我会收到错误。如果我采用变量" key"在do while循环之外它运行正常。只在输入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; 
 }

1 个答案:

答案 0 :(得分:2)

那些Whacky编译器作者和他们疯狂无意义的错误......

喔。坚持下去,也许它们很有用:

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不仅仅是do while的本地,可以在其他地方使用。 (注意:仍然没有100%清楚字符串是什么......)

PS - 是C或C ++ - C没有string类型。如果它是C ++那么你为什么要使用strlen()