0

I'm trying to solve pset2 Cesar encryption.Everything seems fine and encryption is working but when I hit check50 I get errors:

:( encrypts "a" as "b" using 1 as key
    expected prompt for input, found none
:( encrypts "barfoo" as "yxocll" using 23 as key
    expected prompt for input, found none
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected prompt for input, found none
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected prompt for input, found none
:( encrypts "barfoo" as "onesbb" using 65 as key
    expected prompt for input, found none

Here is my code:

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

int main(int argc, string argv[])
{

    int iKey = 0;

    string user_message;

    if (argc < 2 || atoi(argv[0]) < 0 || atoi(argv[1]) < 0)
    {
        printf("enter a single command-line arg: non-negative integer.\n");
        return 1;
    }
    else
    {
        iKey = atoi(argv[1]);
    }

    user_message = get_string();

    // starting of encipher message
    for (int i = 0, j = strlen(user_message); i < j; i++)
    {
        if (islower(user_message[i]) && isalpha(user_message[i]))
        {
            user_message[i] = (user_message[i] - 'a' + iKey) % 26 + 97;
        }

        else if(isupper(user_message[i]) && isalpha(user_message[i]))
        {
            user_message[i] = (user_message[i] - 'A' + iKey) % 26 + 65;
        }

        // print the encrypted text
        printf("%c", user_message[i]);

    }
    printf("\n");

    return 0;

}
0

Didn't the pset specify that you should print out a message asking for input?

2
  • printf("enter a single command-line arg: non-negative integer.\n"); I did it?
    – Xample
    Aug 19 '17 at 7:49
  • No, not really. Its an error statement in response to an incorrect program invocation, followed by a return statement that immediately terminates the program. Also, it doesn't point out what the error was. The error printf should have different, appropriate text and this printf statement should precede the get string statement. Also, remember that when a string type var is created, it's size is set at that point. Because this code doesn't initialize at the same time it is declared, the size will be 0, causing more problems when the getstring is executed. There may be more problems.
    – Cliff B
    Aug 19 '17 at 18:58

You must log in to answer this question.

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