程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 argv 出现分段错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 argv 出现分段错误?

开发过程中遇到使用 argv 出现分段错误的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 argv 出现分段错误的解决方法建议,希望对你解决使用 argv 出现分段错误有所启发或帮助;

我不确定我的代码有什么问题。一切正常,直到我尝试使用 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';
              }
          }

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

解决方法

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

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

大佬总结

以上是大佬教程为你收集整理的使用 argv 出现分段错误全部内容,希望文章能够帮你解决使用 argv 出现分段错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。