Problem with programm

I have a problem with a program i am making. it is a menu with three options. One is writing the needed data to a file and when CTRL+Z is pressed the input should stop. The second is loadin the data from the file and showing it on the screen. The third one is inputting an number and checking it with the rest.

My roblem is that when I try it it breaks after hitting CTRL+Z. When I tried it without a menu it worked but I have to have one. Any ideas on how to fix this.


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
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
struct mag
{
	char ime[20];
	char kod[3];
	double cena_e;
	double cena_d;
}s;
fstream f;
void in()
{
	f.open("articul.txt",ios::out);
	while(cin>>s.ime>>s.kod>>s.cena_e>>s.cena_d)
		f<<s.ime<<" "<<s.kod<<" "<<s.cena_e<<" "<<s.cena_d;
	f.close();
}
void out()
{
	f.open("articul.txt",ios::in);
	while(f>>s.ime>>s.kod>>s.cena_e>>s.cena_d)
	cout<<"\n "<<s.ime<<" "<<s.kod<<" "<<s.cena_e<<" "<<s.cena_d;
	f.close();
}
void com()
{
	cout<<" zadai 4islo za tursene";
	double max;
	cin>>max;
	f.open("articul.txt",ios::in);
	while(f>>s.ime>>s.kod>>s.cena_e>>s.cena_d)
		if(max>s.cena_d)
			cout<<s.ime;
	f.close();
}
void main()
{
	int k;
	while (k!=4)
	{
		//menu
		cout<<"\nInformacionna sistema";
		cout<<"\n1. Suzdavane na nova baza danni.";
		cout<<"\n2. Chetene ot bazata danni.";
		cout<<"\n3. 4etene i izvajdane na artikulite c cena po niska ot zadadenata.";
		cout<<"\n4. Izhod";
		cout<<"\nIzberi opciq: ";
		cin>>k;
		switch(k)
		{
			case 1: in(); break;
			case 2: out(); break;
			case 3: com(); break;
			case 4: exit(0);
		}
	}
}
1
2
3
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h> 


Bad. The .h headers of the standard library are deprecated and are only there for compatibility issues. Remove the .h for all of those.

void main()

Same. Many modern compilers will simply refuse to compile this. Main should always return an int.

Then, doing it with CTRL+Z (which would -in some but not all OS'es) insert an EOF character into the stream is kind of ugly. Inserting a EOF character will put the stream into fail state, which means all subsequent calls to cin>> will fail automatically without even a check of what's inside. You will have to put
1
2
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());


After all of your while loops in your functions.

Topic archived. No new replies allowed.