Home > Mobile >  Why does my bool function require "return true" to work normally?
Why does my bool function require "return true" to work normally?

Time:03-17

I'm learning CS50x course. And the homework of problem set 2 is Caesar's algorithm.

I made it to work normally. But there's one thing make me confused:

the bool only_digits function - it requires a final return true to work normally. I searched on Google and people said there has to be a default return value, okay, I understand.

But when I switched it from TRUE to FALSE, the program just treated all the command-line arguments as FALSE. Therefore the program couldnt work.

I'm new to algorithm and programming. Please help me understand this part.

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

bool only_digits(string s);
char rotate(char c, int n);

int main(int argc, string argv[])
{
    // Make sure program was run with just one command-line argument
    // Also make sure every character in argv[1] is a digit
    if (argc < 2 || argc >2 || only_digits(argv[1]) == false)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    // Convert argument to int
    int x = atoi(argv[1]);

    // Prompt the user
    string plaintext = get_string("plaintext: ");

    // Encrypt the plaintext
    printf("ciphertext: ");
    for (int i = 0, len = strlen(plaintext); i < len; i  )
    {
        char cipher = rotate(plaintext[i], x);
        printf("%c", cipher);
    }
    printf("\n");
}

    // Function check if only digit
bool only_digits(string s)
{
    for (int i = 0, len = strlen(s); i < len; i  )
    {
        if (isdigit(s[i]))
        {
            while(s[i] == '\0')
            return true;
        }
        else
        {
            return false;
        }
    }
return true; /* This part, I dont understand why */
}

    // Function rotate
char rotate(char c, int n)
{
    if (isalpha(c))
    {
        if (isupper(c))
        {
            c = 'A'   (c - 'A'   n) % 26;
            return c;
        }
        else c = 'a'   ((c - 'a'   n) % 26);
        return c;
    }
    else return c;
}

CodePudding user response:

So I'll talk here about a single function, the one with the return true :

bool only_digits(string s)
{
    for (int i = 0, len = strlen(s); i < len; i  )
    {
        if (isdigit(s[i]))
        {
            while(s[i] == '\0')
            return true;
        }
        else
        {
            return false;
        }
    }
return true; /* This part, I dont understand why */
}

So you ask if (isdigit(s[i]) and then you start a while loop that terminates as soon as s[i] != '\0' which is already true as soon as you enter the if-body.

What you'd like to do is to check if there are any non-digits in your string. Something like

bool only_digits(string s)
{
    for (int i = 0, len = strlen(s); i < len; i  )
    {
        if (!isdigit(s[i]))
           return false;
    }
    return true; /* if we didn't find a non-digit, we're fine */
}
  • Related