0

Okay this is driving me nuts.. i have looked at a bunch of these questions which are the exact same.... but none of the answers help.... Basically in my code this is how it goes:

`

if(argc != 2 || k <= 0){
    printf("YOU SCREWED UP!\n");
    return 1;
}
else{

    // everything that happens.. it works so i can't give away the answer but what happens is not my problem...
}

`

So i have tried putting everything outside of the else block as well as tried not to print anything... the first if block.. i don't see whats wrong yet cs50 IDE keeps nagging me about this. This is how the program runs:

 make caesar
 clang -ggdb3 -O0 -std=c99 -Wall -Werror    caesar.c  -lcs50 -lm -o caesar
 jharvard@appliance (~/Dropbox/workspace/pset2): ./caesar
 Segmentation fault (core dumped)
 jharvard@appliance (~/Dropbox/workspace/pset2): ./caesar -2
 YOU SCREWED UP!
 jharvard@appliance (~/Dropbox/workspace/pset2): ./caesar adawdw daf4
 YOU SCREWED UP!
 jharvard@appliance (~/Dropbox/workspace/pset2): ./caesar 5 foo
 YOU SCREWED UP!

Also... I know there are numerous other questions regarding the same thing.. but i have tried them ALL.. with no luck.. hope u can help

1

First just return 1 without printing anything. It shouldn't be like that but check50 expects you to just return 1. What is the k variable?

| improve this answer | |
0

I figured it out.. but i really looked it up on google and found the answer.. Any variables you declare MUST be declared AFTER that if statement... It makes no sense but it works.. Here is the full page if you want: https://www.reddit.com/r/cs50/comments/1viwah/pset2_caesar_handles_lack_of_argv1/

| improve this answer | |
  • You can declare vars before the if statement, as long as they don't make use of a possibly nonexistent argv[x]. I'm guessing that k is dependent on argv[1], so it would fail if you assign the value of a nonexistent argv[1] to it. Similarly, if it is declared and initializes with a value of 0, that if statement would always evaluate as true, even if the argv[1] doesn't exist. In any case, since you've answered your own question, please accept your answer to remove it from the unanswered question pool. thx. – Cliff B Jun 25 '16 at 8:24

You must log in to answer this question.

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