0

i have a problem with this pset. My program compiles properly, but when i'm launching it with key, it stops before get_string... When i'm using the only_digits function which is commented in bottom of this file it runs well, program is getting string and making cipher, but check50 tells me that: handles non-numeric key timed out while waiting for program to exit

I dont know how to make it running properly, can you help me?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    int length = strlen(argv[1]);
    for (int j = 0; j <= length; j++)
    {
        if (isdigit(argv[1][j]) != 0)
        {
            printf("Success\n");
            return 0;
        }
        else
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    int key = atoi(argv[1]);
    if (key < 0)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    string plaintext = get_string("plaintext : ");
    int plain = strlen(plaintext);
    char c[plain];
    for (int i = 0; i <= plain; i++)
    {
        if (isalpha(plaintext[i]) && isupper(plaintext[i]))
        {
            c[i] = ((((plaintext[i] + key % 26) - 'A') % 26) + 'A');
        }
        else if (isalpha(plaintext[i]) && islower(plaintext[i]))
        {
            c[i] = ((((plaintext[i] + key % 26) - 'a') % 26) + 'a');
        }
        else
        {
            c[i] = (plaintext[i]);
        }
    }
    printf("ciphertext: %s\n", c);
}

// string only_digits(string s) {
//         int length = strlen(s);
//         for (int j = 0; j <= length; j++)
//         {
//            if (isdigit(s[j]) == 0)
//             {
//                 return 0;
//             }
//             else
//             {
//                 printf("Usage: ./caesar key\n");
//                 break;
//             }
//         }
//     return s;
// }
1
  • Please edit the question and replace the screen shots with pasted text of the program. And remember to format it properly. Easily done by selecting the text and clicking the {} icon in the format bar. Commented Mar 6, 2022 at 19:44

1 Answer 1

0

The code is terminating early because of this code:

for (int j = 0; j <= length; j++)
{
    if (isdigit(argv[1][j]) != 0)
    {
        printf("Success\n");
        return 0;
    }
    else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

A return statement will always terminate main() or any function that it is in. Since the code has a return statement in both the if statement code block and the else clause, the program will always terminate on the first pass through this for loop.

Programming tip: There's a lot of unnecessary code in this for loop. First, it's not necessary to compare the return result of the isdigit call to 0. Instead, just let the if statement test the return value. Second, an if/else combination isn't needed either. Only the if code block is needed. Put those two techniques together and add the NOT operator, !, you can greatly simplify the code:

for (int j = 0; j <= length; j++)
{
    if ( !isdigit(argv[1][j]) )
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

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 .