0

I was trying to code for following program

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:

Given intervals [1,3],[6,9] insert and merge [2,5] would result in [1,5],[6,9].

Example 2:

Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] would result in [1,2],[3,10],[12,16].

This is the relevant part of my program. Here I want to erase the few positions from the vector, but then I am getting the following error:

error: stray '\177' in program

     intervals.erase(intervals.begin() + (p+1), intervals.begin() + (q+1));

vector<Interval> Solution::insert(vector<Interval> &intervals, Interval newInterval) {

    int n = intervals.size();
    int p = -1, q = -1, a, b;

    for(int i=0; i<n; ++i) {

        if(intervals[i].start <= newInterval.start <= intervals[i+1].end)
            p = i;
        else if(intervals[i].end < newInterval.start < intervals[i+1].start)
            a = i;

        if(intervals[i].start <= newInterval.end <= intervals[i+1].end)
            q = i;
        else if(intervals[i].end < newInterval.end < intervals[i+1].start)
            b = i;
    }
    int x, z;

    if(p != -1 && q != -1)
        x = q-p;

    if(x > 0) {
        z = intervals[q].end;
        intervals.erase(intervals.begin() + (p+1), intervals.begin() + (q+1));
        intervals[p].end = z;
    }
    return vector
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Deepak Sharma
  • 67
  • 1
  • 9
  • 1
    You have a bad character used in your program code. – user0042 Aug 06 '17 at 10:19
  • 1
    Possible duplicate of [Strange gcc error: stray '\NNN' in program](https://stackoverflow.com/questions/1621396/strange-gcc-error-stray-nnn-in-program) – user0042 Aug 06 '17 at 10:21
  • i don't know but it works when i delete p+1 and q+1 from the program – Deepak Sharma Aug 06 '17 at 10:24
  • @user0042 Not exactly. There, the problem was files being compiled that shouldn't be compiled. Here, the problem is the file containing stuff it shouldn't. –  Aug 06 '17 at 10:29
  • @hvd I'd suspect some problem with the IDE or editor used. I cannot see how the OP managed to get a DELETE character into the code. – user0042 Aug 06 '17 at 10:31
  • @user0042 Probably. To be clear, I didn't see that the first two comments were both from you. I agree with your first comment. I was disagreeing to the "Possible duplicate" bit. –  Aug 06 '17 at 10:37
  • 3
    Here's a well-kept secret, how experienced programmers solve errors that they don't understand at first: They google the error message. Really. Then they read more than the first result. Until they understand the problem. It's a like a secret superpower. – Ludwig Schulze Aug 06 '17 at 10:41
  • @Ludwig Unfortunately we don't have a better dupe here to be found with google. It shows a way to find out where the culprit sits, but well. – user0042 Aug 06 '17 at 11:01
  • Start of the analysis: 177 octal is 0x7F (127 decimal). That is [Delete](https://en.wikipedia.org/wiki/ASCII#Delete_&_Backspace) in ASCII. In Geany, using [the source for revision 1](https://stackoverflow.com/revisions/595a9d3c-e838-4775-9f51-caee681016bd/view-source), it shows as "7F" when copy-pasted to [Geany](https://en.wikipedia.org/wiki/Geany), right before `(q+1)`. And in Geany it is possible to delete the character it. – Peter Mortensen Aug 03 '21 at 21:47
  • This is ***not*** a duplicate of *[Strange gcc error: stray '\NNN' in program](https://stackoverflow.com/questions/1621396/strange-gcc-error-stray-nnn-in-program)*. That one is qualitatively different (the compiler somehow got to compiling resource forks on Mac (essentially binary files)). – Peter Mortensen Aug 03 '21 at 21:54
  • Related: *[Compilation error: stray ‘\302’ in program, etc](https://stackoverflow.com/questions/19198332)*. It is a canonical, but only for valid UTF-8 sequences (it is, like this one, typically introduced when copying from web sites, PDF files, chat messages (e.g. Skype Chat), etc.). – Peter Mortensen Aug 03 '21 at 22:10
  • I don't think it is in the source file itself. \177 is a signature for [the compiler chewing on a binary](https://stackoverflow.com/questions/48547750/cannot-use-sourcecpp-from-a-file#comment134364000_48547750) (executable) file, usually due to [some problem with the build system/make](https://stackoverflow.com/questions/73410284/). The very first byte of an [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) file is 0x7F (octal 177, decimal 127), followed by the three (in ASCII) characters E, L, and F. – Peter Mortensen May 07 '23 at 00:14

1 Answers1

4

Did you copy that code from a website?

I managed to reproduce your result with this snippet:

const char* msg = "You can't copy this";

When copied and put on coliru here you'll get the same error code.

What I used for the above snippet in HTML code was:

<code>const char* msg = </code>&#127;<code>"You can't copy this";
</code>

Note the &#127; character I put in there.


To fix that, you can use a decent editor like Notepad++ that will make the stray characters visible:

Notepad++ Screenshot

user0042
  • 7,917
  • 3
  • 24
  • 39
  • no i didn't copy this code. this question was on interviewbit and i was solving this and getting this error. tell me how to delete the positions from the struct vector – Deepak Sharma Aug 06 '17 at 12:43
  • @Deepak As mentioned use a editor like Notepad++ to see which characters are bad exactly. – user0042 Aug 06 '17 at 12:45