c++ strings

Dnsgm

Baseband Member
Messages
61
I'm having some troubles with some strings in c++. heres my code:

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

int main ()
{
    string work;
    cin >> work;
    cout << work;
    ofstream myfile;
    myfile.open ("example.html");
    myfile << "<html>\n";
    myfile << "<head>\n";
    myfile << "<title>";
    myfile << "hello";
    myfile << "</title>\n";
    myfile << "</head>\n";
    
    myfile << "<body>\n";
    myfile << "<font size=45>";
    myfile << work;
    myfile << "</font>\n";
    
    
   
    myfile.close();
    system ("pause");
    return 0;
}


I want to output a sentence but it only outputs the first word, so i need some help their.

Thanks.
 
Okay, I think I see a couple of things that you may or may not be having trouble with in your code. First, I'm not sure why you got away without the statement
"#include <string>" in your code. I'd definitely change that first. Then, to be sure that I get what I need for file manipulations forget about the
"#include <ofstream>" or "#include <ifstream>" thing. The best way to do that is to use the easier and all inclusive "#include <fstream>". It does both and won't add pounds to a program unneccessarily;)

You then have several options to correct the errant output / input with a string. I'll direct you first to the favorite C and C++ reference URL,
http://www.cppreference.com/

It can be used for almost all your C / C++ lookup needs. And it is correct on all points and occasions that I've used it.

Next, I will direct you to a few specific pages that will add to your C++ I/O prowess from within that site. Don't get confused, there is more than one
possible solution and that is a very C / C++ thing in itself. Here you go:

http://www.cppreference.com/cppio/index.html

http://www.cppreference.com/cppstring/index.html

http://www.cppreference.com/cppsstream/index.html

http://www.cppreference.com/cppio/getline.html

http://www.cppreference.com/cppstring/getline.html

http://www.cppreference.com/cppsstream/operators.html

Okay, that isn't as much reading as it seems. Give it a look, especially the last three. If the light bulb doesn't go off, then repost what is happening after your next attempt and I'll show some functional code for it. Good luck and think about the string object. ;)
 
Back
Top Bottom