C++ 使用argv时出现的分段错误

C++ 使用argv时出现的分段错误,c++,segmentation-fault,argv,C++,Segmentation Fault,Argv,我不确定我的代码有什么问题。在我尝试使用argv命令之前,它一直在工作,然后当我执行时,我得到了一个分段错误。请告诉我你的想法 #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int main(int argc, char **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';
              }
          }
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,字符**argv){
向量nums;
向量单{“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”};
向量第二{“十一”、“十二”、“十三”、“十四”、“十五”、“十六”、“十七”、“十八”、“十九”};
向量二{“二十”、“三十”、“四十”、“五十”、“六十”、“七十”、“八十”、“九十”};
字符串数;
字符串s;
s=argv[1];
而(s!=“退出”){
nums.向后推;
}
int b=s.长度();
int a=stoi(s);
如果(a<10){

cout我认为问题在于潜在的无限循环
,而(s!=“quit”){nums.push_(s);}
,如果s真的不是quit?。无限循环更有可能导致分段错误


警告:在
int a=stoi(s);
行中,您可以随时使用
argv
,而无需先检查
argc
以确认您在自找麻烦是安全的。
argv
:参数列表。
argc
:列表中的参数数。在引用argv之前,您需要检查argc[1] 。如果没有提供参数,引用argv[1]将是未定义的,例如segfault。我可以想象,如果这个条件**在(s!=“退出”){nums.push_back(s);}**没有得到false,可能是无限循环?@KenWhite为什么这样认为?操作符==不是标准的一部分,它返回lhs.compare(rhs)吗==0?@OP发生分段错误的代码行是什么?--请告诉我您的想法。--您应该这样做。解决问题是一回事,但至少,您应该努力调试代码以确定问题所在。因此,如果我删除while语句,那么它只是nums。push_(s)我仍然收到一个分段错误。我已经查看了链接,但我不太了解如何解决该问题。我不知道问题出在哪里,因此,我尝试编译您的代码并在onlinegdb.com上运行,但没有问题。如果您认为有任何详细信息,请发布分段错误消息?