How to EOF

Does anybody know how to do EOF ?

How do you do it so that it has the option for Ctr+Z and for Unix Ctr+D

This is my code for example, where, and how exactly do I add it?

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

void getDate(int& mon, int& day, int& year);
int ckDate(int mon, int day, int year);	
void displayMsg(int message);

int main()
{
	int mon, day, year, message;
	string date;
	

	while(cin)
     {
		getDate(mon,day,year);			// call function getDate
		int message=ckDate(mon,day,year);		// cal function ckDate
		cout << mon << "/" << day << "/" << year << "-";
		displayMsg(message);					//call function displayMsg
		
     }
	
	_getch();
	return 0;

}  

void getDate(int& mon, int& day, int& year)
{	cout << "\n\nEnter a date (mm/dd/yyyy): ";
	cin >> mon;
	cin.ignore(100, '/');
	cin	>> day;
	cin.ignore(100, '/');
	cin >> year;
	
	
}	

int ckDate(int mon, int day, int year)
{	
	
	 bool isLeapYear = false;
	 int resultNum = 0;

           if ((year <= 999) || (year > 10000))
			resultNum = 1;//attempting to change from multiple return statements with an integer to a variable resultNum.

               
           
           if ((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 100 == 0 && year % 400 == 0))
               isLeapYear = true;

           
           switch (mon)
           {
               case 1:
                   if (day < 1 || day > 31) 
					   resultNum = 3;
				//resultNum = 3; attempting to assign variable to 3 
				//return resultNum;//returning it to displayMsg

                   break;
               case 2:
                   if (isLeapYear && (day < 1 || day > 29)) 
					   resultNum = 5;
                   if (!isLeapYear && (day < 1 || day > 28)) 
					   resultNum = 6;
                   break;
               
               case 3: // the month was not 1-12 so return 2
					if (mon < 1 || mon > 12) 
						resultNum = 2;
					break;
				   return resultNum;//appears to work correctly


           }
          	return resultNum;//appears to work correctly	
				return 0;
	}



void displayMsg(int message)
{
	switch(message)
	{
	case 0:
		cout << " Good Date";
		break;
	case 1:
		cout << " Bad Year";
		break;
	case 2:
		cout << " Bad Month";
		break;
	case 3:
		cout << " Bad Day not 1-31";
		break;
	case 4:
		cout << " Bad Day not 1-30";
		break;
	case 5:
		cout << " Bad Day not 1-29";
		break;
	case 6:
		cout << " Bad Day not 1-28";
		break;
	}

}

what do you mean "do EOF"?
do you mean object.eof();
or object.close();?
be specific
I mean cin.eof

Didn't know there were other ones but figured that you would be able to understand considering I mentioned the ctrl z and ctrl d
I'm not exactly sure what you are asking but you should default those last cases in your switch statements.
Yea your question doesn't make sense. EOF = end of file. So doing EOF doesn't really mean anything.
What you could do is read until the first read operation fails. It could be because the user enters an invalid input or because that EOF is reached. In your code on line 16 you actually do this already. The problem with the way you do it is that you check cin before reading so the loop will run once too much. If you make getDate return a reference to cin you can check that in the loop condition instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...

while(getDate(mon,day,year))
{
	int message=ckDate(mon,day,year);
	cout << mon << "/" << day << "/" << year << "-";
	displayMsg(message);
}

...

istream& getDate(int& mon, int& day, int& year)
{
	cout << "\n\nEnter a date (mm/dd/yyyy): ";
	cin >> mon;
	cin.ignore(100, '/');
	cin	>> day;
	cin.ignore(100, '/');
	cin >> year;
	return cin;
}
Last edited on
so then how would I add the cin.eof to this?

I want to make it so that the ctrl-z and the ctrl-d would display something and then to close.
ended up doing this and it seems to work

1
2
3
4
5
6
7
8
9
while(cin.eof())
	{
	 cout<<"Good Bye"<<endl;
	 cin.get();
	 _getch();
	 exit(1);
	 {
	 
	 }
Topic archived. No new replies allowed.