0

I need help. Does anyone knows how to fix this? I've tried debugging and the problem is that the int k is always seems to be 0 but I don't really understand why :( $

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

if ( argc != 2 ){
    printf("Usage : ./vinegere <key> \n");
    return 1;
}


if(argv[1]){
    for(int z = 0; z < strlen(argv[1]); z++){
        if(!isalpha(argv[1][z])){
        printf("Only alpahabetic key is allowed :)/n");
        return -1;
        } 
    } 
 }  

 printf("Your message: ");
    string t = GetString();

for(int i = 0; i < strlen(argv[1]); i++){ //getting key
    int k = atoi(argv[1][i]);

    for (int j = 0, n = strlen(t); j < n;j++){ // getting text converted
          if(isalpha(t[j])){

            if(isupper(t[j])){
                int a = (( t[j] -65 + k)% 26) + 65;
                printf("%c",a );
                }


            if(islower(t[j])){
                int a = (( t[j]-97 + k)% 26)+97;
                printf("%c",a);
            }

          }
           else 
            printf("%c", t[j]);
    }

    printf("\n");
}

}

1

The fact that k always seems to be 0 is a clue. According to the documentation for atoi(), it will return 0 when the given parameter (in this case, argv[1][i]) is not convertible to a number.

The function atoi() is used to convert a string representation (i.e., a string of numeric characters) of an integer to an actual integer which it will then return. It will not work on alpha characters.

If you review the class material, you will find that a char can be treated as a one byte signed integer, so there's no need to do this.

There are other issues in your code, but they aren't part of your question. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .