0

i'm trying to make a crypt and decrypt program using a very simple criptography (just moving three letters foward in the alphabet). To do so, i need to use pointers and something is going wrong when i try to ask the user for the phrase he wants to cryptograph. here's the code, the error is on the "encrypt" function, in the scanf line.

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

#define foreach(item, array) \
    for(int keep = 1, \
            count = 0,\
            size = strlen(array); \
        keep && count != size; \
        keep = !keep, count++) \
      for(item = (array) + count; keep; keep = !keep)

void* encrypt(char *phrase, char *alphabet);

void* decrypt(char *phrase, char *alphabet);

int main() {
    setlocale(LC_ALL, "Portuguese");
    char alphabet[27] = {"abcdefghijklmnopqrstuvwxyz"};
    char phrase[100];

    int menu = -1;

    while(menu != 3)
    {
        printf("----------------------CAESAR'S CIPHER----------------------\n");
        printf("1- Encrypt a text.-----------------------------------------\n");
        printf("2- Decrypt a text.-----------------------------------------\n");
        printf("3- Exit program.-------------------------------------------\n");

        printf("Choose an action: ");
        scanf("%d", &menu);

        if(menu == 1)
        {
            encrypt(phrase, alphabet);
        }
        else if(menu == 2)
        {
            decrypt(phrase, alphabet);
        }

    }
}

void* encrypt(char *phrase, char *alphabet)
{

    printf("TYPE THE PHRASE YOU'D LIKE TO ENCRYPT TO CAESAR'S CIPHER: ");
    gets(phrase);
    printf("HERE'S THE ENCRYPTED TEXT: ");

    foreach(char *p, phrase) {
        printf("%v", *p);
            *p = tolower(*p);
            foreach(char *a, alphabet) {
                    if (*p == *a) {
                        if(*p != 'X' && *p != 'Y' && *p != 'Z')
                        {
                            a += 3;
                            printf("%c", *a);
                        }
                        else if(*p == 'X')
                        {
                            printf("A");
                        }
                        else if(*p == 'Y')
                        {
                            printf("B");
                        }
                        else if(*p == 'Z')
                        {
                            printf("C");
                        }
                    }
            }
    }

}
  • Moral: [Don't use `gets()` — it is far too dangerous to be used, ever!!!](https://stackoverflow.com/q/1694036/15168) It isn't even a part of Standard C any more. – Jonathan Leffler Mar 14 '20 at 05:16
  • Your compiler should be complaining about type mismatches between `char (*)[100]` and `char *` — you shouldn't post code that doesn't compile cleanly without showing the messages and asking for help in resolving them. – Jonathan Leffler Mar 14 '20 at 05:19
  • My brain is fried — your `foreach` macro with a double loop has me baffled. I think it is ghastly, and confusing. – Jonathan Leffler Mar 14 '20 at 05:21

1 Answers1

2

If you want to read in a line of text and parse it, write code that reads in a line and parses it. You have code that reads in a number and then code that reads in a line. So when you push "1<enter>" the code that reads in a number reads the 1 and the code that reads in a line reads just the enter key and gets an empty line.

    printf("Choose an action: ");
    scanf("%d", &menu); // this consumes the 1 that you push

    if(menu == 1)
    {
        encrypt(&phrase, &alphabet); // the gets here consumes the enter that you push
David Schwartz
  • 179,497
  • 17
  • 214
  • 278