Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have 2 errors in the if else loop and i don't konow what? Thanks for help me. I am a beginnner.

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

int main(int argc, char* argv[])
{

if (argc != 2)
{ printf("USAGE: cipher <integer key> <encode | decode>\n");
                printf("Then, just type your text and it will automatically output the en/de crypted text! :)\n");

    return 1;
}
int key = atoi(argv[1]);
string s = GetString();

    if (key > 25)
    {
        key = key%26;

            for (int i = 0, longitud = strlen(s); i < longitud; i++)
            {
                int chr = 's[i]';

                if (!isalpha(s[i]))
                {
                    printf("'%c'", chr);
                 else 
                     if (( s[i] >= 'a' && s[i] <= 'z'))
                    {
                        chr =  (chr -'a' + key) % 26 + 'a';
                     printf("'%c'", chr);
                    else 
                        chr = (chr-'A' + key) % 26 + 'A';
                     printf("'%c'", chr);
                    }
                }
             }
        else
            for (int i = 0, longitud = strlen(s); i < longitud; i++)
            {
                int chr = 's[i]';

                if (!isalpha(s[i]))
                {
                    printf("'%c'", chr);

                else
                    if (( s[i] >= 'a' && s[i] <= 'z'))
                    {
                        chr =  (chr -'a' + key) % 26 + 'a';
                     printf("'%c'", chr);

                    else 
                        chr = (chr-'A' + key) % 26 + 'A';
                     printf("'%c'", chr);
                    }
                }
            }
     }

}

share|improve this question
Test if argv is null AND if his length is good. – cl-r Jan 4 at 10:40
Welcome. Your question is off-topic here, please take a look at the faq. It might fit on Stack Overflow, but note that there is no such thing as an if else loop. – codesparkle Jan 4 at 14:36

closed as off topic by Corbin, Brian Reichle, Glenn Rogers, codesparkle, svick Jan 4 at 14:44

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

While you are allowed to drop {} for single statement blocks, your template for an if else should be:

if (condition) {
   statement;
   ...
} else {
   statement;
   ...
}

At least your if (( s[i] >= 'a' && s[i] <= 'z')) ... violates this pattern.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.