hi every one, I am making KBC game in C++. To store questions and its options in a file, I am unable to write successfully the character string of question in the file due to spaces between question. So,can anyone tell me how to write a character string having spaces to a file and how it can be read from the file in a character array?
Regards....

Member Avatar for iamthwee

I think you need getline()

getline() from iostream will do the reading and you should use ofstream to do the writing. Give this a shot. If it doesn't work, post a < 10 line demonstration of the problem.

Dave

Thanx for your help, it worked. But one problem is that i was using a while loop with cin as a condition. this loop keeps on taking input from user unless user enters '^Z'. now using cin.getline, the loop is not terminating by entering '^Z'. What should i do with this loop?
here is the fragmant of that loop

char Q[50];
	cout<<"enter the question";
	while(cin.getline(Q,50,'\n'))
	{

		fout<<Q;

	}

regards...

Member Avatar for iamthwee

Have a do while...

I mean why restrict it to CTRL Z?

You could have a 'do you wish to continue (y/n)'

If answer = n then terminate the loop.

I don't know much about c++. but I am excite to get more knowledge of c++. It's very useful language. It uses many where. All the coding is very easy to implement so I am thankful to share the a part of coding for KBC game.

Yes u are right. But i thought it seemed uncomfortable for the person entering data to select yes or no every time he/she enters a question. Butr now I have done it with do-while as you recommended. Thanks a lot for your help! My problem is solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.