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!
{}
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).