0

I was trying to make the vigenere cipher in c for pset2 and i tried to make it according to the instructions in the description for the problem and also in the walkthrough but when i try to make it, the compiler gives me around 20 errors and I don't know what to do. Like in the beginning I have declared all the variables but for some reason the compiler says that the variables I have used in my code are undeclared. And this whole thing has me confused. Please help me out. Also if there is anything that I could do to improve the method that I am currently using to encipher the input given by the user then please let me know.

Thank you, Shayan

Here is my code below:

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

int main (int argc, string argv[]) 

{

 string plaintext = GetString(); 
 string plaintext_length = strlen(plaintext); 
 string key = argv[1];
 int key_length = strlen(key); 
 int shift[key_length]; 

    if (argc != 2) {

        printf("The command is not valid and cannot be used as a key to encrypt your message\n");
        return 1; 
    }



    for (int i = 0; i < key_length; i++) 

    {

        if (!isaplha(key[i])) 

        {
            printf("The command is not valid and cannot be used as a key to encrypt your message\n");
            return 1; 
        }

    } 



    for (int i = 0; i < key_length; i++) 

    {
        if (isupper(k[i])) {

            shift[i] = key[i] - 65; 

        } else {

           shift[i] = key[i] - 97;       
    } 

}   


    for (int j = 0, i = 0; j < plaintext_length; j++) 

    { 

        if (islower(plaintext[j])) 
        {
            plaintext[j] % 122; 
            printf("%c", (plaintext[j] + shif[i]) % 26); 

        }   else if (isupper(plaintext[j])) 

            {
                plaintext[j] % 90;
                printf("%c", (plaintext[j] + shif[i]) % 26);
            }


        }

    }

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

Let me walk you through your errors and what they mean, but keep in mind that you are to learn from this procedure and not just count on others for correcting those errors. To solve compiling errors you start from the top, the first error. Sometimes solving the top error solves following ones too.

1.

new.c:6:21: error: unknown type name 'string'
int main (int argc, string argv[])
                    ^

You get that error because the type string is included in the CS50 library and you haven't #include it. To solve it just add #include <cs50.h> at the top of the file. That removes 6 errors.

2.

new.c:12:9: error: incompatible integer to pointer conversion initializing
      'string' (aka 'char *') with an expression of type 'unsigned int'
      [-Werror,-Wint-conversion]
 string plaintext_length = strlen(plaintext);
        ^                  ~~~~~~~~~~~~~~~~~

You get that error because per strlen()s man page this function returns an int, not a string so the variable you assign the return value must be an int like you do a couple of lines bellow. That solves 2 errors.

3.

clang -ggdb3 -O0 -std=c99 -Wall -Werror    new.c  -lcs50 -lm -o new
new.c:29:14: error: implicit declaration of function 'isaplha' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        if (!isaplha(key[i]))
             ^

Here you have a typo. You use isaplha() while the correct function is isalpha(). That solves another 3 errors.

4.

new.c:41:21: error: use of undeclared identifier 'k'
        if (isupper(k[i])) {
                    ^

That's because there is no array with the name k. I guess you mean key[i]. That solves 1 error.

5.

new.c:60:42: error: use of undeclared identifier 'shif'; did you mean 'shift'?
            printf("%c", (plaintext[j] + shif[i]) % 26);
                                         ^~~~
                                         shift

Another typo. shif should be shift.

6.

new.c:59:26: error: expression result unused [-Werror,-Wunused-value]
            plaintext[j] % 122;
            ~~~~~~~~~~~~ ^ ~~~

You just have an expression here. You don't do something with it, not assigning it or checking something with it. I'll just delete it. That solves one error.

7.

new.c:65:46: error: use of undeclared identifier 'shif'; did you mean 'shift'?
                printf("%c", (plaintext[j] + shif[i]) % 26);
                                             ^~~~
                                             shift

Again the same typo. That solves another error.

8.

new.c:64:30: error: expression result unused [-Werror,-Wunused-value]
                plaintext[j] % 90;
                ~~~~~~~~~~~~ ^ ~~

Another unused expression. You again do nothing with it. Another error gone.

9.

new.c:72:12: error: expected parameter declarator
    printf("\n");
           ^

This one is a little tricky. It's because you have added a stray } on a line before the printf() and then printf() is outside of main(). Delete the curly brace. And that solves all errors! That will make the code compile but I can't guarantee that it will work as you want. I just solved you your compile time errors. The run-time errors (the errors that occur when you run your code, no when you compile it) are your job.

I hope this helps you learn the procedure and you will follow it yourself next time. Mark the answer as correct if it helped you.

1
  • Your advice sure did help me! Thank you for all your help. Really appreciate it. Aug 5 '15 at 23:28
2

First of all, you forgot to include cs50.h. Next, you have a series of misspellings and syntax errors.

This will be a good exercise in debugging for you and will give you a good understanding of how to interpret what the compiler is telling you. You need to start with the first error, figure out exactly what is wrong with it and work through it. Note that often, one error will generate a series of other errors later, so until you have a better understanding, you should always work through them in order.

Note also that each error will tell you the line number of the code that has the error AND the specific part of the line that is causing the problem by both telling you the number of characters from the start, and by printing a pointer on the line below the bad code that points up at the problem. This will narrow things down a lot.

Fix all of the syntax issues and then you can move on to the logic errors.

Also, please spend a little time exploring the help tab on this page. Among other things, it will explain how to post formatted code.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

4
  • Hey! Thanks for the advice really helped me alot. Thanks for all your help. Aug 5 '15 at 23:22
  • Damn. I was writing such a detailed answer, I didn't see you answered in the meantime.
    – ChrisG
    Aug 5 '15 at 23:25
  • That's ok, I've followed you a bunch too. Just wait till you find yourself chasing Kareem and Kiwi! ;-) I thought about going through all the errors, but I didn't want to deprive Mohammed of all the fun! ;-D
    – Cliff B
    Aug 5 '15 at 23:26
  • Hah! I tried to follow Kareem the first days when he was still very active. It didn't take him more than 10 seconds. :P
    – ChrisG
    Aug 5 '15 at 23:27

You must log in to answer this question.

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