0

This is very frustrating. Have written this code 4 different ways and can not figure it out. First problem :( encrypts "a" as "b" using 1 as key expected "ciphertext: b\n", not "Ciphertext \nb...", from what I have read I think it has to do with manipulating the get_string command.If I could find documentation on the meaning of the error message and how it works maybe I can figure it out for myself. Below is my code and the error messages, would be very grateful for any assistance or point me in the right direction so I can read up on it for myself. Thank you.

// Implement a program that encrypts messages using Caesar's cipher.

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

// Get key from command line argument.

int main(int argc, string argv[])

{
    // Check for 2 arguments only.
    if (argc != 2)
        {
            printf("try again\n");
            return 1;
        }

    // Store key in variable int key.
    int k = atoi(argv[1]);

    // Ensure key is positive
    if (k < 0)
        {
            printf("try again\n");
            return 1;
        }

    // Prompt for text to encrypt
    else
        {
            string code = get_string("Enter a string. \n");
            printf("Ciphertext \n");

    // Next comment
        for (int i = 0, n = strlen(code); i < n; i++)
            {
                if isalpha(code[i])
                {
                    if islower(code[i])
                        printf("%c", (((code[i] + k) -97) % 26) + 97);
                    else
                        printf("%c", (((code[i] + k) -65) % 26) + 65);
                }
                else
                    printf("%c", code[i]);
            }

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

output:

:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
    expected "ciphertext: b\n", not "Ciphertext \nb\..."
:( encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yxo...", not "Ciphertext \nyx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected "ciphertext: EDU...", not "Ciphertext \nED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected "ciphertext: FeV...", not "Ciphertext \nFe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
    expected "ciphertext: one...", not "Ciphertext \non..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    expected "ciphertext: iad...", not "Ciphertext \nia..."
:) handles lack of argv[1]
2

Look closely what check50 expects. It expects your output to start with ciphertext: and not Ciphertext \n (case and punctuation matter).

Relevant parts of the caesar specification:

  • Your program must output plaintext: (without a newline) and then prompt the user for a string of plaintext (using get_string).

  • Your program must output ciphertext: (without a newline) followed by the plaintext’s corresponding ciphertext, with each alphabetical character in the plaintext "rotated" by k positions; non-alphabetical characters should be outputted unchanged.

2
  • Thanks for the help, but I'm still lost. Watched the week 1 lecture over again (thought I was onto something with the array and \0 - ) Also went through all the walk throughs again. The one line that bothers me the most is Your program must output plaintext: (without a newline) and then prompt the user for a string of plaintext (using get_string). I do not understand how to out put plaintext before the get_string command. Like I said totally lost. Any suggestion on where and what to research would be nice. Thanks again. – Charles Mar 2 '18 at 20:10
  • You wrote your own strings where you should have followed the specs. The line string code = get_string("Enter a string. \n"); should use "plaintext: " instead, and printf("Ciphertext \n"); should print "ciphertext: ". And printf("try again\n"); is a bit confusing, better print instructions on how to use the programme correctly. – Blauelf Mar 3 '18 at 23:34
2

You're looking in the wrong place. One of the hidden lessons of the early psets is to write code exactly to the program specification. This is very important in team programming environments where different people are writing different parts of a program that have to fit together later.

Since the problem is the same for all tests, let's look at the output for the first one:

     expected "ciphertext: b\n", not "Ciphertext \nb\..."

It says that it expected ciphertext: b followed by a line feed, not Ciphertext without a colon and then followed by a line feed before the b is printed.

Check50 error messages are quite literal.

1
  • Thanks for your time. Still totally lost. Confused by the exact specification. Have no idea what 'expected "ciphertext: b\n", not "Ciphertext \nb\..." ' means – Charles Mar 2 '18 at 20:20
0

Remember c whilst it can do a lot, it is still very 'dumb'. it will only do what you want. Check you Case of each letter in your output. hint 'C' doesn't equal 'c'.

You must log in to answer this question.

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