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]