【问题标题】:how do i write a code to deny a non-numeric input using i've tried argv[1][i] it dosent seem to work我如何编写代码以使用我尝试过的 argv[1][i] 来拒绝非数字输入,它似乎有效
【发布时间】:2020-07-18 14:41:51
【问题描述】:

嘿,伙计们,我第一次尝试使用 int main(int argc, argv[]) 时不知道我在哪里做错了……我只是不知道我是否正确使用它。这是一些代码...

  1. 代码编译得当,也能正确给出结果

使用 4 作为密钥将“BaRFoo”加密为“FeVJss” 日志 运行./凯撒 4... 发送输入 BarFoo... 检查输出“密文:FeVJss\n”...

*****但是这是我的问题,当我输入 2r 或 4w 或任何非数字键时,它会给我这样的信息:***

:( 处理非数字键 等待程序退出时超时

谁能告诉我如何为这个函数添加另一个循环,以使 nummerica 键打印为 -Usage: ./caesar key 。请 ?非常感谢您的帮助和有用的建议。

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



int main(int argc, string argv[])

{
    // Variable declarations
    bool keySuccessful = false;
    int key = 0;
    int input_length = 0;
    string text = "" ;
    // The number of command line args submitted was incorrect.
    do
    {
    if(argc != 2)
    {
        printf("Usage: ./caesar key .\n");
        return 1;
    }
    else
    {
      // Convert ASCII char to a alphabate char
      // Access individul char in the string plain
      // get the key val and converted to integer
      key = atoi(argv[1]);
      keySuccessful = true;
      printf("%s",argv[1]);
    }
    }
    while(!keySuccessful);

    // get user input
    text = get_string("%s",text);
    printf("ciphertext: ");
    input_length = strlen(text);

        for (int i=0; i<input_length; i++)
        {
        // Checking if is the lowercase a=97 to z=112
        // Print out lower case with ky a=65 to z=90
           if(isupper(text[i]))
            {
                printf("%c", (((text[i] - 65) + key) % 26) + 65);
            }
            else if(islower(text[i]))
            {
                printf("%c", (((text[i] - 97) + key) % 26) + 97);
            }
            else
            {
                printf("%c", text[i]);
            }
        }


    printf("\n");
    return 0;


}


【问题讨论】:

  • 查看strtol 以确保参数是有效数字并获取其值,切勿使用atoi

标签: c cs50 caesar-cipher


【解决方案1】:

如果我正确理解了您的问题,您要确保第一个参数是一个数字,否则停止执行。 您可以轻松地循环参数,并检查来自ctype.hisdigit() 的每个字符的输出,如下所示:

int argumentIsNumber = 1;
for (int i = 0; i < strlen(argv[1]); i++) {
    if (isdigit(argv[1][i]) == 0) {
        argumentIsNumber = 0;
    }
}

if (argumentIsNumber == 0) {
    // Tell the user the introduced key is wrong
}

正如您在此示例中看到的,您可以将标志设置为 1,并在找到非数字时将其更改为 0。然后,当循环结束时,如果所有字符都是数字,则只有标志为 1。

希望对你有帮助!

【讨论】:

  • tyvm 我尝试使用这个 for 循环,但提示我错误消息说: caesar.c:40:12: 错误:关系比较结果未使用 [-Werror,-Wunused-comparison] for (i isdigit(argv[1][i]) !=0 as 还告诉我未确定的声明
  • @echooca 不要忘记#include &lt;ctype.h&gt;
  • tks 很多我看看它再次提交 wiz 一个错误:)
  • @dieortin 如果参数是 656752675635625365263526536536562536253642375689373652645736274 你会认为参数是正确的,但 atoi 不会成功。正确的方法不是那个,你也不要管理负数
【解决方案2】:

如果用户提示类似的想法,你应该编写代码

$ ./caesar 20x

你应该打印

用法:./凯撒键

for (int i = 0 , n = strlen(argv[1]); i < n; i++)
{
    if (isalpha(argv[1][i]))
        {
            printf("Usage: ./caesar key\n");
            return 1; 
        }
}

【讨论】:

  • 您想做一些与上一个答案不同的事情,但您的做法是错误的,例如您认为 12@34 是一个有效数字。阅读isalpha规范
  • @bruno tks 的贡献不确定你是否仍然可以看到我对已关闭问题的回复忘记标记你所以只想在这里发送我的感谢信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
相关资源
最近更新 更多