cs50: get_string doesn't ask for input

32 Views Asked by At

I've been trying to write an encryption program based on CS50's PSET2 - substitution. For some reason my get_string fails to get input from the user, and compiler doesn't detect an error. Nothing really changes compared to the substitution problem set except an if that goes in before the main code. Any help much appreciated.

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

int main(void)
{
printf("Encrypt message or decrypt message? \n (choose 'e' or 'd' )\n");

char x;
scanf("%c", &x);

if (x == 'd')
{
    char siskey_d[26] = "mnbvcxzlkjhgfdsapoiuytrewq";
    char alphabet_d[26] = "abcdefghijklmnopqrstuvwxyz";    
    
    string text_d = get_string("Plaintext: ");
    int n_d = strlen(text_d);
    
    printf("%s", text_d);        
    
    printf("\nCiphertext: ");
    for (int i = 0; i < n_d; i++)
    {
        if (isalnum(text_d[i]))
        {
            for (int j=0; j<26; j++)
            {
                if (islower(text_d[i]))
                {
                    if (text_d[i] == siskey_d[j])
                    {
                        printf("%c", tolower(alphabet_d[j]));
                    }
                }
                else if(isupper(text_d[i]))
                {
                    if (text_d[i] == toupper(siskey_d[j]))
                    {
                        printf("%c", toupper(alphabet_d[j]));
                    }
                }
                else if (isdigit(text_d[i]))
                {
                    printf("%c", text_d[i]);
                    break;
                }


            }
        }
        else if (ispunct(text_d[i]) || isspace(text_d[i]))
        {
            printf("%c", text_d[i]);
        }


    }
    printf("\n");
    
    
}
else if( x == 'e')
{
    char alphabet[26] = "mnbvcxzlkjhgfdsapoiuytrewq";
    char siskey[26] = "abcdefghijklmnopqrstuvwxyz";    
    
    string text = get_string("Plaintext: ");
    int n = strlen(text);

    
    printf("\nCiphertext: ");
    for (int i = 0; i < n; i++)
    {
        if (isalnum(text[i]))
        {
            for (int j=0; j<26; j++)
            {
                if (islower(text[i]))
                {
                    if (text[i] == alphabet[j])
                    {
                        printf("%c", tolower(siskey[j]));
                    }
                }
                else if(isupper(text[i]))
                {
                    if (text[i] == toupper(alphabet[j]))
                    {
                        printf("%c", toupper(siskey[j]));
                    }
                }
                else if (isdigit(text[i]))
                {
                    printf("%c", text[i]);
                    break;
                }


            }
        }
        else if (ispunct(text[i]) || isspace(text[i]))
        {
            printf("%c", text[i]);
        }


    }
    printf("\n");    
}
else
{
    printf("choose the correct letter for encryption or decryption of message. Choose ('e' or 'd'\n)");
}

}

0

There are 0 best solutions below