mardi 5 juillet 2022

How to avoid multiple if-else statement while reading a file?

I am trying to change multiple if-else statement in my program with either switch and enum or some other object-oriented approach. My purpose is in my code there should not be if-else or very less.

class myClass
{
   void readFile();
}  

void myClass :: readFile()
{
     std::string lineByLine;
     std::ifstream myfile;
     myfile.open(file path);
     if (myfile.is_open()) 
     {
        while (std::getline(myfile, lineByLine)) 
       {
           std::pair<std::string, std::string> p1 = FindFirstWord(lineByLine);
           // FindFirstWord --> will break the line into 2 words, First word and remaining words
           
          while (p1.first.compare("}")) {
                if (!p1.first.compare("SCALE")) {
                      // calling some function
                } else {
                    if (!p1.first.compare("symbol")) {
                         // calling some function
                       } else {
                        if (!p1.first.compare("set_minimum_boundary")) {
                              // calling some function
                        } else {
                            if (!p1.first.compare("line")) {
                                // calling some function
                            } else {
                                if (!p1.first.compare("circle")) {
                                    // calling some function
                                    } else {
                                    if (!p1.first.compare("arc")) {
                                        // calling some function
                                       } else {
                                        if (!p1.first.compare("pin")) {
                                            // calling some function
                                       }
                                    }
                                }
                            }
                         }
                     }
                 }

I want to remove the chain of if-else statement using either by switch and enum or some other object oriented approach.

Aucun commentaire:

Enregistrer un commentaire