Unconquerable p 2022-01-23 12:00 采纳率: 90.9%
浏览 3495
已结题

C++报错terminate called after throwing an instance of 'std::out_of_range' what(): stoi

报错:

terminate called after throwing an instance of 'std::out_of_range'
  what():  stoi




#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <sstream>
using namespace std;
int change(int a,int d){
        if(a==0){
        return 0;
    }
    int i=0;char s[120];
    while(a!=0){
        int f=a%d;
        if(f<10){
            s[i++]=f+'0';
        }else{
            char v='a'+f-10;
            s[i++]=v;
        }
        a/=d;
    }
    string k="";
    for (int j = i - 1; j >= 0; j--) {
        k=k+to_string(s[j]);
    }
    
    int iu= stoi(k,0,10);
    return iu;
}


int main(){
    srand(time(0)); 
    int hard=2;
    hard=hard%10;
    int p[hard][hard][hard];
    for (int i=0;i<hard;i++){
        for (int j=0;j<hard;j++){
            for (int x=0;x<hard;x++){
                p[i][j][x]=0;
            }
        }
    }
    int a1=rand()%hard,a2=rand()%hard,a3=rand()%hard;
    p[a1][a2][a3]=666;
    int truenl=0;
    for (int i=0;i<500;i++){
        if(change(i,hard)==(hard-1)*111){
            truenl=i;
            break;
        }
    }
    cout<<"input a num:>=0,<="<<truenl<<endl;
    int a789,a78;
    cin>>a789;
    a78=change(a789,hard);
    int a,b,c;
    a=a78%10;
    a78=a78-a;
    a78=a78/10;
    b=a78%10;
    a78=a78-b;
    a78=a78/10;
    c=a78%10;
    try{
        if (a>=hard || b>=hard || c>=hard){
            throw 77;
        }
        
        if (p[a][b][c]==0){
            cout<<"lose"<<endl; 
            cout<<"right num: "<<change(a1*100+a2*10+a3,hard)<<endl;
        }else{
            cout<<p[a][b][c]<<endl;
        }
    }catch(int){
        cout<<"wrong:you should input a number that <= "<<truenl<<"!"<<endl;
    }

    return 0;
} 

```

  • 写回答

1条回答 默认 最新

  • [PE]经典八炮 2022-01-23 12:21
    关注

    首先要明确一点:这不是报错,这是异常。out_of_range是超限异常,你调试看看k的内容,估计超出了范围。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月12日
  • 已采纳回答 5月4日
  • 请采纳用户回复 1月29日
  • 修改了问题 1月23日
  • 展开全部

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改