ndjustin20 ndjustin20 - 3 months ago 9
C Question

Cipher program — error: unused variable

I keep getting the following error:

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.


My code is below. I'm confused as to why I'm getting an error. If I take the variable "key" out of the do while loop then it runs fine. Only get the error when putting in the do while loop.

#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;
}

Answer

Those Whacky compiler writers and their crazy meaningless errors...

Oh. Hang on, perhaps they are useful:

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 's scope is limited to within the { } pair it is declared within (i.e. the do-while loop) and never used anywhere else. What do you know - just like the compiler said error: unused variable 'key'

Try this:

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);

Now string is not local to just the do while and can be used elsewhere. (note: still not 100% clear what string is...)

PS - is this C or C++ - C doesn't have a string type. And if it's C++ then why are you using strlen()?