Thread: newbie having problem with getline

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    6

    newbie having problem with getline

    Trying to read in a comma delimited text file and parse it into a string array xquestions [10][6].
    Here's 2 lines of my text file...
    line1- "Where is it found that Moses parted the Red Sea?,Joh 3:16,Gen 1,Exo 20,1Sa 17,Exo 14,Exo 14,"

    line2- "Where is it found that Moses parted the Red Sea?,Joh 3:16,Gen 1,Exo 20,1Sa 17,Exo 14,Exo 14,Where is it found that David fought Goliath?,1Sa 17,1Ki 10,1Ki 20,1Sa 17,1Sa 10,1Sa 17,"

    My code is....
    Code:
        ifstream xfile;
        xfile.open(fileName);
        if(!xfile.is_open()){
            cout << "file failed to open" << endl;
        }
        xstring = "x";
        int xrow=0;
        int xcol=0;
        while(!xfile.eof()){
            for(xcol=0; xcol<=6; xcol++){
                getline(xfile, xstring, ',');
                if(xstring == " "){
                    break;
                }
                xquestions[xrow][xcol] = xstring;
            }
            xrow++;
    But... after it runs and builds the array, the array[x][6] is filled with the first comma block of the next line.
    I have tried and tried... but for the life of me, I cannot see where I fat fingered the code.
    Could someone help me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    file.eof() doesn't do what you think it does.
    It is a state, not a prediction.

    If you had a 10 character file and read exactly 10 characters, eof() would still be false.
    You would need to try (and fail) to read the 11th character for eof() to become true.

    You need to test file input operations for success.
    Code:
    const int maxrow = 10, maxcol = 6;
    // read lines from the file until
    // - the max number of expected rows is reached
    // - there are no more lines to read.
    for ( xrow = 0 ; xrow < 10 && getline(xfile,line) ; xrow++ ) {
      // input from a string instead of a file.
      // https://en.cppreference.com/w/cpp/header/sstream
      istringstream is(line);
      for ( xcol = 0 ; xcol < maxcol && getline(is,xstring,',') ; xcol++ ) {
        xquestions[xrow][xcol] = xstring;
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Another problem is that you say your array is declared as xquestions[10][6] and then you say you are accessing xquestions[x][6], which is the 7th element. So presumably it should be declared as xquestions[10][7].

    Also, your file structure is confusing.
    Why is the first line also part of the second line?
    Why are the lines contained in double quotes?
    And there usually isn't a comma after the last item of a line.

    Also, why are you testing if xstring is a single space?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Aug 2022
    Posts
    6
    I was hesitate to post my code snippet simply because of me being so new to C++ and "unlearned" in it. I actually started c++ 2 weeks ago... and as you can see, got stuck.
    I thank both of you for answering... I will study your questions and suggestions...
    Thanks again!

  5. #5
    Registered User
    Join Date
    Aug 2022
    Posts
    6
    Update: I got my program working now. Well at least that part... lol. As to the extra commas in the file, I did that to try to get it to work... but John.c ... you are right I did not need them. And I took out the double quotes.
    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using getLine()
    By sigur47 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2012, 12:09 PM
  2. Problem with using cin.getline() more than once
    By aamirbwppk in forum C++ Programming
    Replies: 4
    Last Post: 10-27-2009, 08:45 AM
  3. getline problem
    By elwad in forum C Programming
    Replies: 2
    Last Post: 04-21-2009, 05:51 PM
  4. problem with getline()
    By vin_pll in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2008, 05:07 AM
  5. cin.getline problem
    By Ivan! in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2003, 09:23 AM

Tags for this Thread