Member Avatar for maggie_fairy

im a first year engg student. i need 2 make a report on cryptography and i need the source code in C for caeser cipher. as it so happens im not very good at c programming... please help
can u tell me wats wrong wid this program?

#include <stdio.h>

void encrypt(int shift);

int main(void)
{
        int shift;
        int decOrEnc;

        printf("Amount of shifts: ");
        scanf("%d", &shift);
        if (shift < 0)
        {
                printf("Bad Input.");
                return 0;
        }
        printf("Type 1 to encrypt or 0 to decrypt: ");
        scanf("%d", &decOrEnc);
        if (decOrEnc != 0 && decOrEnc !=1)
        {
                printf("Bad Input.");
                return 0;
        }
        while(getchar() != '\n');
        if (decOrEnc == 1)
                encrypt(shift);
        else
        {
                shift = -1 * shift;
                encrypt(shift);
        }
        return 0;
}

void encrypt(int shift)
{
        char ch;
        printf("Please enter a string: ");
        ch = getchar();
        while(ch != '\n')
        {
                if (ch == ' ')
                        putchar(ch);

                if(shift == 1)
                                putchar(ch + shift);
                else
                                putchar(ch - shift);
                }
                ch = getchar();
        }
        putchar(ch);
}
while(getchar() != '\n'); // ?
    if (decOrEnc == 1)
        encrypt(shift);
    else
    {
        shift = -1 * shift;
        encrypt(shift);
    }
    return 0;
}

While should have a "{" symbol at the end not a ";".

void encrypt(int shift)
{
    char ch;
    printf("Please enter a string: ");
    ch = getchar();
    while(ch != '\n')
    {
        if (ch == ' ') putchar(ch);
        
        if(shift == 1)
            putchar(ch + shift);
        else
            putchar(ch - shift);
        }  // what this symbol does here?
        ch = getchar();
    }
    putchar(ch);
}

To prevent confusion when using if/else always use "{" and "}".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.