My program is not reading in all of the .txt file











up vote
0
down vote

favorite












I am trying to read a .txt file into an object and then store it in a linked list but it only will read in half of the file.



enter image description here



This is what I'm calling to try and read it in but it only reads up to Dodge Demon.



 while (CarsFile >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile)
{

car.setMake(make);
car.setModel(model);
car.setPrice(price);
car.setYear(year);
car.setHorsePower(horsePower);
car.setTorque(torque);
car.setZeroToSixty(zeroToSixty);
car.setWeight(weight);
car.setQuarterMile(quarterMile);

list.appendNode(car);

}









share|improve this question
























  • I misread the file. No spaces in names, but commas in the numbers are a problem.
    – Johnny Mopp
    Nov 7 at 21:53












  • I changed all the numbers with commas to ints but I'm still having problems
    – Jakekone23
    Nov 7 at 22:28










  • Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
    – Johnny Mopp
    Nov 7 at 22:30

















up vote
0
down vote

favorite












I am trying to read a .txt file into an object and then store it in a linked list but it only will read in half of the file.



enter image description here



This is what I'm calling to try and read it in but it only reads up to Dodge Demon.



 while (CarsFile >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile)
{

car.setMake(make);
car.setModel(model);
car.setPrice(price);
car.setYear(year);
car.setHorsePower(horsePower);
car.setTorque(torque);
car.setZeroToSixty(zeroToSixty);
car.setWeight(weight);
car.setQuarterMile(quarterMile);

list.appendNode(car);

}









share|improve this question
























  • I misread the file. No spaces in names, but commas in the numbers are a problem.
    – Johnny Mopp
    Nov 7 at 21:53












  • I changed all the numbers with commas to ints but I'm still having problems
    – Jakekone23
    Nov 7 at 22:28










  • Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
    – Johnny Mopp
    Nov 7 at 22:30















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to read a .txt file into an object and then store it in a linked list but it only will read in half of the file.



enter image description here



This is what I'm calling to try and read it in but it only reads up to Dodge Demon.



 while (CarsFile >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile)
{

car.setMake(make);
car.setModel(model);
car.setPrice(price);
car.setYear(year);
car.setHorsePower(horsePower);
car.setTorque(torque);
car.setZeroToSixty(zeroToSixty);
car.setWeight(weight);
car.setQuarterMile(quarterMile);

list.appendNode(car);

}









share|improve this question















I am trying to read a .txt file into an object and then store it in a linked list but it only will read in half of the file.



enter image description here



This is what I'm calling to try and read it in but it only reads up to Dodge Demon.



 while (CarsFile >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile)
{

car.setMake(make);
car.setModel(model);
car.setPrice(price);
car.setYear(year);
car.setHorsePower(horsePower);
car.setTorque(torque);
car.setZeroToSixty(zeroToSixty);
car.setWeight(weight);
car.setQuarterMile(quarterMile);

list.appendNode(car);

}






c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 21:54









Johnny Mopp

6,71222344




6,71222344










asked Nov 7 at 21:52









Jakekone23

11




11












  • I misread the file. No spaces in names, but commas in the numbers are a problem.
    – Johnny Mopp
    Nov 7 at 21:53












  • I changed all the numbers with commas to ints but I'm still having problems
    – Jakekone23
    Nov 7 at 22:28










  • Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
    – Johnny Mopp
    Nov 7 at 22:30




















  • I misread the file. No spaces in names, but commas in the numbers are a problem.
    – Johnny Mopp
    Nov 7 at 21:53












  • I changed all the numbers with commas to ints but I'm still having problems
    – Jakekone23
    Nov 7 at 22:28










  • Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
    – Johnny Mopp
    Nov 7 at 22:30


















I misread the file. No spaces in names, but commas in the numbers are a problem.
– Johnny Mopp
Nov 7 at 21:53






I misread the file. No spaces in names, but commas in the numbers are a problem.
– Johnny Mopp
Nov 7 at 21:53














I changed all the numbers with commas to ints but I'm still having problems
– Jakekone23
Nov 7 at 22:28




I changed all the numbers with commas to ints but I'm still having problems
– Jakekone23
Nov 7 at 22:28












Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
– Johnny Mopp
Nov 7 at 22:30






Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
– Johnny Mopp
Nov 7 at 22:30














2 Answers
2






active

oldest

votes

















up vote
0
down vote













The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream to parse.



std::string line;
while (std::getline(CarsFile, line)) {
// Remove commas
line.erase(std::remove(line.begin(), line.end(), ','), line.end());
// Use stringstream to parse
std::stringstream ss(line);
if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
// Add to list....
}
}





share|improve this answer





















  • I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
    – Thomas Matthews
    Nov 7 at 22:11












  • @ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
    – Johnny Mopp
    Nov 7 at 22:13










  • For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
    – Thomas Matthews
    Nov 7 at 22:16












  • @ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
    – Johnny Mopp
    Nov 7 at 22:17




















up vote
0
down vote













You may want to use std::getline to read the text line, then std::istringstream to parse the text:



std::string  record;
while (std::getline(CarsFile, record))
{
std::istringstream car_stream(record);
std::string make;
std::getline(car_stream, make, ',');
//...
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198396%2fmy-program-is-not-reading-in-all-of-the-txt-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream to parse.



    std::string line;
    while (std::getline(CarsFile, line)) {
    // Remove commas
    line.erase(std::remove(line.begin(), line.end(), ','), line.end());
    // Use stringstream to parse
    std::stringstream ss(line);
    if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
    // Add to list....
    }
    }





    share|improve this answer





















    • I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
      – Thomas Matthews
      Nov 7 at 22:11












    • @ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
      – Johnny Mopp
      Nov 7 at 22:13










    • For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
      – Thomas Matthews
      Nov 7 at 22:16












    • @ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
      – Johnny Mopp
      Nov 7 at 22:17

















    up vote
    0
    down vote













    The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream to parse.



    std::string line;
    while (std::getline(CarsFile, line)) {
    // Remove commas
    line.erase(std::remove(line.begin(), line.end(), ','), line.end());
    // Use stringstream to parse
    std::stringstream ss(line);
    if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
    // Add to list....
    }
    }





    share|improve this answer





















    • I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
      – Thomas Matthews
      Nov 7 at 22:11












    • @ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
      – Johnny Mopp
      Nov 7 at 22:13










    • For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
      – Thomas Matthews
      Nov 7 at 22:16












    • @ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
      – Johnny Mopp
      Nov 7 at 22:17















    up vote
    0
    down vote










    up vote
    0
    down vote









    The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream to parse.



    std::string line;
    while (std::getline(CarsFile, line)) {
    // Remove commas
    line.erase(std::remove(line.begin(), line.end(), ','), line.end());
    // Use stringstream to parse
    std::stringstream ss(line);
    if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
    // Add to list....
    }
    }





    share|improve this answer












    The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream to parse.



    std::string line;
    while (std::getline(CarsFile, line)) {
    // Remove commas
    line.erase(std::remove(line.begin(), line.end(), ','), line.end());
    // Use stringstream to parse
    std::stringstream ss(line);
    if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
    // Add to list....
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 7 at 22:10









    Johnny Mopp

    6,71222344




    6,71222344












    • I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
      – Thomas Matthews
      Nov 7 at 22:11












    • @ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
      – Johnny Mopp
      Nov 7 at 22:13










    • For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
      – Thomas Matthews
      Nov 7 at 22:16












    • @ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
      – Johnny Mopp
      Nov 7 at 22:17




















    • I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
      – Thomas Matthews
      Nov 7 at 22:11












    • @ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
      – Johnny Mopp
      Nov 7 at 22:13










    • For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
      – Thomas Matthews
      Nov 7 at 22:16












    • @ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
      – Johnny Mopp
      Nov 7 at 22:17


















    I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
    – Thomas Matthews
    Nov 7 at 22:11






    I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
    – Thomas Matthews
    Nov 7 at 22:11














    @ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
    – Johnny Mopp
    Nov 7 at 22:13




    @ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
    – Johnny Mopp
    Nov 7 at 22:13












    For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
    – Thomas Matthews
    Nov 7 at 22:16






    For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
    – Thomas Matthews
    Nov 7 at 22:16














    @ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
    – Johnny Mopp
    Nov 7 at 22:17






    @ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
    – Johnny Mopp
    Nov 7 at 22:17














    up vote
    0
    down vote













    You may want to use std::getline to read the text line, then std::istringstream to parse the text:



    std::string  record;
    while (std::getline(CarsFile, record))
    {
    std::istringstream car_stream(record);
    std::string make;
    std::getline(car_stream, make, ',');
    //...
    }





    share|improve this answer

























      up vote
      0
      down vote













      You may want to use std::getline to read the text line, then std::istringstream to parse the text:



      std::string  record;
      while (std::getline(CarsFile, record))
      {
      std::istringstream car_stream(record);
      std::string make;
      std::getline(car_stream, make, ',');
      //...
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You may want to use std::getline to read the text line, then std::istringstream to parse the text:



        std::string  record;
        while (std::getline(CarsFile, record))
        {
        std::istringstream car_stream(record);
        std::string make;
        std::getline(car_stream, make, ',');
        //...
        }





        share|improve this answer












        You may want to use std::getline to read the text line, then std::istringstream to parse the text:



        std::string  record;
        while (std::getline(CarsFile, record))
        {
        std::istringstream car_stream(record);
        std::string make;
        std::getline(car_stream, make, ',');
        //...
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 22:10









        Thomas Matthews

        43.9k1168120




        43.9k1168120






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198396%2fmy-program-is-not-reading-in-all-of-the-txt-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini