0

I wrote CS50 course's the caesar task's code. Everything is working excellent. But when I run the command check50, in the response there is a mistake. When the input is not-numeric my code returns 0 and terminates the program. But the response is that it returns 1. Here is my code and the response. Please help to solve this problem. Thank you in advance

-The response

:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:) encrypts "barfoo" as "onesbb" using 65 as key
:) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
:) handles lack of argv[1]
:( handles non-numeric key
    expected exit code 1, not 0
:) handles too many arguments

-My code

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

bool only_digits(string st);
char change (char a, int key);

int main(int argc, string argv[])
{
   if (argc == 2)
   {
       if(only_digits(argv[1]))
       {
           string plaintext = get_string("plaintext:  ");
           int key = atoi(argv[1]);
           printf("ciphertext:  ");
           for (int i = 0; i < strlen(plaintext); i++)
           {
                printf("%c", change(plaintext[i], key));
           }
           printf("\n");
       }
       else
       {
           printf("a\n");
           return 0;
       }
   }
   else
   {
       printf("b\n");
        return 1;
   }
}


bool only_digits(string st)
{
    int d = 0, o = 0;
    for (int i = 0; i < strlen(st); i++)
    {
        if (isdigit(st[i]))
        {
            d++;
        }
        else{
            o++;
        }
    }
    if (o>0)
    {
        printf("%i\n", o);
        return 0;
    }
    else{

        return 1;
    }
}

char change (char a, int key)
{
    if(isalpha(a))
    {
       if(islower(a))
        {
            int lettercode = a-97;
            int check = (lettercode + key);
            if(check<26){
                char changed = check + 97;
                return changed;
            }
            else
            {
                char changed = (check % 26) + 97;
                return changed;
            }
        }
        else
        {
            int lettercode = a-65;
            int check = (lettercode + key);
            if(check<26){
                char changed = check + 65;
                return changed;
            }
            else
            {
                char changed = (check % 26) + 65;
                return changed;
            }
        }
    }
    else
    {
        return a;
    }
}
Vahram
  • 11
  • 2
  • 2
    If the argument isn't 'only digits', you have `return 0;` — but should have `return 1;`. Do not return 0 or invoke `exit(0)` or `exit(EXIT_SUCCESS)` when something has gone wrong. – Jonathan Leffler Mar 16 '22 at 12:18
  • If `only_digits` is `true` you implicitely `return 0` when you leave `main`. If it is `false`, you explicitely have `return 0;` in your code. One of them should be `1`. – Gerhardh Mar 16 '22 at 12:19
  • @Gerhardh I wrote the "only digit" function like this: "if strung contains not numeric symbol return 0, if not return 1. In main I wrote "If "only digit" is true (returns 0) continue the program, if it is false return 0. Where is my mistake? – Vahram Mar 16 '22 at 14:09
  • The error message expects you to return 1 in case there is a non-numerical key. If you `only_digits` function returns `false` you still return 0. As soon as you enter the first `if (argc==2)` you will always return 0 no matter if you find an error or not. – Gerhardh Mar 16 '22 at 14:13
  • Dear @Gerhardh true=1, false=0. Am I right? – Vahram Mar 16 '22 at 14:19
  • It depends. In a boolean context (like in a condition), 0 is false, everything else is true. If you assign to some integer, false evaluates to 0, true evaluates to 1. – Gerhardh Mar 16 '22 at 14:20
  • @Gerhardh In my code if in "if" "only digits" returns 0 it means false? – Vahram Mar 16 '22 at 14:25
  • Correct. But as your function returns type `bool` you should not return `1` / `0` but return `true` / `false` anyway. Then you don't need any implicit conversions. And you should include `stdbool.h`. – Gerhardh Mar 16 '22 at 15:16
  • @Gerhardh in that case how I can return true or false? – Vahram Mar 16 '22 at 15:54
  • I don't understand the question. What's wrong with `return true;` and `return false;` ? – Gerhardh Mar 16 '22 at 16:10
  • @Gerhardh Thank you very much. I have already done it – Vahram Mar 16 '22 at 17:19

0 Answers0