String exersice

Hello everyone, this is my first time here. I need a little help for my last assignment of the year:
I have to read a file and invert the order of the even words, while odd words only exchange the ends (three letters at the middle of odd words stay the same) and three letter words don't change. Then I have to write these words in another file.
This is the code I have so far, the problem is that it only changes one word of my file. I don't know if this is the way to do it as I can't see what my program does to the words.
This is my file (spanish):
7
no
por
ouchm
ragurdam
ceaneam
mas
onarpmet

The resulting file should be:
"no por mucho madrugar amanece mas temprano"

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
 //---------------------------------------------------------------------------
#include <iostream.h>
#include <fstream.h>
#include <vcl.h>
#pragma hdrstop
#pragma argsused
//---------------------------------------------------------------------------
string processeven (string word);
string processodd (string word);

int main(int argc, char* argv[])
{

string phrase;

fstream lectura;
lectura.open("D:\\refran.in",ios::in);
lectura>>phrase;

cout<<phrase<<endl;
   for (int i=0; i<=6; i++)/// I know I should use the first line of the file for this loop, but I couldn't figure out how to do it
   {
   lectura>>phrase;
   cout<<phrase<<endl;
   }

cout<<endl;
cout<<processeven(phrase)<<endl; /// this would be deleted after I put the words in the file
cout<<processodd(phrase)<<endl;

system ("pause");
return 0;
}
//---------------------------------------------------------------------------

string processeven (string word)
{
int size = word.size();
string aux;
        if (size % 2 == 0)
        {
                for (int i=size; i>=0; i--)
                 {
                 aux += word[i];
                 }
                return aux;
        }
}

string processodd (string word)
{
int size = palabra.size();
int odd =size % 2;
int divide;
int j=0;
string aux;
string word2;

        if ( odd != 0 && size >= 3)/// This is because 3 letter words stay the same
        {
        divide =(size-3) / 2 ;
                for (int i=0; i<divide ; i++)
                {
                word2[size] = word[i];
                i++;
                size--;
                }
                for (int i=0; i<divide ; i++)
                {
                word2[j] = word[size];
                i++;
                j++;
                size--;
                }
        }
return word2;
}
The number in the file helps, but you could write the program so that not even that number is needed.

If the file was simply:

no
por
ouchm
ragurdam
ceaneam
mas
onarpmet 


You could read it like:

1
2
3
4
5
6
7
8
9
10
11
while (lectura >> phrase)
{
    // ... process phrase...
}

// if some "phrases" could contain spaces

while (std::getline(lectura, phrase))
{
    // ... process phrase...
}


http://www.cplusplus.com/reference/string/string/getline/

To use the number, you could do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstddef>

// prefer to use size_t for lengths and sizes, instead of using int
std::size_t number_of_phrases = 0;

lectura >> number_of_phrases;

for (std::size_t i=0; i < number_of_phrases; ++i)
{
    lectura >> phrase;

    // ... process phrase...
}


This is the code I have so far, the problem is that it only changes one word of my file.

This is because you keep reading the words but you don't change them:

18
19
20
21
22
23
24
25
26
27
28
29
lectura>>phrase;

cout<<phrase<<endl;
   for (int i=0; i<=6; i++)
   {
   lectura>>phrase; // reading new phrase
   cout<<phrase<<endl; // printing it unchanged
   }

cout<<endl;
cout<<processeven(phrase)<<endl; // phrase is now the LAST word!
cout<<processodd(phrase)<<endl;


You must process the phrases right after you read them.
Topic archived. No new replies allowed.