Home > database >  C, How to use exit code properly?
C, How to use exit code properly?

Time:04-05

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.stack.imgur.com/dnVLV.png) How to 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 )& 64;
                        printf("%c",r1);
                    }
                    else if(c[t]<123 && c[t]>96)
                    {
                        r1 =( c[t]-96    ok )& 96;
                        printf("%c",r1);
                    }
                    else
                    {
                        printf("%c",c[t]);

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



    return 0;

}

I try to do well my hw and all green...

CodePudding user response:

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.

  • Related