0

I'm really not sure why my code doesn't work for char's that are L, and when 20 is added to them on line 34. Appreciate the help for a beginning coder please:)

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

//shows that argc will run if equals 2
    if (argc == 2)
    {
        int i, n, userinputint;
        bool fufilconditions = false;
        userinputint = atoi(argv[1]);

        for (i = 0, n = strlen(argv[1]); i < n; i++)
        {
            if (isdigit(argv[1][i]) && userinputint > 0)
            {
                fufilconditions = true;
                }
            //either notdigits, or not positive
            else
            {

                printf("Usage: ./caesar key\n");
                return 1;
                }

        }

        //basically if it passes all the conditions
        if (fufilconditions == true)
             {
                    string plaintext = get_string("Plaintext: ");
                    printf("ciphertext:");

                    int a, b;
                    for (a = 0, b = strlen(plaintext); a < b; a ++)
                    {

                        if (islower(plaintext[a]))
                        {

                            plaintext[a] += (userinputint % 26);

                            if (plaintext[a] > 122)

                            {
                                plaintext[a] -= 122;
                                plaintext[a] += 96;
                                printf("%c", plaintext[a]);

                            }

                            else
                            {
                            printf("%c", plaintext[a]);
                            }
                        }

                        else if (isupper(plaintext[a]))
                        {
                            plaintext[a] += (userinputint % 26);

                            if (plaintext[a] > 90)
                            {
                                plaintext[a] -= 90;
                                plaintext[a] += 64;
                                printf("%c", plaintext[a]);
                            }
                        }

                        else
                            {
                            printf("%c", plaintext[a] );
                            }

                     }

                    printf("\n");
                    return 0;
                 }

    //doesn't fulfill the digit/more than 0 condition
        else
        {
        printf("Usage: ./caesar key\n");
        return 1;
        }




    }







// doesn't fufill argc=2 condition
    else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }




}
0

IT seems like you code does not insure it prints out a letter. So if you cypher L (76-ASCII) by 20 you will get ` (96-ASCII), when you are actually looking for G (71-ASCII). You need to create some sort of loop that insures that it does not print anything else than letter, so that if it is a capital it does not higher then 90 and instead starts counting again at 65.

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 .