How to create a function type boolean that checks to see if the command line argument is in fact a digit?

huangapple go评论55阅读模式
英文:

How to create a function type boolean that checks to see if the command line argument is in fact a digit?

问题

I am currently taking cs50 introduction into computer science.

I'm trying to create a type bool function with argument string named only_digits that will check to see if the command line argument that the user inputs is in fact a digit. I don't think I'm using the isdigit correctly. What I am trying to accomplish is not only checking to see if it is a decimal digit but also returning it back to main to continue with the program and furthermore convert the key from a string to an int inside main.

I have tried so many different ways. I refuse to go look at other solutions to the whole program because I really want to understand this and be able to learn and progress. It is the "Caesar" cipher "Problem Set 2" in "Week 2 Arrays" of the "Introduction to computer science course."

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

bool only_digits(string s);

int main(int argc, string argv[])
{
   if(argc != 2)
   {
      printf("USAGE: ./caesar KEY\n");
      return 1;
   }
   return 0;
}

bool only_digits(string s)

if (! isdigit, (argv[1]))
{
   printf("INVALID: USAGE: ./caesar KEY\n");
   return 1;
}
return 0;

Now, I have tried implementing that function multiple different ways. I have tried...

if ((isdigit, s) == 0)...if (!isdigit, argv[1])

but I know that I haven't declared s yet for argv[1] which I am having problems with that also. Every time I try to declare it I get an error. I would list more examples of what I have tried and the errors but to be honest I have tried so many different ways that I can't even remember all of them now.

Also, please tell me if I'm wrong on this assumption. The command line argument is stored as a string so it's for example stored in this fashion: if the user typed "./caesar 3" argv["3"] and not argv[3]. So meaning it is stored as a string and not the decimal number 3. So I'm actually trying to check if it is a string and if it is to be able to change it into an int. I don't know if I'm fully understanding that or not and that could be a huge reason to why I'm not able to implement this function due to not grasping the concept.

I checked all the suggested questions and answers that they wanted me to review before submitting this and to be honest I just skimmed through them because there was solutions to other sections of the program and I didn't want to accidentally come across that and lose the ability to try on my own at first. This specific part of implementing the function is the part I'm stuck on for now.

英文:

I am currently taking cs50 introduction into computer science.

I'm trying to create a type bool function with argument string named only_digits that will check to see if the command line argument that the user inputs is in fact a digit. I don't think I'm using the isdigit correctly. What I am trying to accomplish is not only checking to see if it is a decimal digit but also returning it back to main to continue with the program and furthermore convert the key from a string to an int inside main.

I have tried so many different ways. I refuse to go look at other solutions to the whole program because I really want to understand this and be able to learn and progress. It is the "Caesar" cipher "Problem Set 2" in "Week 2 Arrays" of the "Introduction to computer science course."

#include &lt;cs50.h&gt; 
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;ctype.h&gt;

bool only_digits(string s);

int main(int argc, string argv[])
{
   if(argc != 2)
   {
      printf(&quot;USAGE: ./caesar KEY\n&quot;);
      return 1;
   }
   return 0;
}

bool only_digits(string s)

if (! isdigit, (argv[1]))
{
   printf(&quot;INVAILD: USAGE: ./caesar KEY\n&quot;);
   return 1;
}
return 0;

Now, I have tried implementing that function multiple different ways. I have tried...

if ((isdigit, s) == 0)...if (!isdigit, argv[1])

but I know that I haven't declared s yet for argv[1] which I am having problems with that also. Every time I try to declare it I get an error. I would list more examples of what I have tried and the errors but to be honest I have tried so many different ways that I can't even remember all of them now.

Also, please tell me if I'm wrong on this assumption. The command line argument is stored as a string so it's for example stored in this fashion: if the user typed "./caesar 3" argv["3"] and not argv[3]. So meaning it is stored as a string and not the decimal number 3. So I'm actually trying to check if it is a string and if it is to be able to change it into an int. I don't know if I'm fully understanding that or not and that could be a huge reason to why I'm not able to implement this function due to not grasping the concept.

I checked all the suggested questions and answers that they wanted me to review before submitting this and to be honest I just skimmed through them because there was solutions to other sections of the program and I didn't want to accidentally come across that and lose the ability to try on my own at first. This specific part of implementing the function is the part I'm stuck on for now.

答案1

得分: 0

isdigit() 只检查字符串中的一个字符。

//不需要翻译的部分
#include <stdbool.h> //included for bool
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h> // for exit()

#define string char* // 模拟关键字 string

bool only_digits(string s);

int main(int argc, string argv[])
{
        if(argc != 2)
        {
                printf("USAGE: ./caesar KEY\n");
                return 1;
        }
        only_digits(argv[1]);

        return 0;
}

bool only_digits(string s) {
        int len = strlen(s);
        for (int x=0;x<len; x++)
        {
                if (!isdigit( (unsigned char) s[x] ))
                {
                        printf("无效: 用法: ./caesar KEY\n");
                        exit(1); // 硬退出,或将其更改为 return(1) 以"失败"
                }
        }
        return 0;
}

祝你好运!

英文:

isdigit() only checks for one 'character' (of the string).

//do not have this
//#include &lt;cs50.h&gt;
//
#include &lt;stdbool.h&gt; //included for bool
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;ctype.h&gt;
#include &lt;stdlib.h&gt; // for exit()

#define string char* // emulate string as keyword

bool only_digits(string s);

int main(int argc, string argv[])
{
        if(argc != 2)
        {
                printf(&quot;USAGE: ./caesar KEY\n&quot;);
                return 1;
        }
        only_digits(argv[1]);

        return 0;
}

bool only_digits(string s) {
        int len = strlen(s);
        for (int x=0;x&lt;len; x++)
        {
                if (!isdigit( (unsigned char) s[x] ))
                {
                        printf(&quot;INVAILD: USAGE: ./caesar KEY\n&quot;);
                        exit(1); // hard exit, or change it to return(1) to &#39;fail&#39;
                }
        }
        return 0;
}


Good luck

huangapple
  • 本文由 发表于 2023年2月27日 03:04:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定