Simon Says using For Loop

I'm absolutely lost; been working on this for about 4 hours straight yet! Any help would be appreciated:)

"Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares 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. Ex: The following patterns yield a userScore of 4:
simonPattern: RRGBRYYBGY
userPattern: RRGBBRYBGY
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
#include <iostream>
#include <string>
using namespace std;

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

   userScore = 0;
   simonPattern = "RRGBRYYBGY";
   userPattern  = "RRGBBRYBGY";
  
 for (i = 0; i < 10; i++) {
    if (simonPattern[i] == userPattern) {
       userScore = userScore + 1;
    } else {
       break;
    }
    
   cout << "userScore: " << userScore << endl;

   return 0;
}
add [i] to userpattern;

Add ‘}’ before ‘cout<<userScore....’

P.s. On your example userPattern =10, not 4. This is the target?

Else try
1
2
3
4
5
6
for(int i =0; i < Simonpattern.length(); ++i)
{ 
    size_t found = simonpattern.find( userpattern[i] );
    if( found != string::npos )
              userscore++;
}
Last edited on
I chose 10 since there are ten letters in the sequence and so the loop could possibly be iterated a total of ten times, but you know what, I had totally forgotten to set the conditions to be available all values lower than ten, as well. Thanks so much for pointing that out! Totally solved my problem, along with adding the [i] and checking that I had the proper amount of curly brackets (*facepalm). Thanks again. Here was my final program, if you're interested:

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

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

userScore = 0;
simonPattern = "RRGBRYYBGY";
userPattern = "RRGBBRYBGY";

for (i = 0; i <= 10; i++) {
if (simonPattern[i] == userPattern[i]) {
userScore = userScore + 1;
} else {
break;
}
}
cout << "userScore: " << userScore << endl;

return 0;
}
Topic archived. No new replies allowed.