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

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日
  • 展开全部

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘