string.erase does not work?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*

*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Tekstas.h"     // Itraukiamas klases Tekstas failas
using namespace std;
//-------------------------------------------------------------------------
const char CFd[] = "Duomenys.txt";     // Duomenu failo pavadinimas
const char CRf[] = "Rezultatai.txt";   // Rezultatu failo pavadinimas
const int CMax = 10;
Tekstas T[CMax];
//-------------------------------------------------------------------------
void Skaityti(string dfv);
void NagrinetiZodi(string zodis, Tekstas & T);
void NagrinetiEilute(Tekstas & T);
void Redagavimas(string rfv);
//-------------------------------------------------------------------------
int main()
{
	Skaityti(CFd);
	Redagavimas(CRf);
	return 0;
}
//-------------------------------------------------------------------------
// Funkcija nuskaito pradinius duomenis ir sudeda juos i string tipo masyva
// fdv - duomenu failo vardas
void Skaityti(string dfv)
{
	ifstream fd(dfv.c_str());
	string eil;
	int i = 0;
	int sk = 0;
	while (!fd.eof() && sk <= CMax)
	{
		getline(fd, eil);   // nuskaitoma eilute
		sk++;
		T[i].eilute = eil;     // ta eilute idedama i string tipo masyva
		T[i].Uzpildyta = true;
		stringstream ss(eil);
		string zodis;
		int j = 0;
		while (ss.good())      // skaitoma eilute
		{
			ss >> zodis;       // nuskaitomas kiekvienas eilutes zodis
			T[i].zodziai[j] = zodis;  // zodis patalpinamas i zodziu masyva
			j++;
		}
		i++;
	}
	fd.close();
}
//-------------------------------------------------------------------------
// Funkcija nagrineja zodi, 
// zodis - funkcijai perduodamas nagrinejamas zodis
void NagrinetiZodi(string zodis, Tekstas & T)
{
	int ilg = zodis.length();
	int sk = ilg%2;
	if (sk != 0)
		zodis.erase();
	
	/*
	if (sk == 0)
		return true;
	else
		return false;
		*/


}
//-------------------------------------------------------------------------
// Funkcija nagrineja eilute
// Tekstas & T - funkcijai perduodamas objektas
void NagrinetiEilute(Tekstas & T)
{
	int i = 0;
	while (T.zodziai[i].length() !=0)
	{
		NagrinetiZodi(T.zodziai[i], T);
//		if (NagrinetiZodi(T.zodziai[i], T) == false)
//		{
			T.zodziai[i].erase();
//			T.Nelyginis = true;
//		}
		i++;
	}
}
//-------------------------------------------------------------------------
// Funkcija spausdina teksta ir ji redaguoja
// string rfv - funkcijai perduodamas rezultatu failo vardas
void Redagavimas(string rfv)
{
	ofstream fr (rfv.c_str());
	int i = 0;
	while (T[i].Uzpildyta)
	{
		NagrinetiEilute(T[i]);
//		if (T[i].Nelyginis = true)
	//		T[i].eilute.erase();
		fr << T[i].eilute << endl;
		i++;
	}
	fr.close();
}
//------------------------------------------------------------------------- 




everything is working, but .erase doesnt erase a thing (tried .clean too)

my problem here is that i have text and i have to find ODD words and erase them from text ( plus i HAVE TO use class with .h and .cpp files)

as you can see i tried a lot different approaches but neither works...
Last edited on
In NagrinetiZodi you pass the string zodis by value, so any change you make to zodis is only made to the local copy of it -- the original you fed to the function will be unchanged.

In NagrinetiEilute it would appear you're going to be clobbering memory you don't own since you don't limit the value of 'i' in any way. Assuming that the last string in T.zodziai is an empty string (and therefore you aren't trampling memory) looks like you're erasing every string in the array.
Last edited on
yes in nagrinetiEilute it erases every string, but how can i erase from text exacly those words i found in NagrinetiZodi?
Pass the string by reference to NagrinetiZodi.

void NagrinetiZodi(string & zodis, Tekstas & T)

Of course, if you keep erasing every string in the calling function, that won't do much good. There also doesn't seem to be a reason to have the second argument to NagrinetiZodi. Perhaps that's just because you simplified the code before posting?
as i said i tried a lot of different approaches so that was just whats left after those :?

would you be so kind telling me how to do it? becouse i have to give in this project today, been trying to solve this for hours , slept like for 3 hours, head doesnt really work, dont understand how to do it...
Last edited on
> i have text and i have to find ODD words and erase them from text

Assuming that words are separated by white space, and 'ODD words' means words in odd positions,

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
#include <iostream>
#include <string>

std::string& erase_odd_words( std::string& text )
{
    std::istringstream stm(text) ;
    std::string result ;
    std::string word ;
    for( int i = 0 ; stm >> word ; ++i )
        if( i%2 == 0 ) // if the word is not ODD
        {
            // append it to the result string
            if( !result.empty() ) result += ' ' ;
            result += word ;
        }

    return text = result ;
}

int main()
{
    std::string text = "zero one two three four five six" ;
    erase_odd_words(text) ;
    std::cout << text << '\n' ;
}

i have to find ODD words and erase them from text ( plus i HAVE TO use class with .h and .cpp files)
if without class and lot of functions it would be easy task, the point is , my lecturer wants fucking class
Oh, c'mon! If you know how to write the functions, just plug them into a class.

1
2
3
4
5
6
7
8
9
10
//////////  my_class.h ////////////////

// include guard

#include <string>

struct my_class
{
     static std::string& erase_odd_words( std::string& text ) ;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//////////  my_class.cpp ////////////////

#include "my_class.h"

std::string& my_class::erase_odd_words( std::string& text )
{
    std::istringstream stm(text) ;
    std::string result ;
    std::string word ;
    for( int i = 0 ; stm >> word ; ++i )
        if( i%2 == 0 ) // EVEN
        {
            if( !result.empty() ) result += ' ' ;
            result += word ;
        }

    return text = result ;
}


1
2
3
4
5
6
7
8
9
10
11
/////////////////  main.cpp ///////////////////

#include <iostream>
#include "my_class.h"

int main()
{
    std::string text = "zero one two three four five six" ;
    my_class::erase_odd_words(text) ;
    std::cout << text << '\n' ;
}

Last edited on
sorry, but it seems you are not very good at writing at plain text box, both programs give 10 errors, and them arent actually real answer to my problem, btw for record, why you dont use "using namespace std;" becouse it saves lot of writing and time?
Is there any possible way to fix my program to make it erase that odd word from text in array(class) ???

p.s sorry im being a dick, but im really pissed off right now and tired...
> but it seems you are not very good at writing at plain text box

True.


> both programs give 10 errors

Missed #include <sstream> Don't see any other obvious errors.


> Is there any possible way to fix my program

There must be; but I can't - I don't understand any part of your program.

My replies was based purely on the text in English (which I can understand).
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
The text file contains text.
Words from the line to the next line trainers.
Words rows assigned to at least one space.
Spacing can be a string at the beginning and at the end, it may be an empty string.
Remove the words consisting of odd number of characters
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Tekstas.h"     // Itraukiamas klases Tekstas failas added txt file 
using namespace std;
//-------------------------------------------------------------------------
const char CFd[] = "Duomenys.txt";     //TEXT FILE 
const char CRf[] = "Rezultatai.txt";   // ANALYSED AND EDITED TEXT
const int CMax = 10;
Tekstas T[CMax];
//-------------------------------------------------------------------------
void Skaityti(string dfv); //reading
bool NagrinetiZodi(string zodis); //analysis of words in stringstream
void NagrinetiEilute(Tekstas & T); // anlysis of line
void Redagavimas(string rfv); //editing
//-------------------------------------------------------------------------
int main()
{
	Skaityti(CFd);
	Redagavimas(CRf);
	return 0;
}
//-------------------------------------------------------------------------
// Funkcija nuskaito pradinius duomenis ir sudeda juos i string tipo masyva reads file and adds them to STRING ARRAY
// fdv - duomenu failo vardas
void Skaityti(string dfv)
{
	ifstream fd(dfv.c_str());
	string eil;
	int i = 0;
	int sk = 0;
	while (!fd.eof() && sk <= CMax)
	{
		getline(fd, eil);   // reads kube
		sk++;
		T[i].eilute = eil;      // gets line
		T[i].Uzpildyta = true;
		stringstream ss(eil);
		string zodis;
		int j = 0;
		while (ss.good())      // reads line
		{
			ss >> zodis;       // reads every word in line 
			T[i].zodziai[j] = zodis;  // zodis(word) every word added to T[i].zodziai array
			j++;
		}
		i++;
	}
	fd.close();
}
//-------------------------------------------------------------------------
// Funkcija analysis word
// zodis - funkcijai perduodamas nagrinejamas zodis
bool NagrinetiZodi(string zodis) // word analysis
{
	int ilg = zodis.length();
	int sk = ilg%2; // checks if its odd if it is it gives TRUE
	if (sk != 0)
		return true;
	else
		return false;

}
//-------------------------------------------------------------------------
// funcktions anlysis line
// Tekstas & T - funkcijai perduodamas objektas
void NagrinetiEilute(Tekstas & T)
{
	int i = 0;
	while (T.zodziai[i].length() !=0) // word length 
	{
		if (NagrinetiZodi(T.zodziai[i]) == true) // if gotten thing from nagrinetizodi is true then this is true
			T.Nelyginis = true;
		i++;
	}
}
//-------------------------------------------------------------------------
// function edits stuff
// string rfv - funkcijai perduodamas rezultatu failo vardas
void Redagavimas(string rfv)
{
	ofstream fr (rfv.c_str());
	int i = 0;
	while (T[i].Uzpildyta) // 
	{
		NagrinetiEilute(T[i]); // function 
		if (T[i].Nelyginis = true) // if true shoud erase but ????
			T[i].zodziai[i].erase();
		fr << T[i].eilute << endl;
		i++;
	}
	fr.close();
}
//------------------------------------------------------------------------- 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once
#include <string>

using namespace std;

class Tekstas
{
public:
	static const int CMax = 1000;
	string eilute;
	string zodziai[CMax];
	bool Uzpildyta;
	bool Nelyginis;
};
Last edited on
Assuming that the language is Lithuanian, and assuming that Google translate is knows what it is doing:

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
41
42
43
44
45
46
47
48
49
50
//////////  faila_modifikatorius.h ////////////////

#include <string>

struct faila_modifikatorius
{
     static void faila_konvertuoti( const char* ivesties_kelias, const char* produkcija_kelias ) ;

     private:
         static std::string& nutrinti_nelyginis_tekstas( std::string& text ) ;
};

//////////  faila_modifikatorius.cpp ////////////////
//#include "faila_modifikatorius.h"
#include <sstream>
#include <fstream>

std::string& faila_modifikatorius::nutrinti_nelyginis_tekstas( std::string& text )
{
    std::istringstream stm(text) ;
    std::string result ;
    std::string zodis ;
    while( stm >> zodis )
        if( zodis.size()%2 == 0 ) // even number of chars
        {
            if( !result.empty() ) result += ' ' ;
            result += zodis ;
        }

    return text = result ;
}

void faila_modifikatorius::faila_konvertuoti( const char* ivesties_kelias, const char* produkcija_kelias )
{
    std::ifstream ivesties_failas(ivesties_kelias) ;
    std::ofstream produkcija_failas(produkcija_kelias) ;
    std::string linija ;
    while( std::getline( ivesties_failas, linija ) )
        produkcija_failas << nutrinti_nelyginis_tekstas(linija) << '\n' ;
}

/////////////////  main.cpp ///////////////////
//#include "faila_modifikatorius.h"

int main()
{
    const char* const ivesties_kelias = "Duomenys.txt" ; // Duomenu failo pavadinimas
    const char* const produkcija_kelias = "Rezultatai.txt" ; // Rezultatu failo pavadinimas
    faila_modifikatorius::faila_konvertuoti( ivesties_kelias, produkcija_kelias ) ;
}
yes its lithuanian and im lithuanian, but english is fine for me...


all im asking is to try to get MY program to work and erase those words, i dont really need lithuanian(or any other language) translations

p.s edited text above to be easier read in english
Last edited on
1
2
3
4
1. Read lines one by one from the input file.
2. For each line read,
        a. remove words with odd number of chars from the line
        b. write the modified line to the output file. 


To remove words with odd number of chars from the line:
1
2
3
1. read words one by one from the line (use std::istringstream for this)
2. For each word, if the word contains an even number of chars, append it to the result string (with a space between words).
3. Finally assign the result string back to the line.

finally i got :D thanks JLBorges, you're awesome :) thanks thanks :)
Topic archived. No new replies allowed.