Text menu to browse CSV data about seal species

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
8
down vote

favorite
1












I am writing in C++, I have completed a small program which parses CSV files from IUCN and presents the species data using menus. I used a lot of loops. It works, however it is buggy when processing certain input errors.



I would like to know if there are better ways to write this program. Is there a way to use OOP? Would it be helpful to use OOP?



//EndangeredSeals.cpp
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

int main()

std::string entry;
std::vector<std::vector<std::string> > database;
std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");

if (file)
while (std::getline(file, entry))
size_t dbsize = database.size();
database.resize(dbsize + 1);
std::istringstream ss(entry);
std::string field, push_field("");
bool no_quotes = true;

while (std::getline(ss, field, ','))
if (static_cast<size_t>(std::count(field.begin(), field.end(), '"')) % 2 != 0)
no_quotes = !no_quotes;
field.erase(remove(field.begin(), field.end(), '"'), field.end());


push_field += field + (no_quotes ? "" : ",");

if (no_quotes)
database[dbsize].push_back(push_field);
push_field.resize(0);




int dbval = static_cast<int>(database.size());
int entryval = static_cast<int>(database[0].size());
int sealChoice;
int dataChoice;
int switchChoice;
bool moreData;
bool moreSeal = true;
while (moreSeal == true)
return 0;







share|improve this question



























    up vote
    8
    down vote

    favorite
    1












    I am writing in C++, I have completed a small program which parses CSV files from IUCN and presents the species data using menus. I used a lot of loops. It works, however it is buggy when processing certain input errors.



    I would like to know if there are better ways to write this program. Is there a way to use OOP? Would it be helpful to use OOP?



    //EndangeredSeals.cpp
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <algorithm>

    int main()

    std::string entry;
    std::vector<std::vector<std::string> > database;
    std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");

    if (file)
    while (std::getline(file, entry))
    size_t dbsize = database.size();
    database.resize(dbsize + 1);
    std::istringstream ss(entry);
    std::string field, push_field("");
    bool no_quotes = true;

    while (std::getline(ss, field, ','))
    if (static_cast<size_t>(std::count(field.begin(), field.end(), '"')) % 2 != 0)
    no_quotes = !no_quotes;
    field.erase(remove(field.begin(), field.end(), '"'), field.end());


    push_field += field + (no_quotes ? "" : ",");

    if (no_quotes)
    database[dbsize].push_back(push_field);
    push_field.resize(0);




    int dbval = static_cast<int>(database.size());
    int entryval = static_cast<int>(database[0].size());
    int sealChoice;
    int dataChoice;
    int switchChoice;
    bool moreData;
    bool moreSeal = true;
    while (moreSeal == true)
    return 0;







    share|improve this question























      up vote
      8
      down vote

      favorite
      1









      up vote
      8
      down vote

      favorite
      1






      1





      I am writing in C++, I have completed a small program which parses CSV files from IUCN and presents the species data using menus. I used a lot of loops. It works, however it is buggy when processing certain input errors.



      I would like to know if there are better ways to write this program. Is there a way to use OOP? Would it be helpful to use OOP?



      //EndangeredSeals.cpp
      #include <iostream>
      #include <sstream>
      #include <fstream>
      #include <string>
      #include <vector>
      #include <algorithm>

      int main()

      std::string entry;
      std::vector<std::vector<std::string> > database;
      std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");

      if (file)
      while (std::getline(file, entry))
      size_t dbsize = database.size();
      database.resize(dbsize + 1);
      std::istringstream ss(entry);
      std::string field, push_field("");
      bool no_quotes = true;

      while (std::getline(ss, field, ','))
      if (static_cast<size_t>(std::count(field.begin(), field.end(), '"')) % 2 != 0)
      no_quotes = !no_quotes;
      field.erase(remove(field.begin(), field.end(), '"'), field.end());


      push_field += field + (no_quotes ? "" : ",");

      if (no_quotes)
      database[dbsize].push_back(push_field);
      push_field.resize(0);




      int dbval = static_cast<int>(database.size());
      int entryval = static_cast<int>(database[0].size());
      int sealChoice;
      int dataChoice;
      int switchChoice;
      bool moreData;
      bool moreSeal = true;
      while (moreSeal == true)
      return 0;







      share|improve this question













      I am writing in C++, I have completed a small program which parses CSV files from IUCN and presents the species data using menus. I used a lot of loops. It works, however it is buggy when processing certain input errors.



      I would like to know if there are better ways to write this program. Is there a way to use OOP? Would it be helpful to use OOP?



      //EndangeredSeals.cpp
      #include <iostream>
      #include <sstream>
      #include <fstream>
      #include <string>
      #include <vector>
      #include <algorithm>

      int main()

      std::string entry;
      std::vector<std::vector<std::string> > database;
      std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");

      if (file)
      while (std::getline(file, entry))
      size_t dbsize = database.size();
      database.resize(dbsize + 1);
      std::istringstream ss(entry);
      std::string field, push_field("");
      bool no_quotes = true;

      while (std::getline(ss, field, ','))
      if (static_cast<size_t>(std::count(field.begin(), field.end(), '"')) % 2 != 0)
      no_quotes = !no_quotes;
      field.erase(remove(field.begin(), field.end(), '"'), field.end());


      push_field += field + (no_quotes ? "" : ",");

      if (no_quotes)
      database[dbsize].push_back(push_field);
      push_field.resize(0);




      int dbval = static_cast<int>(database.size());
      int entryval = static_cast<int>(database[0].size());
      int sealChoice;
      int dataChoice;
      int switchChoice;
      bool moreData;
      bool moreSeal = true;
      while (moreSeal == true)
      return 0;









      share|improve this question












      share|improve this question




      share|improve this question








      edited Aug 4 at 15:15









      200_success

      123k14143401




      123k14143401









      asked Jan 5 at 12:18









      user157343

      411




      411




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote













          Reformat code



          Try to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.



          Create small functions



          Right now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.



          Hardcoding values



          You have code like this



          std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");



          This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.



          Next examples of hardcoding values



          std::cout << database[sealChoice][14] << std::endl;
          while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())


          Try to give these every number meaningful name, it will much more easier for readers to understand.



          Strange code



          I cannot get why do you do this



           size_t dbsize = database.size();
          database.resize(dbsize + 1);


          Personally, I use resize in only one case.
          Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size.
          You are doing something that will be done better and automatically (no need to write code like this).



          Next



          int dbval = static_cast<int>(database.size());
          int entryval = static_cast<int>(database[0].size());


          I know that works, but I would go with simply



          auto dbval = database.size();
          auto entryval = database[0].size();





          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184359%2ftext-menu-to-browse-csv-data-about-seal-species%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            Reformat code



            Try to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.



            Create small functions



            Right now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.



            Hardcoding values



            You have code like this



            std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");



            This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.



            Next examples of hardcoding values



            std::cout << database[sealChoice][14] << std::endl;
            while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())


            Try to give these every number meaningful name, it will much more easier for readers to understand.



            Strange code



            I cannot get why do you do this



             size_t dbsize = database.size();
            database.resize(dbsize + 1);


            Personally, I use resize in only one case.
            Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size.
            You are doing something that will be done better and automatically (no need to write code like this).



            Next



            int dbval = static_cast<int>(database.size());
            int entryval = static_cast<int>(database[0].size());


            I know that works, but I would go with simply



            auto dbval = database.size();
            auto entryval = database[0].size();





            share|improve this answer

























              up vote
              5
              down vote













              Reformat code



              Try to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.



              Create small functions



              Right now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.



              Hardcoding values



              You have code like this



              std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");



              This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.



              Next examples of hardcoding values



              std::cout << database[sealChoice][14] << std::endl;
              while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())


              Try to give these every number meaningful name, it will much more easier for readers to understand.



              Strange code



              I cannot get why do you do this



               size_t dbsize = database.size();
              database.resize(dbsize + 1);


              Personally, I use resize in only one case.
              Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size.
              You are doing something that will be done better and automatically (no need to write code like this).



              Next



              int dbval = static_cast<int>(database.size());
              int entryval = static_cast<int>(database[0].size());


              I know that works, but I would go with simply



              auto dbval = database.size();
              auto entryval = database[0].size();





              share|improve this answer























                up vote
                5
                down vote










                up vote
                5
                down vote









                Reformat code



                Try to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.



                Create small functions



                Right now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.



                Hardcoding values



                You have code like this



                std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");



                This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.



                Next examples of hardcoding values



                std::cout << database[sealChoice][14] << std::endl;
                while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())


                Try to give these every number meaningful name, it will much more easier for readers to understand.



                Strange code



                I cannot get why do you do this



                 size_t dbsize = database.size();
                database.resize(dbsize + 1);


                Personally, I use resize in only one case.
                Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size.
                You are doing something that will be done better and automatically (no need to write code like this).



                Next



                int dbval = static_cast<int>(database.size());
                int entryval = static_cast<int>(database[0].size());


                I know that works, but I would go with simply



                auto dbval = database.size();
                auto entryval = database[0].size();





                share|improve this answer













                Reformat code



                Try to use IDE options where you can reformat code, right now it is hard to read. There is everything in one place.



                Create small functions



                Right now, your code is a one big function. Firstly, divide your code into small functions that are doing one thing. You could end up with 10 or 15 really small functions. After that, thing about functions that are similar to each other, they are part of one bigger responsibility. You might be able to see some "objects" and create appropriate classes.



                Hardcoding values



                You have code like this



                std::ifstream file("C:\Users\MyPC\Desktop\Programs\Seals.csv");



                This can be done for example using command line arguments. You will be passing path to file when you start running your program, that will be much more flexible.



                Next examples of hardcoding values



                std::cout << database[sealChoice][14] << std::endl;
                while (switchChoice <= 0 || switchChoice >= 4 || std::cin.fail())


                Try to give these every number meaningful name, it will much more easier for readers to understand.



                Strange code



                I cannot get why do you do this



                 size_t dbsize = database.size();
                database.resize(dbsize + 1);


                Personally, I use resize in only one case.
                Suppose that I know that my container will be filled with 10 numbers, so before starting pushing into the vector I resize to the mentioned size.
                You are doing something that will be done better and automatically (no need to write code like this).



                Next



                int dbval = static_cast<int>(database.size());
                int entryval = static_cast<int>(database[0].size());


                I know that works, but I would go with simply



                auto dbval = database.size();
                auto entryval = database[0].size();






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Aug 4 at 8:28









                Newbie

                478314




                478314






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184359%2ftext-menu-to-browse-csv-data-about-seal-species%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation