Unknown symbols when reading file and saving words to an array (iostream)

So the task is finding palindromes (symetrical words) from a text file and outputing them to another file. Text can be read only one symbol at a time.

Current code:
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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

bool symetry(char * x);

int main()
{
    fstream fout("f1.txt", ios::out);
    fstream fin("f.txt", ios::in);
    int i=0;
    char * arr;
    arr=new char[40];
    char symbol;


    if(!fin)cout<<"Unable to open fin"<<endl;
    if(!fout)cout<<"Unable to open fout file"<<endl;

    while(fin)
    {   fin.get(symbol);
        if ( symbol == ' ' || symbol == '!' || symbol == '?' || symbol == '.' || symbol == ',' || symbol == '(' ||symbol == ')' )
        {
        cout<<"Stoping at "<<arr<<endl;
        if(symetry(arr))cout<<"Symetrical word has been found: "<<arr<<endl;

        fout.write(arr,strlen(arr)-1);
        fout<<flush;
        arr[0] = '\0';
        i=0;}

        else

        {
        arr[i]=symbol;
        i++;}


    }
    delete[]arr;
    fin.close();
    fout.close();

    return 0;
}


bool symetry(char * x)
{
for(int y=0,z=strlen(x)-1;z>y;y++,z--)
{   cout<<x[y]<<" is being compared to "<<x[z]<<endl;
    if (x[y]!=x[z]) return false;
}
return true;
};


Output when input file contains "akka alfa beta ala". The output symbols after the real words change randomly when executing.
Stoping at akkał)y
a is being compared to y
Stoping at alfał)y
a is being compared to y
Stoping at betał)y
b is being compared to y

Process returned 0 (0x0)   execution time : 0.044 s
Press any key to continue.


How to get rid of those symbols so I can actually find the symetry?
Or perhaps I should go some different route to achieve the task stated in the beginning?
Just add null terminator. This way the string is terminated and strlen works. But if you don't have a space at the end of the line you won't process the last word. Do you need to process the data one character at a time?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(fin)
    {   fin.get(symbol);
        if ( symbol == ' ' || symbol == '!' || symbol == '?' || 
                symbol == '.' || symbol == ',' || symbol == '(' ||symbol == ')' )
        {
        arr[i] = '\0';   // <----------------------- Added null terminator
        cout<<"Stoping at "<<arr<<endl;
$ ./a.out
Stoping at akka
a is being compared to a
k is being compared to k
Symetrical word has been found: akka
Stoping at alfa
a is being compared to a
l is being compared to f
Stoping at beta
b is being compared to a
Stoping at ala
a is being compared to a
Symetrical word has been found: ala
$
Last edited on
Figured that out aswell while waiting for a response :> Also - yes it is a requirment to process the data one character at a time, even the main goal of the task I guess.

Current code (also ends the array when eof is reached now):
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

#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;

bool symetry(char * x);
int main()
{
    fstream fin("f.txt", ios::in);
    fstream fout("f1.txt", ios::out);

    int i=0;
    char * arr;
    arr= new char [40];
    char symbol;


    if(!fin)cout<<"Unable to open input file"<<endl;
    if(!fout)cout<<"Unable to open output file"<<endl;


    while(fin)
    {
        fin.get(symbol);

        if ( fin.eof() || symbol == ' ' || symbol == '!' || symbol == '?'
        || symbol == '.' || symbol == ',' || symbol == '(' ||symbol == ')' )
        {
        arr[i]='\0';
        cout<<endl;
        cout<<"Stoping at '"<<arr<<"' "<<endl;
        if(symetry(arr)&&i>0)
        {cout<<"Symetrical word has been found: "<<arr<<endl;

        fout<<arr<<'\n';
        fout<<flush;
        }
        arr[0] = '\0';
        i=0;
        }

        else

        {
        arr[i]=symbol;
        i++;

        }


    }

    fin.close();
    fout.close();

    return 0;
}


bool symetry(char * x)
{
for(int y=0,z=strlen(x)-1;z>y;y++,z--)
{   cout<<x[y]<<" is being compared to "<<x[z]<<endl;
    if (x[y]!=x[z]) return false;
}
return true;
};


It works quite well but yet another requirment of the task is that the file should be read sequentialy and in my textbook it is defined as
1
2
3
4
5
6
Read the first value from file
WHILE (file hasn't ended)
{
Process the value read
Read the next value from file
} 


Yet if I try to read the value first, then process it inside the while cycle and then read the next value inside it - the program goes into a loop and just keeps on returning
Stoping at ' '

over and over. How to overcome that?
Last edited on
Here is a copy of my test INPUT file "F.TXT"

The prog has trouble at the end of each line, and finally crashes near the end.

I'll just have to spend some time to figure-it-out. I'm thinking it's the same problem as before - end end of line terminator. ( somewhere)

ala ddd iffi abbba abbcbba
this file is used with Palindromes
akka alfa abbxbba abbybba abbzbba abbabba abbbbba abbcbba aaa
wwwww wwwaww wwwbwww wwwcwww wwwdwww wwwewww
xxxxx nnnnn xxxxxx.,;xxxxxxx xxxxxx.,;xxqqjqqqxxxxpx mmmmm ......
yyyyy
zzzzz

beta
ala
ala
ddd
iffi
abbba
abbcbba

yyyyyyyyyyyyy
zzzzzzzzzzzzz
0000000000000
Adding " if (symbol == '\n ' || ...........)" at the part where it checks for the end of the "word" solves the crashing and errors from new lines but still the sequential reading part mentioned before puzzles me. Not sure of what causes that loop if I modify it as in the textbook example
Last edited on
Not sure why you would get infinite loop with seeing what you did, but I think this does what you want. I used Incis B input file.
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
    fin.get(symbol);
    while(fin) {
        if ( fin.eof() || symbol == ' ' || symbol == '!' || 
              symbol == '?' || symbol == '\n' || symbol == '.' || 
                  symbol == ',' || symbol == '(' ||symbol == ')' ) {
           arr[i]='\0';
           cout<<endl;
           cout<<"Stoping at '"<<arr<<"' "<<endl;
           // I put the i check first because if it fails there 
           // is no need to call symetry
           if(i>0 && symetry(arr)) {  
             cout<<"Symetrical word has been found: "<<arr<<endl;
             fout<<arr<<'\n';
             fout<<flush;
           }
           arr[0] = '\0';
           i=0;
        }
        else {
           arr[i]=symbol;
           i++;
        }
        fin.get(symbol);
    }
Stoping at 'ala' 
a is being compared to a
Symetrical word has been found: ala

Stoping at 'ddd' 
d is being compared to d
Symetrical word has been found: ddd

Stoping at 'iffi' 
i is being compared to i
f is being compared to f
Symetrical word has been found: iffi

Stoping at 'abbba' 
a is being compared to a
b is being compared to b
Symetrical word has been found: abbba

Stoping at 'abbcbba' 
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbcbba

Stoping at 'this' 
t is being compared to s

Stoping at 'file' 
f is being compared to e

Stoping at 'is' 
i is being compared to s

Stoping at 'used' 
u is being compared to d

Stoping at 'with' 
w is being compared to h

Stoping at 'Palindromes' 
P is being compared to s

Stoping at 'akka' 
a is being compared to a
k is being compared to k
Symetrical word has been found: akka

Stoping at 'alfa' 
a is being compared to a
l is being compared to f

Stoping at 'abbxbba' 
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbxbba

Stoping at 'abbybba' 
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbybba

Stoping at 'abbzbba' 
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbzbba

Stoping at 'abbabba' 
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbabba

Stoping at 'abbbbba' 
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbbbba

Stoping at 'abbcbba' 
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbcbba

Stoping at 'aaa' 
a is being compared to a
Symetrical word has been found: aaa

Stoping at 'wwwww' 
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwww

Stoping at 'wwwaww' 
w is being compared to w
w is being compared to w
w is being compared to a

Stoping at 'wwwbwww' 
w is being compared to w
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwbwww

Stoping at 'wwwcwww' 
w is being compared to w
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwcwww

Stoping at 'wwwdwww' 
w is being compared to w
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwdwww

Stoping at 'wwwewww' 
w is being compared to w
w is being compared to w
w is being compared to w
Symetrical word has been found: wwwewww

Stoping at 'xxxxx' 
x is being compared to x
x is being compared to x
Symetrical word has been found: xxxxx

Stoping at 'nnnnn' 
n is being compared to n
n is being compared to n
Symetrical word has been found: nnnnn

Stoping at 'xxxxxx' 
x is being compared to x
x is being compared to x
x is being compared to x
Symetrical word has been found: xxxxxx

Stoping at '' 

Stoping at ';xxxxxxx' 
; is being compared to x

Stoping at 'xxxxxx' 
x is being compared to x
x is being compared to x
x is being compared to x
Symetrical word has been found: xxxxxx

Stoping at '' 

Stoping at ';xxqqjqqqxxxxpx' 
; is being compared to x

Stoping at 'mmmmm' 
m is being compared to m
m is being compared to m
Symetrical word has been found: mmmmm

Stoping at '' 

Stoping at '' 

Stoping at '' 

Stoping at '' 

Stoping at '' 

Stoping at '' 

Stoping at '' 

Stoping at 'yyyyy' 
y is being compared to y
y is being compared to y
Symetrical word has been found: yyyyy

Stoping at 'zzzzz' 
z is being compared to z
z is being compared to z
Symetrical word has been found: zzzzz

Stoping at '' 

Stoping at 'beta' 
b is being compared to a

Stoping at '' 

Stoping at 'ala' 
a is being compared to a
Symetrical word has been found: ala

Stoping at 'ala' 
a is being compared to a
Symetrical word has been found: ala

Stoping at 'ddd' 
d is being compared to d
Symetrical word has been found: ddd

Stoping at 'iffi' 
i is being compared to i
f is being compared to f
Symetrical word has been found: iffi

Stoping at 'abbba' 
a is being compared to a
b is being compared to b
Symetrical word has been found: abbba

Stoping at 'abbcbba' 
a is being compared to a
b is being compared to b
b is being compared to b
Symetrical word has been found: abbcbba

Stoping at '' 

Stoping at 'yyyyyyyyyyyyy' 
y is being compared to y
y is being compared to y
y is being compared to y
y is being compared to y
y is being compared to y
y is being compared to y
Symetrical word has been found: yyyyyyyyyyyyy

Stoping at 'zzzzzzzzzzzzz' 
z is being compared to z
z is being compared to z
z is being compared to z
z is being compared to z
z is being compared to z
z is being compared to z
Symetrical word has been found: zzzzzzzzzzzzz

Stoping at '0000000000000' 
0 is being compared to 0
0 is being compared to 0
0 is being compared to 0
0 is being compared to 0
0 is being compared to 0
0 is being compared to 0
Symetrical word has been found: 0000000000000
Last edited on
The problem was me taking the next symbol in the else part rather than in the main part of the while cycle. Silly me. Everything works as it should now, thank you all for the repsonses :)
Topic archived. No new replies allowed.