0

EDITED: I have managed to edit the mistakes but I don't understand why it is not returning 1 when argv[1] is not keyed in.

Can someone tell me if I am doing anything wrong with:

if (argc == 2)
{.....
}
else return 1;

Here is my code:

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

int main(int argc, char* argv[])
{
    if (argc == 2)
    {
        int k = atoi (argv[1]);
        string p = GetString();
            for (int i = 0, n = strlen(p); i<n; i++)
            {
                if  (isalpha(p[i]))
                {
                    if (islower(p[i]))
                    {
                       int l = p[i];
                       int lresult = (k + l);
                       if (lresult>122)
                        {
                            int modlresult = (((p[i] - 97) + k) % 26) + 97;
                            printf ("%c", modlresult);
                        }
                        else
                            printf("%c", lresult);
                    } 
                    else if (isupper(p[i]))
                    {
                        int u = p[i];
                        int uresult = (k + u);
                        if (uresult>90)
                        {
                            int moduresult = (((p[i] - 65) + k) % 26) + 65;
                            printf ("%c", moduresult);
                        }
                    else
                    printf("%c", uresult);
                    }
                }
                else
                printf ("%c", p[i]);
            }    
    printf ("\n");
    return 0;
    }
    else 
    return 1;
}

1 Answer 1

0

This int l = 'p[i]'; causes l to always have the same value. Which could be anything. Perhaps you meant int l = p[i]; (no tick marks around p[i]). The computer takes this 'p[i]' literally, it does not see what the value of p[i] is. Similarly, int u = 'p[i]'; is a problem.

Cannot say if that's the only problem in your program. Once that is fixed, you may encounter other issues.

3
  • Hi, thanks alot for the help it cleared my misconception. Could you tell me why my code does not return 1 when argv[1] is not even keyed in? Commented Oct 5, 2016 at 15:52
  • Looks like it should? How are you checking it? If you run caesar without arguments, you won't "see" a 1. You would need to echo $? at the command line immediately after the program exits to see the return value. I don't remember where that's described in the course material. I think you are missing the part in the spec that says should yell at the user. That means print some message (like a usage message) to indicate that the user has made a mistake. Commented Oct 5, 2016 at 16:15
  • "I think you are missing the part in the spec that says should yell at the user. That means print some message (like a usage message) to indicate that the user has made a mistake." Hi, you're right. i passed the check50 after printing a simple error message. Thanks once again! Commented Oct 6, 2016 at 10:15

You must log in to answer this question.

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