Output is just a little off
Apr 29, 2016 at 12:47pm UTC
I've been trying to troubleshoot one of my functions and during my output I get some letters in between my words. I am using a file and the first line reads:
Whether a translation of HOMER may be best executed in blank verse or
I am not sure what to do to fix this function, the function in question is the extract_word function.
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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
bool read_line(ifstream &fin, char line[]);
bool extract_word(char line[], char temp[], int &index);
int main()
{
int k = 0;
ifstream fin;
ofstream fout;
char line[100] = { " " };
int index = 0;
char temp[30] = { " " };
fin.open("other.txt" );
fout.open("output.txt" );
bool readline = true ;
bool word = true ;
while (readline == true )
{
readline = read_line(fin, line);
index = 0;
while (word == true )
{
word = extract_word(line, temp, index);
cout << temp << endl;
for (k = 0; k < 30; k++)
{
temp[k] = '\0' ;
}
}
cout << temp << " " ;
}
if (readline == false )
{
cout << " end of file" ;
}
fout << line;
}
bool read_line(ifstream &fin, char line[])
{
while (!fin.eof())
{
fin.getline(line, 100);
return true ;
}
return false ;
}
bool extract_word(char line[], char temp[], int &index)
{
int i = 0;
int j = 0;
int k = 0;
bool check = true ;
if (!isalpha(line[index]))
{
return false ;
}
cout << line[index + 1] << endl;
while (isalpha(line[index]))
{
temp[i] = line[index];
i++;
index++;
}
check = true ;
int checker = 0;
checker = index;
for (j = 1; !isalpha(line[checker - j]) ; j++)
{
temp[checker - j] = '\0' ;
}
index++;
return check;
}
Apr 29, 2016 at 8:19pm UTC
> during my output I get some letters in between my words.
perhaps you may want to consider to get rid of line 77.
Topic archived. No new replies allowed.