【问题标题】:A segmentation fault with the use of argv使用 argv 的分段错误
【发布时间】:2021-06-05 04:58:03
【问题描述】:

我不确定我的代码有什么问题。在我尝试使用 argv 命令之前,一切正常,然后当我去执行时,我遇到了分段错误。请告诉我你的想法。

   #include <iostream>
   #include <vector>
   #include <algorithm>
   #include <string>
   using namespace std;
  
  
   int main(int argc, char **argv) {
  
      vector<string> nums;
      vector<string> single {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
      vector<string> second {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
      vector<string> twos {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
 
      string num;
      string s;
 
      s = argv[1];
 
      while (s != "quit") {
          nums.push_back(s);
      }
     int b =s.length();
      int a = stoi(s);
 
          if (a< 10){
                 cout << "Number" << s << "is written as" << single[a] << '\n';
          }
          else if (a < 100){
              int temp10 = a / 10;
              int temp1 = a - temp10*10;
 
              if (temp1 == 0){
                  cout << "Oops! Entered a 0 in the number";
              }else{
                  cout << "Number" << s << "is written as" << single[temp10] << single[temp1] << '\n';
              }
          }
          else if (b == 3){
              int temp100 = a / 100;
              int temp10  = a - temp100*100;
              temp10  = temp10 / 10;
              int temp1   = a - temp100*100 - temp10*10;
                 if (temp10 == 1){
                       cout << "Oops! Entered a 1 in the tens place";
 
                  }else if (temp10 == 0 || temp1 == 0){
                      cout << "Oops! Entered a 0 in the number";
                  }else{
                      cout << "Number" << s << "is written as" << single[temp10] << single[temp1] << '\n';
                  }
          }
          else if (b == 4){
              int temp1000 = a / 1000;
              int temp100 = a - temp1000*1000;
              temp100 = temp100 / 100;
              int temp10  = a - temp1000*1000 - temp100*100;
              temp10  = temp10 / 10;
              int temp1   = a - temp1000*1000 - temp100*100 - temp10*10;
              if (temp10 == 1){
                  cout << "Oops! Entered a 1 in the tens place";
 
              }else if (temp1000 == 0 || temp100 == 0 || temp10 == 0 || temp1 == 0){
                  cout << "Oops! Entered a 0 in the number";
              }else{
                  cout << "Number" << s << "is written as" << temp1000 << "thousand" << temp100 << "hundred" << single[temp10] << si
    ngle[temp1] << '\n';
              }
          }

代码的重点是接受用户的命令行输入并用单词拼出数字。

【问题讨论】:

  • 任何时候您使用argv 而不先检查argc 来确认它是安全的,您就是在自找麻烦。 argv:参数列表。 argc: 列表中的参数数量。
  • 在引用 argv[1] 之前需要检查 argc。如果没有提供引用 argv[1] 的 arg,则将是未定义的,例如 segfault。
  • 我可以想象如果这个条件 ** while (s != "quit") {nums.push_back(s);} ** 没有得到错误,可能是无限循环?
  • @KenWhite 你为什么这么认为?不是 operator== 标准的一部分,它返回 lhs.compare(rhs) == 0?
  • @OP 哪一行代码发生了分段错误? -- 请让我知道你的想法。 -- 你应该这样做。解决问题是一回事,但至少,您应该努力调试代码以找出问题的哪里How to debug small programs

标签: c++ segmentation-fault argv


【解决方案1】:

我认为问题在于潜在的无限循环while (s != "quit"){nums.push_back(s);},如果 s 真的不是 quit 怎么办?无限循环很可能导致分段错误。

警告:在int a = stoi(s); 行,您将拥有terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

【讨论】:

  • 因此,如果我删除 while 语句使其只是 nums.push_back(s),我仍然会收到分段错误。我已经查看了链接,但我理解的不够多,无法解决该问题。
  • 我不知道是什么问题,所以我尝试编译您的代码并在 onlinegdb.com 上运行,但没有问题,如果您认为它有任何问题,能否请您发布您的分段错误消息详情?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
相关资源
最近更新 更多