Problem writing into 2 text files

I have the following problem when I try to run a simple writing to a text file: for the first text file : "alfa.txt" the program works fine, but for the "beta.txt" text file the program stops working and I can't write anything to it. Here is the source code:

#include<iostream.h>
#include<fstream.h>
#include<stdio.h>

void main()

{char ch1,ch2;

fstream f1("alfa.txt",ios::out);
fstream f2("beta.txt",ios::out);
while(cin>>ch1)
f1<<ch1;
f1.close();

while(cin>>ch2)
f2<<ch2;
f2.close();

}

Can anybody help me with this thing?
Thank you very much!
What exactly do you want to write to alpha and beta files?
Mainly names, and for alfa file it works fine, I write the names, then i hit Ctrl+Z to finish the reading from the keyboard and when I want to do the same thing with beta file (the second while) it won't work. The window turns inactive!
Insert cin.clear(); before while(cin>>ch2) .
After you you pressed Ctrl-Z cin is in eof state. cin.clear(); clears the state and allow to continue work.
I just tried cin.clear(); and I get the same thing...nothing. could you please try and run this code on your compiler and see what do you get?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;

int main()

{char ch1,ch2;

fstream f1("alfa.txt",ios::out);
fstream f2("beta.txt",ios::out);
while(cin>>ch1)
f1<<ch1;
f1.close();
cin.clear();
while(cin>>ch2)
f2<<ch2;
f2.close();

}


worked on Linux (with Ctrl-D - as end of input) and works on Windows (but before and after Ctrl-Z I need to press Enter)
At last! It worked ! The problem was my version of C++, I was working on the 3.1 version. Now I've downloaded the 5.2 version and it works like a charm, exactly as you've said - by pressing Enter before and after hitting Ctrl+Z.

Thank you very much for your help!
I have another question. Now the program works fine and writes the names into the text files. But how can I sort alphabetically the names into the two text files? Can you give me an example?
Topic archived. No new replies allowed.