How Do I read whole line, rather than just strings before Space?

This is a discussion on How Do I read whole line, rather than just strings before Space? within the C++ Programming forums, part of the General Programming Boards category; Brief history about me. Hey, Kind of Noob and started programming recently. My situation I have a Text file that ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up How Do I read whole line, rather than just strings before Space?

    Brief history about me.
    Hey, Kind of Noob and started programming recently.

    My situation
    I have a Text file that contain 7 questions and I want to read those questions from a text file, full lines not just Strings before SPACE.

    The questions are being stored in a struct called Question.

    Here is the Source Codes:

    My Struct

    Code:
    // Declaring and defining struct that has one member //
    
    struct Question
    {
        string Questiontxt;
    
    
    };
    
    
    Question questions[MAXQUES];
    Code:
    // Reading Questions //    
    
    ifstream quesf("questions.txt");
    
    
        while (!quesf.eof())
        {
            for (int i=0; i<MAXQUES; i++)
            {
                getline( quesf >> questions[i].Questiontxt));
            }
        }
    
    
        quesf.close();
    Actual code that writes first line:

    Code:
    Gwin.writeString(235,310,questions[2].Questiontxt);
    My Text File:

    Code:
    In the United States, how much is a dime worth?How many inches are in a foot?
    Michael Jordan professionally played which of these sports?
    Where does the sun set?
    What is the meaning of bogus?
    What is the state pension age for men?
    Where does Santa Claus come from?
    The result I am getting is ..:

    from?
    But I want it display full line of text. ie :
    In the United States, how much is a dime worth?How many inches are in a foot?
    Last edited by Swadesh; 04-14-2012 at 03:56 PM.

  2. #2
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Code:
    getline( quesf >> questions[i].Questiontxt));
    That shouldn't even compile. std::istream::operator >> returns an std::istream&, and std::getline doesn't take any such parameter. Plus you've got an extra closing parenthesis at the end there. Perhaps you meant:

    Code:
    getline(quesf, questions[i].Questiontxt);
    Also, read up on std::vector; it works like an array only it's resizable.
    Last edited by gardhr; 04-14-2012 at 06:38 PM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    ^ Hey, Thanks that seemed to grab the line..

    Thanks Again..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 09:55 PM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 09:51 AM
  3. Read strings line by line until Ctrl+C is pressed
    By r00t in forum C Programming
    Replies: 25
    Last Post: 11-17-2009, 03:18 AM
  4. Read single words seperated by white space in as strings
    By steals10304 in forum C Programming
    Replies: 2
    Last Post: 11-02-2009, 03:05 PM
  5. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21