0

my code-->

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

int main(int argc, string argv[])           
{
    if(argc!=2)
    {
        printf("Enter the key as well, in the command line !\n");
        return 1;
    }

    int key=atoi(argv[1]);

    printf("plaintext: ");

    string ptext=get_string();
    int len=strlen(ptext);              //needed to pass arguments in for-loop later
    int temp=key;                       //can't change value of key, so assign a temporary variable for performing calculations
    int z=0;
    while(temp>0)
    {
        temp=temp/10;
        z++;                                   
    }
    int array[z];
                                                        // z is now the length of the key
    for(int k=0;k<z;k++)
    {
        array[k]=temp%10;
        temp=temp/10;
    }                                                   // all the individual numbers in key are now stored in an array--123 is now 1,2,3

    for(int i=0,k=z;i<len,k>=0;i++,k--)             //whilst the loop runs over the length of the entire input string, the key is used again and again
                                                    //since the array elements are stored in reverse order of that in which they're to be used, i can't initialise from k=0 rather, from k=z and decreasing
    {
        if(k==0)
        {
            k=z;                                    //so that the loop runs again and again
        }

        if(!isalpha(ptext[i]))
        {
            k++;                                    //do not increase the value of key element if the element of input is NOT alphanumeric
        }

        if(isalpha(ptext[i]))                       //perform the operation for alphanumeric chars only
        {
            if(islower(ptext[i]))
            {
                ptext[i]=ptext[i]-97;
                ptext[i]=((ptext[i]+array[k])%26)+97;       //ciphering lower case letters
            }
            if(isupper(ptext[i]))
            {
                ptext[i]=ptext[i]-65;
                ptext[i]=((ptext[i]+array[k])%26)+65;           //ciphering uppercase letters
            }
        }

    }

    for(int i=0;i<len;i++)
    {
        printf("%c",ptext[i]);
    }

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

the error-->

caesar.c:38:22: error: relational comparison result unused [-Werror,-Wunused-comparison]
    for(int i=0,k=z;i<len,k>=0;i++,k--)             //the marks show that error is related to i<len condition//

i have absolutely no idea why that condition would not be used in the loop- i goes to one and k goes to one, the ascii value changes, i increases and k increases and the cycle repeats !

is my understanding of the error correct at all? i believe it means that the condition has zero significance in the output of the program, i might as well delete that condition..

thanks!

  • also, can someone please tell me how do i get the program to look "proper" like the others do it? the error doesn't show up in the box, the header files don't come up inside the box etc... is there a page for these? – Sakazuki Akainu Mar 10 '17 at 17:23
  • There's a {} button on top of the edit window. Select your code, press it, and it'll add four spaces in front of any line (unless there were four spaces in front of all lines, in which case it will remove them, assuming you want to undo a previous button press). – Blauelf Mar 10 '17 at 17:37
  • thanks! i'll keep that in mind the next time i post any question! – Sakazuki Akainu Mar 10 '17 at 18:07
1

First to the error: i<len,k>=0 uses the comma operator, which evaluates its left side, discards the result, and evaluates to its right side. So i<len,k>=0 is the same as k>=0, the value of i<len is ignored.

You're doing Caesar's cipher, right? This one looks like a strange crossover of caesar and vigenère.

In Caesar's cipher, you shift all characters by the same number, so there's no need for any array, just shift a letter to range 0-25, add the key, apply %26, and shift it back into the letter range.

In Vigenère, you instead have a keyword, composed of various letters, and shift the letters of the plaintext by the alphabet position of the current keyword letter, advancing the keyword index on encrypting a letter.

Oh, and be aware of get_string returning NULL on [edit]a problem when reading from stdin, for example when it finds an EOF (End-of-file, also Ctrl+D)[/edit]. Your call to strlen on the next line would instantly segfault. Not part of the tests, but doesn't hurt to test for.

| improve this answer | |
  • oh my goodness, i am an idiot.. thanks for pointing that out blauelf :D i don't know how i messed that much lol – Sakazuki Akainu Mar 10 '17 at 18:06
  • also, the thought that a null string could be entered never crossed my mind; now that i think of it, it is a very important condition indeed – Sakazuki Akainu Mar 10 '17 at 18:11

You must log in to answer this question.

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