Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 25th, 2007, 07:26 PM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Bourne
Default Simple (noob) Question

I am new to C++ (as is evident by this simple quewstion), but if anybody could take the time to help..

I am trying to get a simple program to write user-specified contents to a file. I have the basics working, but it will cut anything off from the content after a space is entered. Here is the code I have. (I realize this is probably not the best way to go about doing this, but I'm trying to keep it simple, as I am still very new to C++ programming.

Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string content;
int main () {

    cout << "Enter your content: ";
    cin >> content;

  ofstream myfile;
  myfile.open ("example.txt");
  myfile << content; //this does not allow spaces; only the first set of chars before the space will be included
  myfile.close();
  return 0;
}
When the program is ran, anything after a space in the entered content is cut off, and is not written to the file.

Any help would be appriciated.

Reply With Quote
  #2 (permalink)  
Old February 26th, 2007, 10:07 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Fort Wayne, Indiana, USA.
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

considering that you are new to C++ I will give you a simple fix to take care of this otherwise you will have to go back and redo some things which you may not understand and will learn later on as you progress through the learning stages. When you use cin it stops accepting input at white spaces which are tabs spaces or enters. to accept these just use the getline function to accept the whole line and it will work perfect.

~ Geo

~ Don't take life too seriously, you'll never get out alive!
Reply With Quote
  #3 (permalink)  
Old February 26th, 2007, 10:08 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Fort Wayne, Indiana, USA.
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

HERE IS YOUR CODE :

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
     string content;

     cout << "Enter your content: ";
     getline(cin, content);

     ofstream myfile;
     myfile.open ("example.txt");
     myfile << content;
     myfile.close();
     return 0;
}

~ Geo

~ Don't take life too seriously, you'll never get out alive!
Reply With Quote
  #4 (permalink)  
Old February 26th, 2007, 10:09 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Fort Wayne, Indiana, USA.
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

See it's the same exact thing except one line is a little different.

Good Work keep it up =P

~ Geo

~ Don't take life too seriously, you'll never get out alive!
Reply With Quote
  #5 (permalink)  
Old February 26th, 2007, 11:44 AM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Bourne
Default

Ok thank you for that. Works now.
I appriciate the help.
Reply With Quote
  #6 (permalink)  
Old February 27th, 2007, 12:35 AM
Registered User
 
Join Date: Feb 2007
Location: , , India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Let usa help each other and make this forum a great success
Reply With Quote
  #7 (permalink)  
Old March 6th, 2007, 10:07 AM
Registered User
 
Join Date: Mar 2007
Location: , , India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
You have a very cool Forum here…loved the content.
I have a question..
- “Seeking experienced programmer with following skills: strong browser UI background, specifically in JavaScript, AJAX, CSS, HTML. Also must have good system & software design skills, e.g. OOP. C++/C knowledge a plus”

I came across this site called Myndnet.com where u get paid if u answer this question.
It’s a platform for people to buy and sell Technology related information. Good money for people like us in the IT domain.
Here the link http://www.myndnet.com/login.jsp?ref...83&channel=A32
Sign up is free…check it out…
You can contact me at my id here for more questions : barot.alpa@gmail.com

Cheers:)
Alpa


Reply With Quote




Similar Threads
Thread Thread Starter Forum Replies Last Post
noob question mattl Java Basics 1 August 27th, 2007 11:28 PM
total noob xslt question mrscott XSLT 3 February 22nd, 2006 04:35 PM
Noob XML question Skido XML 0 December 23rd, 2005 04:25 PM
Noob needs help with XSL syntax question lancia12 XSLT 1 October 1st, 2004 08:45 AM
Noob Question jverdin BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 3 April 2nd, 2004 10:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.