CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Sep 2010

    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:

    char sQuestion1[256];
    char sQuestion2[256];
    char sQuestion3[256];

    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?
    John Smith
    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?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Seattle, WA

    Re: Whats wrong with my strings?

    First of all, using a char[255] 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.