[Broken Code, C++] Loop when input is larger than cstring

I'm trying to write the first part of a safe program that prompts the user to enter a file and verifies that the file exists.

It is the first part of a bigger program. It has to be written using only c-strings, no strings.

The loop functions when the file is found, but when the input is larger than the limit, it loops.

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
#include <iostream>		//Input and output
#include <cctype>		//char classifications
#include <fstream>		//File streams
#include <cstdio>		//Standard output (printf)

using namespace std;		//Declare the std namespace

int main ()	{
	ifstream fsInputFile;			//declare input file stream
	ofstream fsOutputFile;			//declare output file stream
	const int MAX_SIZE (31);		//define maximum filename size
	bool bFileFound;			//bool to check if the file exists
	char cFileName[MAX_SIZE];		//declare input file name
	do {
		//prompt for input
		printf ("Please enter the name of the file (less than %i characters):\n",MAX_SIZE - 1);
		cin.getline (cFileName,MAX_SIZE);	//store input as the file name
		fsInputFile.open (cFileName);		//open the file
		bFileFound = fsInputFile.fail();	//check if the file can be open, result as bFileFound
		fsInputFile.close ();			//close the file
	} while (bFileFound);				//conditional to repeat as long as file can't be opened
	printf ("You entered: %s\n", cFileName);	//output the name of the file entered

	return 0;		//exit returning no errors
} 				//close main function 


The output is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ ./lab2-cstrings 
Please enter the name of the file (less than 30 characters):
0123456789012345678901234567890123456789
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
There was an error opening the file.
Please enter the name of the file (less than 30 characters):
...


Any suggestions or ideas of where I'm going wrong would be appreciated. I'm not opposed to going a different direction for verification and limiting the length. Thanks!
Function

cin.getline (cFileName,MAX_SIZE);

leaves the new line character in the input buffer. So next time it reads all characters before the new line character that is it reads nothing. You should remove the new line chharacter from the input buffer after each using of cin.getline
Last edited on
Topic archived. No new replies allowed.