C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-16-2003, 03:09 AM   #1
Registered User
 
Join Date: Mar 2002
Posts: 12
Question Getting user input for filename loop

I have a program that prompts the user for an existing text file that they want my program to modify. Currently, when the user puts in a file that can't be found, the program displays an error message then quits. I would prefer to put the prompt in a while loop and while the file can't be found, prompt the user to try to input the file name again. I set up my loop but it doesn't work. THis is how I tried it:

Code:
cin>>input_file;

	fin.open(input_file);

	while (fin.fail())
	{
		fin.close();
		cout<<"Error opening input file. Did you input the wrong name or path?\n";
		cout<<"\nEnter the name of the text file: \n\n";
		cin>>input_file;

	fin.open(input_file);
	}
At first I didn't have the fin.close() at the top of the loop but added it when the loop was getting stuck in an infinite loop. I'm thinking that I might have to rename the file variable name each time, i.e.: input_file, input_file2, input_file3...

Is there a better way?
jpchand is offline   Reply With Quote
Old 09-16-2003, 06:37 AM   #2
twm
root
 
Join Date: Sep 2003
Posts: 232
>Is there a better way?
Code:
while (!cin.getline(filename) || !fin.open(filename)
  cerr<<File open failure, try again...\n";
twm is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some help with C program writing The_PC_Gamer C Programming 9 02-12-2008 09:12 PM
for loop with sort issues redmondtab C Programming 10 10-09-2006 10:36 AM
vectors and user input Chaplin27 C++ Programming 6 01-17-2005 10:23 AM
comparing user input lambs4 C Programming 5 12-15-2002 10:28 AM
Format User Input When using strings Unregistered C Programming 4 03-08-2002 03:59 AM


All times are GMT -6. The time now is 02:05 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22