Reading and writing... Problems in while loops.

Write a program system that reads several lines of information from a data file and prints each word of the file on a separate line of an output file followed by the number of letters in that word.

Also, print a count of words in the file on the screen when done. Assume that words are separated by one or more blanks.


Any idea what is wrong with the logic of my while loops? Can't get them to work properly...
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
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string> 
#include <fstream>
#define inFile "C:\\Users\\s\\Desktop\\FK\\Ch8_3.txt"
#define outFile "C:\\Users\\s\\Desktop\\FK\\out.txt"
using namespace std;
void processData(ifstream&, ofstream&);
using namespace std;

int main()
{
ifstream ids;
ofstream ods;



ids.open(inFile);  // I know this opens the files I am reading from and writing to.
ods.open(outFile);
if (ids.fail()){
	cerr<<"error can't open "<< inFile<<" for input"<< endl;
	return EXIT_FAILURE;
}
if (ods.fail()){
	cerr<<"error can't open "<< outFile<<" for input "<< endl;
	return EXIT_FAILURE;
}
processData(ids, ods);

ids.close();
ods.close();
return 0;
}
void processData(ifstream& ids, ofstream& ods){ 
	char NWLN ='\n';
	char nextchar;
	int charCount;  
	string word;
	
	ids.get(nextchar);
	while(!ids.eof()) //while not end of file
	{
		charCount=0;// set the count to 0
		
		while (nextchar!=NWLN){ // while it is not a new line
			
		while(nextchar!=' ') { // continue getting the next character   while also counting each new character. THis continuse until you hit a space
			 
			charCount++; 
			cout<< nextchar << endl;
			ids.get(nextchar);
			word=nextchar+nextchar;
			//ods<<(word);
		}
		
		}

		//ods.put(nextchar);
		//ods<< setw(30-charCount)<< "    letter count = " << charCount<< endl;
}
		
}
	

	
Last edited on
The processData() function reads file character by character, which is not very practical. It is much easer to read a word from file using >> operator. To get the length of the word you can use string::length() function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void processData(ifstream& ids, ofstream& ods){
	int charCount;
	string word;

	while(!ids.eof()) //while not end of file
	{
         ids >>word; // read a word from file
         charCount=word.length(); // length of the word

         // output
         ods <<word;
		 ods<< setw(30-charCount)<< "    letter count = " << charCount<< endl;
    }

} 



Hope this helps.
Thanks for your help. That way is much simpler, but my professor wants me to do it the long way to make sure I understand the logic. I understand the logic... but dont know exactly how to implement it.
If the infile has a newline as the first line you will have an infinite loop in the outer while because...

nextchar = '\n'
Not the end of file so enter first while loop.
nextchar is a newline so do not enter second while loop and consequently nextchar is not updated and remains '\n'.
ids never reaches eof so loop forever.

You have an infinite loop in the second while once the first space is read because..

space after the last char of a word is read in the third while loop.
nextchar == space
nextchar != '\n' so remain in second while loop
but since nextchar == space don't enter third while loop

Also this word=nextchar+nextchar; is wrong e.g. if nextchar is 't' say, word is "tt". actually nextchar+nextchar returns the char with ascii code equal to twice nextchar's ascii code.
You should set word to empty string and add each char to it.
When end of word is reached reset word to empty string.
Last edited on
Topic archived. No new replies allowed.