-3

I have been working on this Caesar cipher code and this is what I have gotten so far and when the program runs nothing happens.

#include <stdio.h>
#define MAX 80

 main()
{
    int shift, num; 
    char ciph[MAX];
    printf("Enter the message to be encrypted: ");
    while ((ciph[num] = getchar()) != '\n'){  //loops until a blank space is read
        for(num=0; num<MAX; num++){ //scans characters into the array
        ciph[num] = getchar();
        }
    }
    printf("\nEnter the shift amout: ");
    scanf("%d", &shift); //reads the shift amount
    for (num = 0; num < MAX; num++) //shifts the letter
    {
        int c = ciph[num];
        if ('a' >= c && c <= 'z')
            c = ((c - 'a') + shift) % 26 + 'a';
        else if ('A' >= c && c <= 'Z')
            c = ((c - 'A') + shift) % 26 + 'A';
        else
        {

    }
    ciph[num] = c;
}
printf("%c", ciph[num]); //prints the encryption
}
MrTux
  • 32,350
  • 30
  • 109
  • 146
Isaiah
  • 1
  • 1
  • What is your input? 80 keystrokes? Suspect code should detect `'\n'` in the first loop. – chux - Reinstate Monica Mar 31 '15 at 00:10
  • how should i incorporate that? – Isaiah Mar 31 '15 at 00:17
  • Curious, I requested what input OP used (to help better understand the goal) and instead of a sample input, OP replied with a request for more help. – chux - Reinstate Monica Mar 31 '15 at 01:42
  • my bad, i dont know what op is but im gonna assume you are referring to me. but anyways 80 is the max amount of characters that will be stored into the array, the input is letters. – Isaiah Mar 31 '15 at 01:50
  • `char ciph[10]` is a string, for example "test". `chip[0]` is one character in that string, in this case `t`. Use gets or scanf to get the input `chip`. User `printf("%s\n", chip)` to print the string. `printf("%c", chip[num])` will only print one character. Also `num` must be initialized to zero. – Barmak Shemirani Mar 31 '15 at 03:23
  • How will anyone learn from this incident if you delete the question content after you discover your error? – mlp Mar 31 '15 at 05:47

2 Answers2

0

I'm not sure what your intent is but

'a' <= ciph

needs to be

'a' <= ciph[SOMETHING]

and

ciph = (ciph - 'a')+shift) % 26 + 'a';

is missing a '('

James
  • 6,978
  • 2
  • 17
  • 18
0

I think you are supposed to leave the blank space alone with this thing, but I guess that's up to you.

//read the whole line

int i;
int len = strlen(line);
for (i = 0; i < len; i++)
{
    int c = line[i];
    if ('a' >= c && c <= 'z')
        c = ((c - 'a') + shift) % 26 + 'a';
    else if ('A' >= c && c <= 'Z')
        c = ((c - 'A') + shift) % 26 + 'A';
    else
    {
        //leave it
    }
    line[i] = c;
}

//write the whole line
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • why did you make an int with the value of ciph couldnt i have left it? – Isaiah Mar 31 '15 at 00:27
  • @Isaiah, I meant you can work on the whole string, instead of asking user to input one character at a time. I changed the code again, I renamed it to `line` that's the input string – Barmak Shemirani Mar 31 '15 at 00:30
  • oh i see, but 1 input at a time was never the goal, I'm just a newb to this i really meant to read and entire string of characters in and one by one shift them – Isaiah Mar 31 '15 at 00:36
  • Note: Say the length of the string was 50. `for (i = 0; i < strlen(line); i++)` calculates the string length 50 times when 1 would do. Suggest calculate the length outside the loop. A simple solution is `for (i = strlen(line); i-- > 0; )` – chux - Reinstate Monica Mar 31 '15 at 01:39
  • Im not calculating, what i actually want to do is to shift the letters as the int i increments until i reaches n - 1 (the max 79) or there are no more characters to shift – Isaiah Mar 31 '15 at 01:59
  • @chux, noted, that's a bad habit, I shouldn't pass it on. Isaiah, I think chux was talking about my code (which I changed now). You should use gets(ciph); to get input. – Barmak Shemirani Mar 31 '15 at 02:01
  • ` while (ciph[num] !='\n' ){ //loops until next line is read for(num=0; num – Isaiah Mar 31 '15 at 02:09