Simon Says

The program is suppose to compare the two strings starting from index 0. For each match, add one point to userScore. Upon a mismatch, exit the loop using a break statement. Why isnt it counting all the points?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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";

   /* Your solution goes here  */
while (userPattern[i] == simonPattern[i]) {
   userScore = userScore + 1;
   ++i;
   if (userPattern != simonPattern) {
      break;
   }
}
   
   cout << "userScore: " << userScore << endl;

   return 0;
}
Line 19: You're going to break out of the loop on the first iteration. You're testing if the entire strings are the same. They're not.

Just get rid of lines 19-20. You don't need them. You will exit the loop at line 16 on the first mismatch.

You're going to have a string overrun problem if the strings match. Your while statement does not terminate at end of the string.

Last edited on
Topic archived. No new replies allowed.