473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Recognize enter to determine a default filename

beacon
579 512MB
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...
Oct 9 '07 #1
3 1705
Ganon11
3,652 Expert 2GB
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
Oct 9 '07 #2
beacon
579 512MB
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
Oct 9 '07 #3
Ganon11
3,652 Expert 2GB
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.
Oct 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: nfr | last post by:
I have a Singleton Model object that can be instantiated by different applications at runtime. This object activates a Channel using the config file in its constructor, which needs the name of the...
2
by: Flip | last post by:
On my current form, when I hit the enter key in a texbox, the form appears to resubmit itself, but it's not actually doing a submit (or so I think?). I would like to have the enter button fire the...
5
by: ewillyb | last post by:
Hi, ASP.NET has some interesting behavior when the user hits the Enter key. If there are multiple ASP:Buttons (rendered as HTML submits) on the form, when the user hits enter, the first button's...
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
9
by: David Veeneman | last post by:
I'm just getting started with ASP.NET, using VS 2005. As an exercise, I opened the root web site in VS 2005 and created a simple welcome page. I saved the page as Default.aspx and made sure that...
3
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to...
5
by: =?Utf-8?B?Sm9obiBLb3R1Ynk=?= | last post by:
Hi all, We lease a non-managed Web Server running AV software but no IDS. It is Windows 2003 STD which receives automatic nightly Windows Security patches at 3AM. When I logged into the RDP...
0
by: Andrew Han | last post by:
I have a simple textbox where a user can enter a numeric value. The RangeValidator works perfectly well to determine if the value is a number of whatever type I specify AND to determine if the...
3
by: LordHog | last post by:
Hello, How would I go about finding the default handler, let's say a text file (*.txt), then launch the default handler with the file as an argument? I had found how to launch an external...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.