【问题标题】:Why is my loop stopping at the first argument (char of argv) in C?为什么我的循环在 C 中的第一个参数(argv 的字符)处停止?
【发布时间】:2021-08-19 04:55:26
【问题描述】:

我不明白为什么我的代码在命令行参数的第一个字符处停止。 我认为问题出在循环中,我需要它来检查命令行参数是否为数字,因此第一部分和第二部分并不重要。 谢谢你,对不起,我是新手。

int main(int argc, string argv[])
{
    // checking if there is more than one command-line argument
 
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    } 
    else 
    {
       // checking if command-line argument is a number
      for (int i = 0, n = strlen(argv[1]); i < n; i++)
      {
          printf("%c\n", argv[1][i]);
          if (isdigit(argv[1][i])) 
          {
              return 0;
          }
          else 
          {
              printf("Usage: ./caesar key\n");
              return 1;
          }
          
      }
      
      
    }
        

    
} 

【问题讨论】:

  • 你从main返回,程序结束。确定不是你的意思?
  • 不,不是,但是当我打印argv[1][i] 时,它只打印第一个字符。
  • 那是因为你returnif/else 的所有情况下。这意味着程序每次都会在第一个循环/字符处终止。如果您不希望它终止,则至少将return 语句之一更改为其他内容。您究竟希望isdigit 真实案例做什么?
  • 记住argc == 2 表示程序有1 参数。 argv[0] 始终是正在运行的可执行文件的名称...

标签: arrays c loops cs50


【解决方案1】:

有效 的情况下,您正在执行 return,因此您检查 argv[1]first 字符.

您希望在有效情况下终止循环,累积值。

这是一些重构的代码:

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

// so we don't have to include cs50.h ...
typedef char *string;

int
main(int argc, string argv[])
{

    // checking if there is more than one command-line argument
    if (argc != 2) {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    // checking if command-line argument is a number
    int keyval = 0;
    for (char *cp = argv[1];  *cp != 0;  ++cp) {
        if (! isdigit(*cp)) {
            printf("Usage: ./caesar key\n");
            return 1;
        }

        // accumulate the number as we go along, digit-by-digit
        keyval *= 10;
        keyval += (*cp - '0');
    }

    printf("keyval=%d\n",keyval);

    return 0;
}

这是另一种方式:

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

// so we don't have to include cs50.h ...
typedef char *string;

int
main(int argc, string argv[])
{

    // checking if there is more than one command-line argument
    if (argc != 2) {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    // checking if command-line argument is a number
    for (char *cp = argv[1];  *cp != 0;  ++cp) {
        if (! isdigit(*cp)) {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }

    // we can use a standard function to get the value
    int keyval = atoi(argv[1]);
    printf("keyval=%d\n",keyval);

    return 0;
}

这是另一种方式:

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

// so we don't have to include cs50.h ...
typedef char *string;

int
main(int argc, string argv[])
{

    // checking if there is more than one command-line argument
    if (argc != 2) {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    // checking if command-line argument is a number
    char *cp;
    int keyval = strtol(argv[1],&cp,10);

    // invalid digit found
    if (*cp != 0) {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    printf("keyval=%d\n",keyval);

    return 0;
}

请注意,strtol 将接受 值(例如 -37)作为有效值。因此,如果不能接受负值,我们需要额外检查 [就在 valid printf] 之前:

// negative value found
if (keyval < 0) {
    printf("Usage: ./caesar key\n");
    return 1;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2019-01-10
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多