0

below is my code for PSET2 caesar. I was very confident when I ran it, so I decided to check if it had any errors (and it did!) I am confused as to why this is occurring and I don't know what to change.

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


int main(int argc, string argv[])
{
    string key = argv[1];

    //checks if user enters input correctly
    if (argc != 2)
    {
        printf("Invalid. Enter: ./caesar (insert number here)\n");
        return 1;
    }

    int k = atoi(key) % 26;

    if (k <= 0)

    {
        printf("Invalid. We only accept positive numbers.\n");
        return 1;
    }

    //retrieving input to encrypt
    string input = get_string();

    //for loop to encrypt each char

    for (int i = 0, n = strlen(input); i < n; i++)
    {
        //conversions according to whether a char is lower/uppercase
        int x = 0;
        if (isupper(input[i]))
        {
            x = (((int)input[i] - 65 + k) % 26) + 65;
            printf("%c", (char)x);
        }
        else if (islower(input[i]))
        {
            x = (((int)input[i] - 97 + k) % 26) + 97;
            printf("%c", (char) x);
        }
        else
        {
            printf("%c", input[i]);
        }
    }

    printf("\n");
    return 0;
}

Here are the errors: enter image description here

enter image description here

Help is very much appreciated!

0
2

It's all the same problem. There's no prompt for input. Look at the following code:

//retrieving input to encrypt
string input = get_string();

There's supposed to be something printed by the get_string() function that will serve as the prompt. It's not supposed to just sit there and wait for input. You need to review the pset spec for Caesar for exact requirements as well as get_int().

In prior years, your code would have passed, but the pset was changed to require the prompt. Is it possible that you are working off of a previous year's material? If so, you need to switch to the current year's requirements. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • Oh, my apologies :) Inserting "input" didn't seem to be enough as far as fixing errors... Was I supposed to do something else? Jul 13 '18 at 21:16
  • The instructions are very specific as to what prompt to use. I'll leave it to you to reread them, including the examples of what it should look like.
    – Cliff B
    Jul 13 '18 at 21:19
  • yes I finally fixed the issue! Thank you for telling me where I need to look at. Jul 13 '18 at 21:23

You must log in to answer this question.

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