Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have the code below:

C++
string str1;
	ofstream fp("file1.txt");
	if(fp.is_open()){
		cout<<"\n Enter first string ::: ";
		cin>>str1;
		fp<<str1<<"\n";		
	}	


It works but it doesn't save tho whole string which i type as the input.
for example, i type:

Hello world!

in the file it's written:

Hello

until the first blank space in the sentence it save to a file, after that, i don't know what happens, it just don't save them.

Why does it work like it?
Is it a problem with the string or with the ' fp<< ' ?

How should i fix it?

Thanks,
Posted

1 solution

Because the blank is a string separator for cin. You should use (with its caveats...) getline[^].
 
Share this answer
 
Comments
m.r.m.40 1-Aug-14 15:36pm    
getline(cin,str1);
It worked,
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900