0

I have this error. Please Help

~/workspace/pset2/ $ make vigenere clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow vigenere.c -lcrypt -lcs50 -lm -o vigenere vigenere.c:38:4: error: expected expression else ^ 1 error generated. make: *** [vigenere] Error 1 

My code below

#include <cs50.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> int main(int argc,string argv[]) { if (argc == 2) { int k[strlen(argv[1])]; for(int a = 0, b = strlen(argv[1]); a < b; a++) { if(islower(argv[1][a])) k[a] = argv[1][a] - (int)"a"; if(isupper(argv[1][a])) k[a] = argv[1][a] - (int)"A"; } printf("plaintext: "); string p = get_string(); printf("ciphertext: "); for (int i = 0, n = strlen(p); i < n; i++) { if (isalpha((char)p[i])) { if(isupper((char)p[i])) printf("%c", (char)(p[i] + k[i % strlen(argv[1])] - 65) % 26 + 65); else if(islower((char)p[i])) printf("%c", (char)(p[i] + k[i % strlen(argv[1])] - 97) % 26 + 97); } else printf("%c",(char)p[i]); } printf("\n"); return 0; }; else return 1; } 
4
  • How have you declared argv? Commented Dec 11, 2017 at 23:11
  • I did the int main(int argc, string argv[]), Is that what you mean? Commented Dec 11, 2017 at 23:34
  • can you edit your question and post the entire code up to that point in the program? (so, the first 16 lines or so). No need for anything after that. Commented Dec 11, 2017 at 23:35
  • I just did. Please reload Commented Dec 11, 2017 at 23:38

1 Answer 1

1

atoi takes a string as argument. You are passing it a single char.

As an example, if you have atoi("123"); you'll get 123. But you can't pass it atoi('1') as that is a char.

If you are trying to get the ascii value of each char in the keyword, you can subtract either 'A' or 'a' from the char, such that if argv[1][a] was 'c', then 'c' - 'a' is 2. I think this is what you are trying to do?


Followup

You have to subtract either 'a' or 'A' depending on the value of argv[1][a] right? You are trying to get the char down to the range of 0-25 and that will only work if you know if it's upper or lowercase. (I see that you are using magic numbers 97 and 65 later on. Do you know why?)

Also, what happens if the plaintext char isn't alpha? You shouldn't be incrementing the key, but you are because you are using i as the increment value. You should create a new variable to keep track of which key value you need to be using.

8
  • can you please reload? Commented Dec 12, 2017 at 0:28
  • edited my answer Commented Dec 12, 2017 at 0:31
  • I updated it again... Commented Dec 12, 2017 at 0:50
  • your code no longer matches your error. why are you trying to access p before you have even declared it? why are you no longer looping over argv[1] Commented Dec 12, 2017 at 0:52
  • I'd suggest you close off this question and work on testing your code. Then, if you get stuck again, open a new question. SE doesn't work well to debug someone's code step by step. Commented Dec 12, 2017 at 0:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.