Cin gets ignored ?

Same story again. In the void nfile() and in ofile(), the program gives the output but ignores the input (cin) and gives the output: "Error occured...".

Can someone please explain and correct what i do wrong?
It is annoying to say the least.

Thank you.


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
void file(){


	string choice;

	char x;
	


	cout << "Would like to 'open' a file or 'create' a one ? " << endl << endl;
	cin >> choice;

	if(choice == "new"){x = 'c';}
	if(choice == "create"){x = 'o';}
	else{cout <<"Error, I don't know that command ? Could you please chose one of the one that I know, Thanks! " << endl;
			Sleep(3000);
			system("CLS");
			return file();}

	switch (x){
	
	case 'c': nfile();

	case 'o': ofile();
		

	}
	system("CLS");
	return CReturn();

}
void nfile(){

	
	ofstream myfile;
	char filename[50];
	string userinput;


	cout << "Enter the filename: ";
	std::cin.sync(); 
	cin.getline(filename, 50);
	
	myfile.open(filename);

	if(myfile.is_open()){
	
		cout << "Enter Text here: " << endl << endl;
		cin.sync();
		getline(cin, userinput);
		myfile << userinput;
	
	}

	else{cout << "Error Occured..." << endl;
		system("PAUSE");}

	myfile.close();

}
void ofile(){


	ofstream myfile;
	char filename[50];
	string userinput;


	cout << "Enter the filename: ";
	cin.getline(filename, 50);

	myfile.open(filename);

	if(myfile.is_open()){
	
		cout << "Enter Text here: " << endl << endl;
		getline(cin, userinput);
		myfile << userinput;
	
	}

	else{cout << "Error Occured..." << endl;
			cout << filename << endl;
		system("PAUSE");}

	myfile.close();

		system("CLS");

}
http://www.cplusplus.com/forum/articles/6046/

You need to clear the input buffer.
Topic archived. No new replies allowed.