0

I'm trying to get input with spaces in my code and I figured out using get line() so when I'm trying to use it inside loop but when It enters the loop it throws an exception terminating with uncaught exception of type std::out_of_range: basic_string Aborted

So how to fix it?

My Code:

#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
    int N, Q;
    map<pair<string, string>,string> DATA;
    cin>>N;
    while(N--){
    string Code;
    getline(cin,Code);
    for(int i=0;i<Code.length();i++){
        if(Code[i]==' '){
            Code.erase(Code.begin()+i);
        }
    }
    string Tag,Data,Type;
    Tag=Code.substr(1,4);
    int Start=Code.find('"');
    for(int i=Start+1;i<Code.length();i++){
        Data.push_back(Code[i]);
        if(Code[i]=='>'){
            Data.pop_back();
            Data.pop_back();
            break;
        }
    }
    for(int i=5;i<Code.length();i++){
        Type.push_back(Code[i]);
        if(Code[i]=='='){
            Type.pop_back();
            break;
        }
    }
    DATA.emplace(make_pair(Type,Tag),Data);
    }
    string Print;
    cin>>Print;
    string tag_n,type_n;
    tag_n=Print.substr(0,4);
    type_n=Print.substr(5,Print.length());
    pair<string,string>Val={type_n,tag_n};
    if(DATA.find(Val)!=DATA.end()){
        cout<<DATA[Val];
    }
    else{
        cout<<"Not Found!";
    }
}

PS:

Thanks for reading

Sorry for bad English

WhozCraig
  • 59,130
  • 9
  • 69
  • 128
  • Where does the exception occurr (which loop)? – IWonderWhatThisAPIDoes Feb 17 '21 at 07:13
  • Can you give some sample input that causes the exception? – john Feb 17 '21 at 07:14
  • @IWonderWhatThisAPIDoes in the first while loop (N--) –  Feb 17 '21 at 07:16
  • 2
    I see you are attempting to remove spaces from the string after reading it. Does your code following this step have an assumption that the string has no spaces? Because that bit where you remove spaces doesn't work for consecutive spaces. – paddy Feb 17 '21 at 07:16
  • @john –  Feb 17 '21 at 07:16
  • @john actually I tried to debug it as soon as it e tera the loop it exits –  Feb 17 '21 at 07:17
  • Yea @paddy Actually the reason behind removing the space is that as you can see I have made 3 sub string from the string and I know that the input would be similar actually I'm trying to solve a question from hacker rank the input would be x=TagNo name could be anything –  Feb 17 '21 at 07:20
  • There are a few problems with the code. But the main one is that you first call to getline is reading the trailing newline after the number of test cases, not reading a fresh line. Add the following after `cin>>N;` add `cin.ignore(std::numeric_limits::max(), '\n');` see duplicate for full details. – john Feb 17 '21 at 07:24
  • Your code is full of assumptions, such as the input succeeds, there are no consecutive whitespace characters, `find('"')` will succeed, and that `Data` contains at least two items when you encounter `'>'`. On that last point, the standard says that behavior is undefined if you call `pop_back()` on an empty string. On previous points, if `find()` returns `std::string::npos` then the character was not found. You should not then go and add 1 to that value and start using it. – paddy Feb 17 '21 at 07:24
  • Since you've tagged a whole bunch of modern C++ standards, then why aren't you just using `std::regex`? It would be perfect for this, without all the horrendous stuff you have right now. – paddy Feb 17 '21 at 07:26
  • 1
    This is the duplicate, https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction. Can't vote for it right now – john Feb 17 '21 at 07:27
  • When `Code` is empty `Tag=Code.substr(1,4)` will throw that exception – Alan Birtles Feb 17 '21 at 07:31

0 Answers0