Crash idk why

I dont kno why this program crashes. help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstring>
using namespace std;
string rez[7]= {"aaa","aab","aba","abb","aba","baa","bab"};
string inc[7]= {"aab","aba","abb","baa","bab","bba","bbb"};
int n, op, i, poz, r, f;
string c ,str;
int main()
{
    cin>>n>>op;
    cin>>c;
    if(op>0)
    {
        for(i=1; i<=op; i++)
        {
            cin>>poz>>r;
            r--;
            if(c.compare(poz,poz+3,inc[r])==0)
                c.replace(poz,poz+3,rez[r]);
        }
        c.resize(n);
        cout<<c;
    }
    else
    {
        for(i=0; i<=7; i++)
        {
            f=0;
            while(c.find(inc[i],f)!=string::npos)
            {
                f=c.find(inc[i],f);
                cout<<f<<' ';
                c.replace(f,f+3,rez[i]);
            }
        }


    }
    return 0;
}

Input: 4 0 abba
Output should be aaaa or at least smth.
Last edited on
closed account (48T7M4Gy)
Didn't crash for me. Just doesn't do anything except exit gracefully.


4 0 abba
 
Exit code: 0 (normal program termination)
It doesn't crash but goes in an endless loop.
while (c.find (inc[i], f) != string::npos)
closed account (48T7M4Gy)
Too right. Why the shell exits with 0 as above, who knows. Infinite loop protection?
Your loop is running out of bounds. The last element in inc and rez are at index 6 but you let the index i go all the way to 7. This could very well cause the program to crash.
Topic archived. No new replies allowed.