0

I am working on caesar's cipher and I am not done yet but below is my work in progress. I had done something similar and it was working all I needed to do is write some code to get it to wrap around the alphabet. But this time when I try to compile the piece of code below (which still needs work) but I at least wanted to see if it worked it is telling me that int k variable is unused. I use it in the variable number and I tried defining that variable within the brackets of the code but it still didn't work. What do I have to change? Thanks!

# include <stdio.h>
# include <cs50.h>
# include <string.h>
# define NUMBER = (j + k) % 26

int main( int argc, string argv[])
{
    int k = atoi(argv[1]);

    printf("Give me a string\n");
    string j = GetString();

    for(int i = 0, n = strlen(j); i<n; i++)
         printf("NUMBER");
}
1

You never really used k in main after you've declared it. This error is actually caused by passing the -Werror option on compilation which is responsible for turning warnings to errors.

To fix that error, you need to use k in some computation (e.g., print it, use it as part of an expression, etc.) or you could probably exclude the option -Werror from the compilation command and ignore the warning, but I don't think there's any point of doing that.

I use it in the variable number

The only place I can see another k in your code is the macro that you defined. This is actually a different k and you can use this k to refer to the value that is passed in this place when using the macro.

The k that was created in main has scope from the point where it was declared to the closing brace of the function. The k that's in the macro has scope only in the macro expression. You cannot use refer to other variables in your program inside the argument list of a macro even if these variables are in scope.

As a side note, you don't really need an assignment operator when defining macros and maybe you should surround the expression with parentheses to avoid confusion with other expressions.

0

Consider a simple function that returns the square of a number

int square(int a)
{
     return a*a;
}

And if you do something like this

int k = square(5);

Now, this statement yields error unused variable k on C99, but works fine on gcc, C++ 4.8.1, and C++ 11.

So while working on C99(which is used in CS50), let that statement be replaced by

int k;
k = square(5);

Also I would suggest you to replace # define NUMBER = (j + k) % 26 by # define NUMBER(j, k) (j + k) % 26, and have a look at how #define works.

Good Luck.

1
  • Downvoter, please specify what's wrong? – sinister Dec 15 '14 at 14:05

You must log in to answer this question.

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