0

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)");
}

}

Gio
  • 11
  • 3
  • 1
    Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) which is then read by `get_string()` as an "empty string". – Weather Vane Dec 02 '20 at 14:34
  • Problem isn't solved by leaving a whitespace before the format specifier. – Gio Dec 02 '20 at 15:14
  • That still leaves the following newline the buffer. The single character is also a line input, so read that as a string too and examine the first character. – Weather Vane Dec 02 '20 at 15:16
  • I am so confused. The problem is that scanf stores the new line character at its buffer right? Why does this happen and why is the single character also a new line input? As a solution you suggest to store my characters (d or e) into a string instead of a char? – Gio Dec 02 '20 at 16:00
  • No, the `scanf()` does *not* store the newline. That's the problem, it remains in the input buffer, where it stays for `get_string()` to read without waiting to input another string. The newline is there because you typed it. My suggested solution is to use `get_string()` for the first input too, and check the first character of the input string. – Weather Vane Dec 02 '20 at 16:16
  • Thanks so much. Haven't encountered this problem before! Your solution helped me proceed. I'll be looking into the buffer more to see what causes similar issues with scanf. Does this happen only with scanf or with different input functions as well? – Gio Dec 02 '20 at 17:32
  • It happens with `getchar()` too. Ideally, don't mix the input methods. CS50 is learning material, later on you might use `fgets()` to read all line-oriented input and it you need to extract data from the input string, apply `sscanf()`. – Weather Vane Dec 02 '20 at 17:35
  • got it, thanks mate – Gio Dec 02 '20 at 20:11

0 Answers0