EOF problem

Hi everyone, I am writing a program that counts how many times each of the numbers 0-4 have been typed.

the exercise asks that the input will be halted with ^Z (EOF).

This is what I written so far:

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
//freq.cpp
//the program counts how many times each of the numbers 0-4 have have been typed

// Preprocessor for include files
#include <stdio.h>			// C style I/O
#include <iostream>			// C++ style I/O using operator 
using namespace std;		// The C++ logical collection of functions

void main() {
	//initialize variables
	
	const int end_of_file=26; //dec number for CONTROL+Z to finish character reading
	const int four=52;	//dec number for number 4 
	const int zero=48; // dec number for number zero
	char c; //is the character variable that will be used as counter.
	int number_four=0; // counter that tells how many 4 have been typed
	int number_zero=0; // counter that tells how many 0 have been typed
	int counter=0; // counter that tells how many digits have been typed

	printf("please type any number.\n when done press CTRL+Z");
	
	
	//collects characters and assigns them to variable c
	while((c=getchar())!=end_of_file){
		//while((c=getchar())!=EOF){ is not working, instead it works with a custom end_of_file variable
		switch (c){
		case zero:number_zero++;break;	//counts the 0s
		case four:number_four++;break;	//counts the 4s
		default:counter++;break;		//counts other characters
		}
	} 
	 
	//prints the results
	cout<<"the number of '0's is: "<<number_zero<<endl;
	cout<<"the number of '4's is: "<<number_four<<endl;
	cout<<"the number of other characters is: "<<counter<<endl;
	cout<<"the total number of characters is: "<<counter+number_zero+number_four<<endl;
}


So if you can see, I defined a custom variable called end_of_file as replacement for the EOF varible, because I have not been able to use the latter correctly, if I delete line number 24 and replace it with line number 25, why does my program wrecks and stops working?

Thanks.
Last edited on
1
2
const int four=52;	//dec number for number 4 
const int zero=48; // dec number for number zero 

This is not portable; there is no guarantee that the char '4' when converted to an int would yield 52.

> The exercise asks that the input will be halted with ^Z (EOF).

This too is not portable (it is Windows specific. EOF would be ^D on most Unix implementations.)

Consider writing code that would run on any implementation, using any character encoding, as long as there is support for the notion of an end of file for stdin. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
    int num_zeroes = 0 ;
    int num_fours = 0 ;
    int num_others = 0 ;

    char input ;
    std::cin >> std::noskipws ; // do not skip over white spaces
    while( std::cin >> input ) // as long as we have read a char (not reached eof)
    {
        if( input == '0' ) ++num_zeroes ;
        else if( input == '4' ) ++num_fours ;
        else ++num_others ;
    }

    std::cout << '\n' << num_zeroes << ' ' << num_fours << ' ' << num_others << '\n' ;
}
Hi JLBorges,

Thanks for your reply. Although what you have said is totally true regarding portability, I MUST follow instructions.

The exercises states:

"The input will be halted with ^Z (EOF). EOF means End-of-File and is defined in <stdio.h>. Thus, the constant EOF can be used in a condition (test if EOF has been typed). Print the amount of times a certain number has been typed."

I am using visual studio in windows 7 to compile the program. Could this be the reason why the EOF constant does not work as I wish?

thanks
Do as JLBorges suggests and test against '0' and '4' directly.

Your assignment is misleading; there is no EOF character. The reason your code failed to detect the EOF is because EOF is an int value:

1
2
3
4
int c;
...
while ((c = getchar()) != EOF) {
  ...

Hope this helps.
Topic archived. No new replies allowed.