vendredi 24 novembre 2017

c++ :'terminate called after throwing an instance of 'std::bad_alloc''

I have tried to compile and run my program, it compilled succesfully, but process didn't return like normally and this message was shown 'terminate called after throwing an instance of 'std::bad_alloc'', also it showed process returned 3 <0x3> if that may be helpful.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void skaitymas (int & sk, string failas, string  tekstas[100]) {
    ifstream f (failas.c_str());
    while( getline(f, tekstas[sk]) ) {
        sk++;
    }
    f.close();
}

void atskyrimas( int eil, string & tekstas, string komentarai[5]) {
    int a, s = 0, ilgis = 0;
    for ( int i = 0; i < tekstas.length(); i++) {
        if (i < tekstas.length() - 1) {
            if ((tekstas[i] == '*')&&(tekstas[i + 1] == '*')) {
                a = i + 2;
                while (( tekstas[a] != '*' )&&(tekstas[a + 1] != '*' )){
                    komentarai[s] += tekstas[a];
                    ilgis++;
                }
                tekstas.erase(i, ilgis + 2);
                s++;
            }
        }
    }
}
int main () {
    int sk = 0;
    string failas = "duomenys.txt", tekstas[100], komentarai[100][5] = {""};
    skaitymas(sk, failas, tekstas);
    for ( int i = 0; i < sk; i++ ) {
        atskyrimas(i, tekstas[i], komentarai[i]);
    }
    ofstream p ("rezultatai.txt");
    for ( int i = 0; i < sk; i++ ) {
        p << i << ": " << tekstas[i] << endl;
    }
    p << endl;
    for ( int i = 0; i < sk; i++ ) {
        for( int a = 0; a < 5; a++ ) {
            if ( komentarai[i][a] != "" ) {
                p << i << ": " << komentarai[i][a] << endl;
            }
        }
    }
    p.close();
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire