Connecting Tech Pros Worldwide Forums | Help | Site Map

Recognize enter to determine a default filename

beacon's Avatar
Needs Regular Fix
 
Join Date: Aug 2007
Posts: 279
#1: Oct 9 '07
Hi,

I'm doing a homework assignment, but I have done most of the work and am only looking for some tutoring to get past this small portion of the program I have been tasked to write.

The program says that a filename of "numbers.txt" will be defaulted to if the user presses enter when prompted. I set this up as a string and had no problem getting the default value to work properly. My problem occurs when the user tries to create a file with an actual name. I try to infile.open, but it won't accept my variable I used to create the string.

I'm not sure if this makes sense, but hopefully my code will. Here goes nothing:
Expand|Select|Wrap|Line Numbers
  1. void fileName(){
  2.  
  3.     ifstream infile;
  4.  
  5.     cout<<"Please enter a name for your file: ";
  6.     string x;
  7.     getline(cin,x);
  8.  
  9.     if(x == "\0"){
  10.         cout<<"Your filename will be numbers.txt";
  11.         infile.open("nums.in");
  12.     }
  13.     else{
  14.         cout<<"Your filename will be "<<x<<endl<<endl;
  15.         infile.open();
  16.     }
  17. }
Where I have infile.open(), I haven't been able to find anything I can use to place inside the parenthesis. If anyone can help me out, I would greatly appreciate it.

Thanks...

Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Oct 9 '07

re: Recognize enter to determine a default filename


Yep, this is a little tricky. The .open function is looking for something called a CString, which is a string represented as an array of characters. strings are around to make our lives a lot easier, but they don't work with this function! Fortunately, the C++ makers (in all wisdom) gave us a function to bridge this gap.

Instead of putting x in the parentheses, put x.c_str(). c_str() is a function that returns the CString equivalent of the string.

This should fix your problem :D
beacon's Avatar
Needs Regular Fix
 
Join Date: Aug 2007
Posts: 279
#3: Oct 9 '07

re: Recognize enter to determine a default filename


Quote:

Originally Posted by Ganon11

Yep, this is a little tricky. The .open function is looking for something called a CString, which is a string represented as an array of characters. strings are around to make our lives a lot easier, but they don't work with this function! Fortunately, the C++ makers (in all wisdom) gave us a function to bridge this gap.

Instead of putting x in the parentheses, put x.c_str(). c_str() is a function that returns the CString equivalent of the string.

This should fix your problem :D

It looks like it fixed it. It certainly didn't return an error.

One of my gripes with my Computer Science Program is that we haven't covered I/O streams and different ways to utilize them. Because of that, when my professor asks something like this of us, it feels like such an ordeal to get such an easy answer.

Have any suggestions for learning more on the subject?

Thanks again for the help Ganon
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#4: Oct 9 '07

re: Recognize enter to determine a default filename


Quote:

Originally Posted by beacon

It looks like it fixed it. It certainly didn't return an error.

One of my gripes with my Computer Science Program is that we haven't covered I/O streams and different ways to utilize them. Because of that, when my professor asks something like this of us, it feels like such an ordeal to get such an easy answer.

Have any suggestions for learning more on the subject?

Thanks again for the help Ganon

Practice?

Are you using a textbook? if so, see if there's a chapter on file i/o and read up on it.
Reply