0

The below code lost a mark; check50 gave the explanation just below. A different time-out query (by Abhijit) appears to have had a different cause. The check50 line used: check50 cs50/problems/2021/x/substitution Check50 gave this explanation:

:( handles invalid characters in key
Cause
timed out while waiting for program to exit
Log
running ./substitution ZWGKPMJRYISHFEXQON2DLUACVT...**

Lines 47-50 below (i.e. // error if cipher contains non-alpha value) aim to handle non-alphabetic chars. Also, while running checks, the substitution key (above) that caused the check50 error was picked up another way--it has fewer than 26 chars. I'm still unsure why check50 'timed out'.

Any help (or other improvements) with the below would be much appreciated--thanks in advance!

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

const int LEN = 26;

int alphaix[LEN];

char alpha[LEN];

int main(int argc, string argv[])
{
    
    string err = "Input error: ";

    if (argc == 2)
    {
        string cipher = argv[1];
        int ciphlen = strlen(cipher);

        // check ciphlen against LEN
        if (ciphlen == LEN)
        {
            for (int i = 0; i < LEN; i++)
            {
                // assign cipher[i] to temp
                char temp = cipher[i];

                // check if cipher char is alpha
                if (isalpha(temp))
                {
                    // populate alpha arr with cipher lower chars
                    if (islower(temp))
                    {
                   // alphaix is a diagnostic var only
                        alpha[i] = temp;
                        alphaix[i] = temp - 97;
                    }
                    // populate alpha arr with cipher upper chars
                    else
                    {
                        alpha[i] = temp;
                        alphaix[i] = temp - 65;
                    }
                }
                // error if cipher contains non-alpha value
                else
                {
                    printf("%s cipher contains non-alpha value\n", err);
                    **// insertion of this single line below fixed the error described in this question!**
                    **Return 1;**
                }
            }
        }
        // if ciphlen != LEN
        else
        {
            printf("%s must contain 26 chars\n", err);
            return 1;
        }

        // ciphertext duplicate check
        for (int i = 0; i < LEN; i++)
        {
            char temp = toupper(cipher[i]);
            for (int j = (i + 1); j <= LEN; j++)
            {
                char temp2 = toupper(cipher[j]);
                if (temp2 == temp)
                {
                    printf("%s %c, %c duplicate cipher char\n", err, temp, temp2);
                    return 1;
                }
            }
        }
    }
    // if too few/many command-line arguments
    else
    {
        printf("%s c-line arg\n", err);
        return 1;
    }

    /*
    // diagnostic only, print alpha, alpha index and array
    for (int k = 0; k < LEN; k++)
    {
        printf("alpha[%i], %c: %i; ",k, alpha[k], alphaix[k]);
    }
    printf("\n");
    */

    // collect plaintext from user
    string plaintxt = get_string("plaintext: ");

    int plen = strlen(plaintxt);
    int tempix = 0;

    printf("ciphertext: ");

    for (int i = 0; i < plen; i++)
    {
        // assign plaintxt[i] to temp
        char temp = plaintxt[i];

        // check temp is alpha
        if (isalpha(temp))
        {
            // if temp is lower
            if (islower(temp))
            {
                // calculate temp index
                tempix = temp - 97;
                for (int j = 0; j < LEN; j++)
                {
                    // compare temp index to alpha array
                    if (tempix == alphaix[j])
                    {
                        temp = tolower(alpha[tempix]);
                    }
                }
                // print ASCII char
                printf("%c", temp);
            }
            // if temp char is upper
            else
            {
                tempix = temp - 65;
                for (int j = 0; j < LEN; j++)
                {
                    if (tempix == alphaix[j])
                    {
                        temp = toupper(alpha[tempix]);
                    }
                }
                printf("%c", temp);
            }
        }
        // if temp char is non-alpha
        else
        {
            printf("%c", temp);
        }
    }
    printf("\n");
}

1 Answer 1

1

Have you actually tested the program with an invalid char, like a number, in the key? Try using the alphabet with a 2 instead of an a and see what it does.

It should be incredibly obvious why the program "timed out" while check50 was waiting for it to end.

The fix should be very obvious too. If not, leave a message.

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

4
  • Hi Cliff, thanks for your response. I did test the code with all the suggested conditions. In testing, it picks up non-alphabetic chars within the key, duplicate chars, and both too few or too many chars. That's what's puzzling, Check50 reports this specific error but I haven't been able to replicate it in manual testing/debugging.
    – sub2600
    Sep 12, 2021 at 13:14
  • You're right, it does pick it up. What does it do next though? Does the program end or does it ask for input that check50 never provides, so it times out waiting?
    – Cliff B
    Sep 12, 2021 at 21:51
  • Thanks again, Cliff, for having another look. I just went in deep with the debugger and watched it work through some different scenarios--got it!
    – sub2600
    Sep 13, 2021 at 15:59
  • 1
    ... A simple mistake in the end. I'd forgotten to include an exit line after detecting a non-alpha value, so I was getting an error message but the program kept going. I've added 'Return 1;' at 'line 50' in the above code, and it now gets full marks in Check 50.
    – sub2600
    Sep 13, 2021 at 16:07

You must log in to answer this question.

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