最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How do I ignore special characters, in this case <, >, and "", in a string that I need to

programmeradmin9浏览0评论

I am trying to make a program that can take a string from a file, and convert it to 4 predefined integers.

For now, I am attempting the stage after extracting the line from the document, to split it up into the wanted parts.

"" were creating issues, but it was easily solved by std::replace'ing the offending characters. Eventually, the source of the string will be directly from the file, but when inputting it manually, it automatically breaks the terminal:

string:   <bounds minlat="51.4374338" minlon="-0.2090608" maxlat="51.5663602" maxlon="-0.0019455"/><bounds minlat="51.4374338" minlon="-0.2090608" maxlat="51.5663602" maxlon="-0.0019455"/>
    
:bounds

And somehow, with no help, it breaks in a weirder manner:

string:   <bounds minlat="51.4374338" minlon="-0.2090608" maxlat="51.5663602" maxlon="-0.0019455"/><bounds minlat="51.4374338" minlon="-0.2090608" maxlat="51.5663602" maxlon="-0.0019455"/>
    
<bounds

If someone has an indication of how to approach this, I would be grateful. Googling "Input sanatisation" only gives ignoring numbers and special characters, which this input still breaks.

Full experimentation code below, the used input is in the //comment:

#include <iostream>
#include <string> 
#include <algorithm>
using namespace std; 

int main(){
    //  <bounds minlat="51.4374338" minlon="-0.2090608" maxlat="51.5663602" maxlon="-0.0019455"/>
    string line6;
    cout << "string: "; cin >> line6;
    std::replace( line6.begin(), line6.end(), '<', ':');
    std::replace( line6.begin(), line6.end(), '>', ';');
    std::replace( line6.begin(), line6.end(), '"', '_');

    cout << "\n" << line6;

    return 0;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论