getline and cin abnormal output

hey guys I am having the following error for this code when I try to input file values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout<<"Please enter file name for reading: ";
	cin.getline(fileName,15,'\n');
	
	cout<<"Please enter values for file: ";
		while(!(cin>>floatNum)){
			cout<<"Input float numbers ";
			cin.clear();
			cin.ignore();
			}
	file.open(fileName);
		for(int i=0; i<numbers;i++){
			file<<floatNum<<endl;
		
		}
	
	file.close();



The error output:

Hello. How many numbers you wish to put in file: 5
Please enter file name for reading: Please enter values for file: average.txt
Input float numbers Input float numbers Input float numbers Input float numbers Input float numbers Input float numbers Input float numbers Input float numbers Input float numbers Input float numbers

Please help me why this is happenning?
You didn't provide the part of the code which causes the problem. But I guess it looks a bit like this:
1
2
3
    int numbers = 0;
    cout << "Hello. How many numbers you wish to put in file: ";
    cin >> numbers;


and how does it work? The message "How many numbers you wish to put in file:" is displayed. The user types "5" and then presses enter.

Now everything which the user types is held in the input buffer, including the enter key, which is stored as a newline character. The line cin >> numbers; will skip any leading whitespace, then read the numeric characters and put it into the variable numbers. As soon as some non-numeric character is encountered, the cin >> will stop reading, that means the newline character is left in the buffer.

That has a knock-on effect for the following getline, which reads characters from the buffer until it reaches the newline, which is read and discarded. Thus fileName is read as an empty string, a line of length zero.

After that, the user is asked to enter the number, cin>>floatNum but instead the user types "average.txt" which of course is not a number. Hence the cin will fail and the loop will be executed repeatedly. The cin.ignore() will discard a single character of the text "average.txt" each time until eventually the buffer is emptied.

How to avoid this mess? After cin >> numbers;, clear the input buffer before the getline, by something like:
 
    cin.ignore(1000, '\n');
which will ignore up to 1000 characters, or until a newline is found, whichever comes first.

There are a few other issues with the code. The prompt message tells the user the program will read from the file, but the code file<<floatNum<<endl; is trying to write to the file, not read from it. I would change the text of the message to avoid the user accidentally overwriting some important file. The difference between read and write is not trivial.

Also the for loop at line 11 will output the same value to the file repeatedly which might not be what you actually want?

Last edited on
thanks i understand now. Its all about buffer reading...I solved the prob but one more question about buffer. Does buffer process data character by character always? And how many types of I/O are there in c++. stdio, file i/o and?...thanks again :)
and yes I wanted to insert diff values to file for a number of times...
There is C I/O (c streams) defined in cstdio: stdin / stdout /stderr /... and C file streams (FILE*)

And there is C++ streams: generic streams (which std::cin / std::cout / std::cerr / ... are instances of), inherited from them file streams and string streams.

So there is actually 2 differend I/O types: C-like and C++-like
Topic archived. No new replies allowed.