Home > Blockchain >  How to pass argv[1] from main into a function?
How to pass argv[1] from main into a function?

Time:07-31

How do I take argv[1] from main so that I can use it in my function called only_digits? You don't have to show it to me on my code. I would just like to see, how argv[1] (that is stored in main) can be used in a function (outside of main).

It is also the first time I've used a bool function. I'm wondering if, in the <cs50.h> library, you can use strings and bools the way I used them here. Am I using them right?

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

bool only_digits(string);

int x;

int main(int argc, string argv[])
{
    string s = argv[1];
    x = only_digits(s);
    if (x == 1)
    {
        printf("True\n");
    }
    else if (x == 0)
    {
        printf("False\n");
    }
}

bool only_digits(string s);
{
    for (i= 0; i < strlen(s); i  )
    {
        if (isdigit(s [i]))
        {
            return 1;
        }
        else if (!isdigit(s [i]))
        {
            return 0;
        }
    }
}

CodePudding user response:

Your code is really close to being correct. There are a couple of things:

  1. remove the semicolon from the definition of only_digits.
bool only_digits(string s);
{

should be:

bool only_digits(string s)
{
  1. make the i in the loop an int.
for( i = 

should be:

for( int i =

  1. fix the logic in the loop so it returns false (aka 0) if any character is not a digit, else if it gets all the way through the loop return true.

bool
only_digits (string s)
{
  for (int i = 0; i < strlen (s); i  )
    {
      if (!isdigit (s[i]))
    {
      return 0;
    }
    }
  return 1;
}

CodePudding user response:

the semicolon(;) is needed to be removed

bool only_digits(string s)
{
    for (i= 0; i < strlen(s); i  )
    {
        if (isdigit(s[i]))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}

and you can simple use else in place of elseif because if s[i] is not digits then doesn't matter what it is we just need to return false or Zero in that condition

CodePudding user response:

How do I take argv[1] from main so that I can use it in my function called only_digits?

In your main, the expression argv[1] designates an object of type string. You can do with it anything that you can do with any other string, including pass it as a function argument. Your only_digits() function expects an argument of type string, so you don't need to do anything special to pass argv[1] to it. This will work fine:

    x = only_digits(argv[1]);

You can also assign argv[1] to a variable of type string, and then pass the value of that variable, as the code you present does. That's needlessly roundabout, but it will work fine, too.

However, if your function is intended to determine whether all the characters in the string that is passed to it are decimal digits, then it is buggy, as other answers describe in more detail. Loop notwithstanding, it decides what value to return based only on the first character in its input string, because either one or the other conditional inside the loop must evaluate to true no matter what that character is, and the function returns either way. It also fails to return anything at all in the event that an empty string is passed to it.

Note also that CS50 makes the unfortunate choice of using string as an alias for type char *. This is perhaps easier to read, and it defers the need to talk about pointers, but it sets students up for confusion later on when they discover that strings seem to behave differently than objects of most other data types.

It is also the first time I've used a bool function. I'm wondering if, in the <cs50.h> library, you can use strings and bools the way I used them here. Am I using them right?

There is nothing inherently wrong with the way you are using string and bool. Probably the compiler would warn you if there were, or even outright reject the code. Your program produces different results than you expect because it contains logic errors.

CodePudding user response:

You ask two questions.

First, there's no need for string s. argv[] is an array of pointers to strings, so you could pass the string of interest like so:

x = only_digits( argv[1] );

BUT, before using it, you must test that the user has supplied a string:

if( argc == 2 )
    x = only_digits( argv[1] );

Second, returning 1 for true or 0 for false, will work, but you should use the tokens that mean boolean results.

return true; // or return false;

Here's a version that moves the function ahead of its invocation. In this way, there's no need for a function prototype.

bool only_digits( string s )
{
    while( isdigit( *s ) )
        s  ;

    // TRUE if reached end of string
    if( *s == '\0' )
        return true;
    return false;
}

void main( int argc, string argv[] )
{
    if( argc == 2 && argv[1][0] != '\0' && only_digits( argv[1] ) )
        printf( "True\n" );
    else
        printf( "False\n" );
}
  • Related