473,770 Members | 6,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recognize enter to determine a default filename

beacon
579 Contributor
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.tx t" 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 1729
Ganon11
3,652 Recognized Expert Specialist
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 Contributor
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 Recognized Expert Specialist
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
3026
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 config file as a string. Because the object does not know the application in which it is being instantiated and because it's constructor is private, I need to know how to determine the name of the default config file (i.e.,...
2
8100
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 logic I have for the button. In other words, I would like to have a default button for the form, is this making sense? I thought I saw on Fritz Onion's MSDN webcasts there is a way to assign a default button to a form/page, is this in fact...
5
3136
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 click event will fire and the page will submit. I have a series of pages with Previous and Next navigational Btns. The Previous button is the first button, so when the user hits enter, the previous page is served up. Enter should result in...
11
7764
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 the web server performs a database query and displays the results. All of this works fine. I want the user to be able to press the Enter key while the cursor is still in SearchTextBox and have the SearchButton.Click event fire (thus performing...
9
6576
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 name was the first name on the list id the default documents in the IIS control panel. IIS doesn't recognize Default.aspx as the default page for the IIS root web. It will open the page if I type http://localhost/Default.aspx, and if I create an...
3
11632
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 be working fine when I test it with Hyperterminal . I am using the example program that comes with pyserial, as below. --------------- import serial
5
1597
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 console on Monday I saw what looked like a Password Cracking software running with the name at the top of the window E-Security. It looks like it had gone through 69,914,496 permutations already. I went into Task Manager and killed a program I did...
0
1269
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 value sits between the min and max values I specify. However, if the user enters a valid number within the specified range, but enters it in scientific notation, the RangeValidator doesn't recognize it as a valid number.
3
2546
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 program, but I do not know how I would find the default handler to a file type. Any help is greatly appreciated. Code to launch application: ProcessStartInfo pInfo = new ProcessStartInfo();
0
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.