vendredi 15 novembre 2019

Why is this string considered an invalid argument when passing into stoi

I have a file called getBurnRata.java that prompts the user to input a burnrate, when the burnrate is less than 0 the program ends. This file reads from that file to grab the burn rate.

string inputLine;
    if(myInputFile)
            { 
                //cout << "#opened file." <<endl ; 
                getline(cin, inputLine);
                cout << inputLine << endl ;
                do{
                        getline(cin, inputLine);
                        //getline(cin, inputLine);
                        //cout << "#input line: "<<  inputLine << endl ; 
                        //cout << "#length: " << inputLine.length() << endl ;
                        if((inputLine.empty() == false ) && (inputLine.length() > 0)){
                            // cout << "#inside notempty" <<endl ;
                            // cout << "#substr 0: " << inputLine.at(0) << endl ; 
                            // cout << "#substr 1: " << inputLine.at(1) << endl ;
                            //cout << "#substr 2: " << inputLine.at(2) << endl ;
                            if(inputLine.at(0) == '#'){
                                //cout << "#input line: "<<  inputLine << endl ;
                                cout << "# line: " << inputLine << endl ;
                            }
                            else if(inputLine.at(0) == '%'){
                                cout << "#this is %%%!!!" << endl ; 
                                cout << "#substr 0: " << inputLine.at(0) << endl ; 
                                cout << "#substr 1: " << inputLine.at(1) << endl ;
                                //cout << "#substr 2: " << inputLine.at(2) << endl ;
                                try{
                                    cout << "#inside try" << endl ; 
                                    cout <<"#inputLine: " << typeid(inputLine).name() << endl ; 

                                    int burnRate = stoi(inputLine);
                                    cout << "#burnRate: " << burnRate << endl ; 
                                }
                                catch(string error){

                                }
                            }
.......................

the console:

inputLine: NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi

i was under the impression

NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE is a string.
`

Aucun commentaire:

Enregistrer un commentaire