0

I am having a problem with removing all instances of "amp;" in a string in c++, I have created a loop to replace "amp;" with "" until there are no "amp;" instances left. I am having an unknown error where the program starts printing random characters, I have never seen it before. There is an image below. I have used mutliple other loops to remove things in the same program, so I am unsure why it is happening now.

Character Glitch: https://screenrec.com/share/EcVLHoasvU

This is the loop I have used:

try {
  while(DownloadLink.find("amp;") != string::npos){
    DownloadLink.replace(DownloadLink.find("amp;"), 4, "");
  }
} catch (const exception& Error) {
  cerr << "Error: " << Error.what();
}

For a minimal reproducible example:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string DownloadLink;
    
    DownloadLink = "https://rr2---sn-gjo-w43l.googlevideo.com/videoplayback?expire=1685464604&amp;ei=vNF1ZPypCYLk8wS04YS4Bw&amp;ip=193.135.13.240&amp;id=o-AOroxIeyjf0SXpp4Jjav3mMxHNrQnAmWFHliIzDxKFK8&amp;itag=22&amp;source=youtube&amp;requiressl=yes&amp;mh=xJ&amp;mm=31%2C29&amp;mn=sn-gjo-w43l%2Csn-ab5sznly&amp;ms=au%2Crdu&amp;mv=m&amp;mvi=2&amp;pl=24&amp;initcwndbps=248750&amp;spc=qEK7B7YCjgMFmSN1lRYNJdgmcwOfjSE&amp;vprv=1&amp;svpuc=1&amp;mime=video%2Fmp4&amp;ratebypass=yes&amp;dur=1535.256&amp;lmt=1685416420579114&amp;mt=1685442532&amp;fvip=1&amp;fexp=24007246%2C24362688%2C51000022&amp;beids=24350017&amp;c=ANDROID&amp;txp=4318224&amp;sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cratebypass%2Cdur%2Clmt&amp;sig=AOq0QJ8wRAIgOFevBFo84_aL0L8hSD1vwztRDGStTF6OaohO4gA8OIUCIE1wN5685dZ7FROQXO9W7BOv4thF8B3KxFE6iefy60xS&amp;lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&amp;lsig=AG3C_xAwRgIhAOO9mtQzsKDrCes2EPmTwOyB8QtGblPFo30I9VrOsSnMAiEAh91ImIfeaugrIfJ4Q0U30viktdo4sWHv0ywQV0kfjt8%3D&amp;title=Ages+1+-+100+Fight+For+$500,000";
    while(DownloadLink.find("amp;") != string::npos){
        DownloadLink.replace(DownloadLink.find("amp;"), 4, "");
    }
    cout << DownloadLink;

    return 0;
}

Has anyone seen anything like this before or knows how to help? Thanks

EthanSteel
  • 331
  • 8
  • This doesn't print any characters unless there's an exception so what exception do you get? What type Is `DownloadLink`? Please show a [mre]. – Ted Lyngmo May 30 '23 at 16:22
  • @TedLyngmo There is no exception caught, it always shows the error with the random characters. The type of DownloadLink is a string, and the current content of it is, "https://rr2---sn-gjo-w43l.googlevideo.com/videoplayback?expire=1685464604&ei=vNF1ZPypCYLk8wS04YS4Bw&ip=193.135.13.240&id=o-AOroxIeyjf0SXpp4Jjav3mMxHNrQnAmWFHliIzDxKFK8&itag=22&source=youtube&requiressl=yes&mh=xJ&mm=31%2C29&mn=sn-gjo-w43l%2Csn-ab5sznly&ms=au%2Crdu&mv=m&mvi=2&pl=24"... – EthanSteel May 30 '23 at 16:29
  • Then, please show a [mre]. – Ted Lyngmo May 30 '23 at 16:31
  • @TedLyngmo I have added it to the question – EthanSteel May 30 '23 at 16:37
  • Unfortunately, the example, while being minimal, does not not reproduce the problem, so it's not a minimal reproducible example. – Ted Lyngmo May 30 '23 at 16:39
  • @TheCoder Your code works for me. There's some crucial difference between the bugged code you actually have and the posted code. – john May 30 '23 at 16:41
  • @TedLyngmo I have updated it to include the actual content now, does this help, sorry for the confusion – EthanSteel May 30 '23 at 16:41
  • It just prints the string with all the `amp`s removed. – Ted Lyngmo May 30 '23 at 16:42
  • For me the error where random characters are printed to the program still happens – EthanSteel May 30 '23 at 16:43
  • The current string in the question doesn't even have `amp` in it so what happens if you just do `std::cout << DownloadLink << '\n';` without the loop? – Ted Lyngmo May 30 '23 at 16:44
  • The latest example does not contain any `&` which presumably is just some kind of cut and paste error. However if I add some in your code still appears to work. I think the issue here is that the code you have posted is perfectly correct, the bug you have is in some other code. Unfortunately this happens all the time. People are convinced a bug is in one place, post that code, but the code is correct and the bug is somewhere else. – john May 30 '23 at 16:47
  • With the new string, the cleaning of `amp` makes the string into exactly what it looked like in your previous edit of the question. How are you running this program? From an IDE? Does it make a difference if you add a newline at the end of the printing line? – Ted Lyngmo May 30 '23 at 16:47
  • OK, yet another update to the data, and yet again the posted code is functioning correctly. – john May 30 '23 at 16:48
  • Btw, are you actually running the [mre] in your question on your side and see this issue or do you just _think_ this code should reproduce the issue? – Ted Lyngmo May 30 '23 at 16:49
  • 2
    If I had to guess I would say that the strange output you are seeing is the content you would get if you followed the link you are trying create. – john May 30 '23 at 16:52
  • The error is fixed, it turns out my compiler was corrupt and I reinstalled it and now everything is working correctly, thanks for the help. – EthanSteel May 30 '23 at 17:05
  • @TheCoder FYI, I would suggest removing the redundant `find()` call and the `try/catch`, you don't need either of them, eg: `string::size_type pos = 0; while ((pos = DownloadLink.find("amp;", pos)) != string::npos) { DownloadLink.replace(pos, 4, ""); }` And then, consider using `erase()` instead of `replace()`, eg: `DownloadLink.erase(pos, 4);` – Remy Lebeau May 30 '23 at 18:54

1 Answers1

1

To begin with, I would suggest you take a look at this question to correct your use of using namespace std; for the reasons listed there. Secondly, you can replace replace() with erase() in C++. Trying to replace something with nothing might be improved by just erasing it. As for the problem, you already stated in the comments that you reinstalled the compiler so that I don't need to address it.

Your code that I modified:

#include <iostream>
#include <string>
int main() {
//You can enter the MrBeast challenge link during initialization
    std::string DownloadLink = "https://rr2---sn-gjo-w43l.googlevideo.com/videoplayback?expire=1685464604&amp;ei=vNF1ZPypCYLk8wS04YS4Bw&amp;ip=193.135.13.240&amp;id=o-AOroxIeyjf0SXpp4Jjav3mMxHNrQnAmWFHliIzDxKFK8&amp;itag=22&amp;source=youtube&amp;requiressl=yes&amp;mh=xJ&amp;mm=31%2C29&amp;mn=sn-gjo-w43l%2Csn-ab5sznly&amp;ms=au%2Crdu&amp;mv=m&amp;mvi=2&amp;pl=24&amp;initcwndbps=248750&amp;spc=qEK7B7YCjgMFmSN1lRYNJdgmcwOfjSE&amp;vprv=1&amp;svpuc=1&amp;mime=video%2Fmp4&amp;ratebypass=yes&amp;dur=1535.256&amp;lmt=1685416420579114&amp;mt=1685442532&amp;fvip=1&amp;fexp=24007246%2C24362688%2C51000022&amp;beids=24350017&amp;c=ANDROID&amp;txp=4318224&amp;sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cratebypass%2Cdur%2Clmt&amp;sig=AOq0QJ8wRAIgOFevBFo84_aL0L8hSD1vwztRDGStTF6OaohO4gA8OIUCIE1wN5685dZ7FROQXO9W7BOv4thF8B3KxFE6iefy60xS&amp;lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&amp;lsig=AG3C_xAwRgIhAOO9mtQzsKDrCes2EPmTwOyB8QtGblPFo30I9VrOsSnMAiEAh91ImIfeaugrIfJ4Q0U30viktdo4sWHv0ywQV0kfjt8%3D&amp;title=Ages+1+-+100+Fight+For+$500,000";
    while (true) {
        size_t temp = DownloadLink.find("amp;");//since the link is pretty long, you only have to find it once
        if (temp != std::string::npos) {
            DownloadLink.erase(temp, 4);//you have the position saved in temp
        }
        else { break; }//if there is no more, break
    }
    std::cout << DownloadLink;
    return 0;
}
Hudson
  • 312
  • 2
  • 18