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.
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...
#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' ;
}
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' ;
}
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...
/*
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
usingnamespace std;
//-------------------------------------------------------------------------
constchar CFd[] = "Duomenys.txt"; //TEXT FILE
constchar CRf[] = "Rezultatai.txt"; // ANALYSED AND EDITED TEXT
constint 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)
returntrue;
elsereturnfalse;
}
//-------------------------------------------------------------------------
// 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;
};
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 forthis)
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.