Search code examples
c++stringstring-comparison

Compare each character of two strings. New and looking for input


I'm currently in a class for C++ Development at WGU, and I'm presented with an exercise based on Simon Says. Basically, simonPattern is a string of 10 Characters (R, G, B, Y), and userInput would be the input at each step of the game. The chapter is on break and continue statements, but the exercise didn't provide any input on what kind of statements to use (if, while, for). I tried several different ways to write this but the compiler in the book exercises doesn't allow any more #include statements, I ended up passing with the following:

#include <iostream>
#include <string>
using namespace std;

int main() {
   string simonPattern;
   string userPattern;
   int userScore = 0;
   int i = 0;

   userScore = 0;
   simonPattern = "RRGBRYYBGY";
   userPattern  = "RRGBBRYBGY";
   char simonPattern0 = simonPattern[0];
   char simonPattern1 = simonPattern[1];
   char simonPattern2 = simonPattern[2];
   char simonPattern3 = simonPattern[3];
   char simonPattern4 = simonPattern[4];
   char simonPattern5 = simonPattern[5];
   char simonPattern6 = simonPattern[6];
   char simonPattern7 = simonPattern[7];
   char simonPattern8 = simonPattern[8];
   char simonPattern9 = simonPattern[9];
   char userPattern0 = userPattern[0];
   char userPattern1 = userPattern[1];
   char userPattern2 = userPattern[2];
   char userPattern3 = userPattern[3];
   char userPattern4 = userPattern[4];
   char userPattern5 = userPattern[5];
   char userPattern6 = userPattern[6];
   char userPattern7 = userPattern[7];
   char userPattern8 = userPattern[8];
   char userPattern9 = userPattern[9];

   for (i = 0; i < 2; ++i) {
      if (simonPattern0 == userPattern0) {
         userScore = ++userScore;
      }
      if (simonPattern0 != userPattern0) {
         break;
      }
      if (simonPattern1 == userPattern1) {
         userScore = ++userScore;
      }
      if (simonPattern1 != userPattern1) {
         break;
      }
      if (simonPattern2 == userPattern2) {
         userScore = ++userScore;
      }
      if (simonPattern2 != userPattern2) {
         break;
      }
      if (simonPattern3 == userPattern3) {
         userScore = ++userScore;
      }
      if (simonPattern3 != userPattern3) {
         break;
      }
      if (simonPattern4 == userPattern4) {
         userScore = ++userScore;
      }
      if (simonPattern4 != userPattern4) {
         break;
      }
      if (simonPattern5 == userPattern5) {
         userScore = ++userScore;
      }
      if (simonPattern5 != userPattern5) {
         break;
      }
      if (simonPattern6 == userPattern6) {
         userScore = ++userScore;
      }
      if (simonPattern6 != userPattern6) {
         break;
      }
      if (simonPattern7 == userPattern7) {
         userScore = ++userScore;
      }
      if (simonPattern7 != userPattern7) {
         break;
      }
      if (simonPattern8 == userPattern8) {
         userScore = ++userScore;
      }
      if (simonPattern8 != userPattern8) {
         break;
      }
      if (simonPattern9 == userPattern9) {
         userScore = ++userScore;
      }
      if (simonPattern9 != userPattern9) {
         break;
      }
      
   }

  

   cout << "userScore: " << userScore << endl;

   return 0;
}

While this did pass the tests for several different assignments to simonPattern and userPattern, I can't help but feel like this was pure brute force and could have been written with less code considering the restriction of only <iostream> and <string>. I'm also aware of if chains, but when I tried for and while it would throw errors.


Solution

  • You didn't need all those variables — you already had arrays to work with! And the beauty of the arrays it that you can use other variables to index into them, which allows you to collapse all that repeated logic into another loop.

    Your program could have been written like this:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
       string simonPattern;
       string userPattern;
       int userScore = 0;
       int i = 0;
    
       userScore = 0;
       simonPattern = "RRGBRYYBGY";
       userPattern  = "RRGBBRYBGY";
    
       for (i = 0; i < 2; ++i) {
          bool stop = false;
          for (int j = 0; j < 10; j++) {
             if (simonPattern[j] == userPattern[j]) {
                userScore = ++userScore;
             }
             else {
                stop = true;
                break;
             }
          }
          
          if (stop)
             break;
       }
    
       cout << "userScore: " << userScore << endl;
    
       return 0;
    }
    

    Admittedly, having to introduce bool stop to break out of the outer loop from within the inner loop is a bit minging. But we can fix that by hiving the logic off into a function, from which we can simply return when we want:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int calculateUserScore(const string& simonPattern, const string& userPattern) {
       int userScore = 0;
       
       for (int i = 0; i < 2; ++i) {
          for (int j = 0; j < 10; j++) {
             if (simonPattern[j] == userPattern[j])
                ++userScore;
             else
                return userScore;
          }
       }
    
       return userScore;
    }
    
    int main() {
       string simonPattern = "RRGBRYYBGY";
       string userPattern  = "RRGBBRYBGY";
    
       cout << "userScore: " << calculateUserScore(simonPattern, userPattern) << endl;
    }
    

    Notice that I've also moved your initial assignments into initialisations, and removed the redundant return 0 in main, for yet more brevity. userScore = ++userScore also contains a redundancy; ++ already performs the increment and there's no need to re-assign the result of doing that.

    Finally, I have no idea what the purpose of the i < 2 loop is: why do all this twice? Ensure you really meant to do that.