#include <iostream> //Input and output
#include <cctype> //char classifications
#include <fstream> //File streams
#include <cstdio> //Standard output (printf)
usingnamespace std; //Declare the std namespace
int main () {
ifstream fsInputFile; //declare input file stream
ofstream fsOutputFile; //declare output file stream
constint 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
$ ./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!
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