An Dy Andy
An Dy Andy

Reputation: 21

C, How to use exit code properly?

When I try to submit my work, the system told me to use the exit code. When I use return 0 and recheck, the system told me to use return 1... (https://i.sstatic.net/dnVLV.png)

:) caesar.c exists.  
:) caesar.c compiles.  
:( encrypts "a" as "b" using 1 as key
    expected "ciphertext: b\...", not "ciphertext: b"  
:( encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yx...", not "ciphertext: yx..."  
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected "ciphertext: ED...", not "ciphertext: ED..." 
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected "ciphertext: Fe...", not "ciphertext: Fe..."  
:( encrypts "barfoo" as "onesbb" using 65 as key
    expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    expected "ciphertext: ia...", not "ciphertext: is..."  
:( handle lack of argv[1]
    expected exit code 1, not 0  
:( handles non-numeric key
    timed out while waiting for program to exit
:( handles too many arguments
    expected exit code 1, not 0

How can I fix it and what's wrong with my code?

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

int main(int argc, string argv[])
{
    int ok;
    char r1;
    if (argc == 2)
    {
        for (int i = 0, s = strlen(argv[1]); i < s; i++)
        {
            if (!isdigit(argv[1][i]))
            {
                printf("Sorry\n");
                return 0;
            }
            else
            {
                ok = atoi(argv[1]);
                string c = get_string("Enter:");
                printf("ciphertext: ");
                for (int t = 0, a = strlen(c); t < a; t++)
                {
                    if (c[t] < 91 && c[t] > 64)
                    {
                        r1 = (c[t] - 64 + ok) % 26 + 64;
                        printf("%c", r1);
                    }
                    else if (c[t] < 123 && c[t] > 96)
                    {
                        r1 = (c[t] - 96 + ok) % 26 + 96;
                        printf("%c", r1);
                    }
                    else
                    {
                        printf("%c", c[t]);
                    }
                }
                return 0;
            }
        }
    }
    else
    {
        printf("Sorry\n");
    }
    printf("\n");

    return 0;
}

I try to do well with my homework and all green...

Upvotes: 1

Views: 1462

Answers (2)

chqrlie
chqrlie

Reputation: 144740

There are multiple issues in your code:

  • you should return a non zero exit status upon error.

  • if the number given as a command line argument has more than 1 digit, you perform multiple iterations (one for each digit). You should move the encoding loop out of the first for loop.

  • using hard coded ASCII values for upper and lower case letters makes the code less portable and hard to read. You should use character constants 'A', 'Z', etc.

  • r1 = (c[t] - 64 + ok) % 26 + 64; is incorrect and may produce @ instead of Z for some inputs. You should use r1 = (c[t] - 65 + ok) % 26 + 65; or better r1 = (c[t] - 'A' + ok) % 26 + 'A';

  • same mistake for r1 = (c[t] - 96 + ok) % 26 + 96;

Here is a modified version:

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

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "missing argument\n");
        return 1;
    }
    char *arg = argv[1];
    char *p;
    int shift = (int)strtol(arg, &p, 10);
    if (!(arg[0] >= '0' && arg[0] <= '9') || p == arg || *p != '\0') {
        fprintf(stderr, "invalid shift argument: %s\n", arg);
        return 1;
    }
    char *s = get_string("Enter string: ");
    printf("ciphertext: ");
    for (int t = 0; s[t] != '\0'; t++) {
        unsigned char c = s[t];
        /* assuming ASCII: upper and lowercase letters are contiguous */
        if (c >= 'A' && c <= 'Z') {
            c = (c - 'A' + shift) % 26 + 'A';
        } else
        if (c >= 'a' && c <= 'z') {
            c = (c - 'a' + shift) % 26 + 'a';
        }
        putchar(c);
    }
    putchar('\n');
    return 0;
}

Upvotes: 2

Yunnosch
Yunnosch

Reputation: 26703

You use the exit code by adding a return <value>, at the appropriate code line.
With <value> being what matches your interface definition, in case of your online judge it seems to be a 1.
In your code you at least fail to do so here:

else
    {
        printf("Sorry\n");
    }

which should be

else
    {
        printf("Sorry\n");
        return 1;
    }

An alternative is the more explicit https://en.cppreference.com/w/c/program/exit for situations in which the path to the end of the program is not as obvious.

(This is mostly what the comment by Lundin mentions. I turned it into an explicit answer.)

However, to completely satisfy the judge you need to work on your output.
With the info given in the question, a solution for those problems is not possible.

Upvotes: 0

Related Questions