TheJeebo / CPP_LE_1

C++ Learning Environment 1
0 stars 0 forks source link

Fix styling (optional) #2

Open charlesamacon opened 4 years ago

charlesamacon commented 4 years ago

Code is a bit hard to read with current bracket style. This comes down to personal preference, but I recommend the Allman Style

Current Code:

while(true){
        delayCout(intro, 20);
        intro = "/n " + removeIt(intro);
        delayCout("  " + player1.getInfo("name") + "\n",100);
        std::cin >> uIn;
        std::cout << attempt;
        if(forceLower(uIn) == "exit"){
            delayCout("ERROR\n     ", 200);
            system("cls");
            ghost1.Intro(player1.getInfo("name"));
            attempt++;
        } else if(forceLower(uIn) == "info"){
            std::string msg1 = "Player Name: " + player1.getInfo("name") + "\n ";
            std::string msg2 = "Player Age: " + player1.getInfo("age") + "\n  ";
            std::string msg3 = "Player Height: " + player1.getInfo("height") + "\n   ";
            delayCout(msg1, 20);
            delayCout(msg2, 40);
            delayCout(msg3,80);
            attempt++;
        } else if(uIn == "n"){
            delayCout("T3RM1N@TE\n", 40);
            break;
        } else if(forceLower(uIn) == "help"){
            if(attempt % 3 == 0){
                ghost1.Static();
            } else if(attempt % 2 == 0){
                ghost1.Static2();
            }
            std::cout << "\n\n no\n\\nn";
            attempt++;
        } else if(attempt > 10){
            ghost1.Intro(player1.getInfo("name"));
            break;
        } else{
            std::string msg4 = "eeee\n eee\n ee\n e\nexit\n  ";
            std::string msg5 = uIn + uIn + uIn + uIn + "\n  ";
            delayCout(msg4, 20);
            delayCout(msg5, 10);
            attempt++;
        }
    }

Optional Allman Bracket Styling

while(true)
{
        delayCout(intro, 20);
        intro = "/n " + removeIt(intro);
        delayCout("  " + player1.getInfo("name") + "\n",100);
        std::cin >> uIn;
        std::cout << attempt;
        if(forceLower(uIn) == "exit")
        {
            delayCout("ERROR\n     ", 200);
            system("cls");
            ghost1.Intro(player1.getInfo("name"));
            attempt++;
        } 
        else if(forceLower(uIn) == "info")
        {
            std::string msg1 = "Player Name: " + player1.getInfo("name") + "\n ";
            std::string msg2 = "Player Age: " + player1.getInfo("age") + "\n  ";
            std::string msg3 = "Player Height: " + player1.getInfo("height") + "\n   ";
            delayCout(msg1, 20);
            delayCout(msg2, 40);
            delayCout(msg3,80);
            attempt++;
        } 
       else if(uIn == "n")
       {
            delayCout("T3RM1N@TE\n", 40);
            break;
        } 
       else if(forceLower(uIn) == "help")
       {
            if(attempt % 3 == 0)
            {
                ghost1.Static();
            } 
            else if(attempt % 2 == 0)
            {
                ghost1.Static2();
            }
            std::cout << "\n\n no\n\\nn";
            attempt++;
        } 
       else if(attempt > 10)
       {
            ghost1.Intro(player1.getInfo("name"));
            break;
        } 
       else
       {
            std::string msg4 = "eeee\n eee\n ee\n e\nexit\n  ";
            std::string msg5 = uIn + uIn + uIn + uIn + "\n  ";
            delayCout(msg4, 20);
            delayCout(msg5, 10);
            attempt++;
        }
    }

Keeps it legible. Also, I would break out your forceLower(uIn) == whatever statements into a switch statement, if possible.

TheJeebo commented 4 years ago

I'm still trying to implement the switch statement but ran into a snag when I found it doesnt support strings, just int and char. I have seen the Allman style in other references but didnt know it had a name so I'll practice it and convert this code

charlesamacon commented 4 years ago

I would recommend setting up an enum of your returnable strings and doing something like:

enum inputs
{
      exit,
      info,
      n,
      help,
      [etc......]
};

inputs compareString (std::string const& inputString)
{
      if (inputString == "exit") return exit;
      else if (inputString == "info") return info;
      ....
      else [return whatever you want for a default]
}

void yourFunction()
{
      switch (compareString(inputString))
      {
            case exit:
            .....
            case info:
            .......
      }
}
TheJeebo commented 4 years ago

Very cool, I didnt know about that. I will give that a try.