error dealing with end of file

this program is designed to take redireted input from a file and count the characters, words and lines in it, but it seems to immediantly see the end of file for any file put into it. the "countlinewordchar" function is where the EOF check is. could someone please explain what i'm doing wrong?

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
  #include <iostream>
#include <ctype.h>
#include <string>
#include <fstream>
using namespace std;

struct CmdLineRecord
{
   enum { MAX_NAME_LENGTH = 32 };    // Max allowable filename chars
   int SyntaxError;                  // True if user syntax is incorrect
   int WantsLineCount;               // set to true if user wants line count
   int WantsWordCount;               // set to true if user wants word count
   int WantsCharCount;               // set to true if user wants char count
};

struct CountsRecord
{
   unsigned LineCount;          // number of lines
   unsigned WordCount;          // number of words
   unsigned CharCount;          // number of characters
};

void DetermineWhatUserWants(CmdLineRecord & User, int arg, char * arv[]);
int CountLineWordChar(CountsRecord & Data, char File[]);
void ReportResults(CmdLineRecord User, const CountsRecord & Data);


int main(int argc, char * argv[])
{
   CmdLineRecord User;
   CountsRecord Data;
   int FileOpened = 1;

   DetermineWhatUserWants(User, argc, argv);
   if (!User.SyntaxError)
      FileOpened = CountLineWordChar(Data, argv[1]);

   if (FileOpened)
      ReportResults(User, Data);
   else
     cerr << "Cannot open file " << argv[1];

   return 0;
}

/**************  DetermineWhatUserWants  ****************************
Action  :  Function will determine what the user wants to have displayed
           for the count, based on command line input, i.e. does user
           want to display how many lines or words or characters or
           combination of them, or if an input error occurred. Second
           argument in command line must be the file name the third argument
           is the option of display.

           Command line options are inputed after the "/" char and are:
                c : character count
                w : word count
                l : line count

           If no options are inputed then the default of display all count
           totals is done, otherwise the option is done.  One option or two
           is permitted after the "/", illegal character option will result
           in a syntax error.

Parameters:
  Reference  :
    U        : User variable of struct type given above
  Value
    arg      : number of command line arguments
    *arv[]   : array of pointers each pointing to different argument
Returns    : nothing
Preconditions : None
=======================================================================*/
void DetermineWhatUserWants(CmdLineRecord & U, int arg, char * arv[])
{
	U.SyntaxError = 0;
	U.WantsLineCount = 0;    //initialize all wants to zero to start
	U.WantsCharCount = 0;
    U.WantsWordCount = 0;

	if (arg>3)
	{
		U.SyntaxError=1;
		return;
	}	
	
	if (arg==2)
	{
		U.WantsLineCount=1;
		U.WantsCharCount=1;
		U.WantsWordCount=1;
	}
	else if (arv[2][0]!='\\')
	{
		U.SyntaxError=1;
		return;
	}
	else
		switch (arv[2][1]||arv[2][2]||arv[2][3])
		{
			case 'l':U.WantsLineCount=1;
					break;
			case 'w':U.WantsWordCount=1;
					 break;
			case 'c':U.WantsCharCount=1;
			  		 break;
			default: U.SyntaxError=1;
					 return;
		}
}

/*********************  CountLineWordChar  *****************************
Action  :  Function will count the number of characters, words and lines
           in given input stream of text ended by control Z, ^Z, EOF

Parameters:
  Reference :
    Data    : variable of type struct given above
  Value
    File[]  : second command line argument that has the file to read from

Returns   : 1 if file opened, 0 if can not open file or not found

NOTE   :  Characters are everything, including newline and form feed.
          Words are delimited by whitespace characters and EOF.
          Does not take into consideration hypentation, words are composed
          numbers and letters, punctuation also included in words.

Precondition : none
=======================================================================*/
int CountLineWordChar(CountsRecord & Data, char File[])
{
   char Ch;							 // current character in stream
   ifstream FileIn;	          // declare FileIn to be input file

    Data.CharCount = 0;
    Data.WordCount = 0;
    Data.LineCount = 0;

    FileIn.get(Ch);
	++Data.CharCount;
   while(!EOF)
   {
		while (!isspace(Ch))
		{
			cin.get(Ch);
			++Data.CharCount;
		}
		++Data.WordCount;
		if (Ch=='\n')
			++Data.LineCount;
		cin.get(Ch);
		++Data.CharCount;
   }
   if (Data.CharCount>=1)
	   ++Data.LineCount;
   return 1;
}

/**********************  ReportResults  ********************************
Action  :  Function will display the number of words, lines or characters
           or all of them depending on what the user entered on the command
           line input.
Parameters :
  Value    : 2 value parameters, User - what to display
                                 Data - holds the number to display
Returns    : nothing
======================================================================*/
void ReportResults(CmdLineRecord User, const CountsRecord & Data)
{
	if (User.WantsCharCount==1)
		cout<<"Characters = "<<Data.CharCount<<endl;
	if(User.WantsWordCount==1)
		cout<<"Words      = "<<Data.WordCount<<endl;
	if(User.WantsLineCount==1)
		cout<<"Lines      = "<<Data.LineCount<<endl;
}
Topic archived. No new replies allowed.