0

Please help me, I have gotten this far but when I run the code it does not work as anticipated. I don't understand why my arc !=2 is not working. also when i do ./ caesar 3 and type "hello" it prints only hh on the next line.

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


int main(int argc, string argv[])
{
    if (argc != 2)
{
    return 1;
    printf("try again\n");
}
else
{
    string s = GetString();
    int k = atoi("argv[1]");
    for (int i = 0, n = strlen(s); i < n; i++)
    {
        if (isalpha(s[i]))
        {
            if (isupper(s[i])){
                int c = s[i] - 65;
                int letter = (c - k) % 26;
                int letterr = letter + 65;
                printf("%c", letterr);

            }
            else if (isspace(s[i]))
            {
                printf(" ");
            }
            {
                int c = s[i] - 97;
                int lletter = (c - k) % 26;
                int lletterr = lletter + 97;
                printf("%c", lletterr);
            }

        }
        else
        {
            return 1;
        }
       printf("%c\n", s[i]);
       return 0; 

    }       



}

}

3
  if (argc != 2)
{
    return 1;
    printf("try again\n");
}
else
{....

why else, it is an unnecessary statement, if (argc != 2), exit the program, but simply the next line of code is executed, the if-else statement is not appropriate in this case, addition it should be

if (argc != 2)
    {
        printf("try again\n");
        return 1;
    }

second thing

int k = atoi("argv[1]");

is incorrect, the correct way is

 int k = atoi(argv[1]);

third thing, the most serious, the logical flow of your program is incorrect, just to mention a couple of things. return 1 and return 0, are inside the for loop, so that the first iteration are leaving the program, ultimately I think you still have much work ahead

1
  • Thank you, appreciate it. I agree I have much work ahead, this feels very overwhelming and difficult for me, but hopefully I can get there. Jul 23 '16 at 23:24

You must log in to answer this question.

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