簡體   English   中英

使用 argv 的分段錯誤

[英]A segmentation fault with the use of 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); 在拋出 'std::invalid_argument' what(): stoi 的實例后,您將終止調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM