September 24th, 2010, 03:25 PM
Whats wrong with my strings?
I have a high-school teacher who gave us a list of 10 questions. And each week he gives us another book to read and tells us to answer those 10 questions in a typed report. So I made a C++ program with the 10 questions pre programmed in, so the program would ask me the question, and I would type in each answer. And at the end it would output every question with each answer and I would copy and paste it into word. But I'm having a problem. (I don't want you to think that I want you to write a program for me, because I already have all the code finished, I just have one problem:
So I declare the variables that will hold the answer to each question:
Then the program prompts for input:
cout << "What is your name?" << endl;
cin >> sQuestion1;
cout << "What is the name of the book?" << endl;
cin >> sQuestion2;
cout << "Who is the author of the book?" << endl;
cin >> sQuestion3;
I compile the code and run the program just fine. But if one of my inputs to the question has a space in it, the program will skip the next step:
For example, this is copy pasted directly from the program:
What is your name?
What is the name of the book?
Who is the author of the book?
it doesn't let me type for "What is the name of the book?". It just skips right to the next one.
What can I do to fix this?
September 24th, 2010, 03:29 PM
Re: Whats wrong with my strings?
First of all, using a char for your answers is unduly limiting. Why not use a std::string which can be of any length, instead?
Second, the normal behavior of operator>> is to stop at any whitespace. If you wish to delimit your answers some other way, such as with a newline, then you should use getline() instead.
Tags for this Thread
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!