Outputting strings to files

Hello everybody. I'm a beginner programmer who just joined this forum. I'm trying to write a program for which I need a component that allows the user to input a string with whitespaces and have the program output this to a history file using fprintf. I tested it with just printf so I could see how it went. I figured out how to output one word to an external file, but once I included white spaces, it stopped recording and just output one word. Below is a copy of the code I used followed by the output.

Input
---------------------------------------------------------------------------------
#include <string>
#include <iostream>
using namespace std;

int main()
{
std::string name;
cout<<"enter first and last name: ";
std::getline(std::cin, name);
cout<< name;
printf("\n\n%s\n\n", name);
}
---------------------------------------------------------------------------------

Output
---------------------------------------------------------------------------------
enter first and last name: nick czarnek
nick czarnek

(null)

Press any key to continue...
---------------------------------------------------------------------------------


Anybody have any idea how to get rid of the null and use printf to output the input so that I can send it to the output file with fprintf?

Thanks,
Nick
Printf's %s wants a char*, not an std::string I believe. Use .c_str() if you need a char*...although since you are using C++, you should use fstreams to write to files instead of fprintf().
I agree. Why are you using C output? And is getline part of the std namespace?
Topic archived. No new replies allowed.