Else If statement help

Write your question here.

So I understand that we are taking the length of our hidden word and creating a variable for it.

What I am confused about is what does the I and J actually represent in the equation if they are basically doing the same thing. If I can get a more in-depth in lame terms way to understand this. I know we are checking each letter against our hidden word once letter at a time and checking that each value is equal to our hidden word but does the I represent our Input value? and what does J stand for THanks again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  Put the code you need help with here.


	//loop through all letters in the guess
	int32 HiddenWordLenght = MyHiddenWord.length();	
	for (int32 i = 0; i < HiddenWordLenght; i++) {
		//compare letters against the hidden word
		for (int32 j = 0; j < HiddenWordLenght; j++){
		//if they match then 
			if (Guess [i] == MyHiddenWord[i]) {
				//if theyre in the same place 
				if (i == j) {
					BullCowCount.Bulls++;//increment bu lls
				}
					 
				else {
					BullCowCount.Cows++;//incriment cows 
				}
					
			}

Actuall i and j are indexes within the string. Since j is not used as an index it is useless. It can be rewritten as
1
2
3
4
5
6
7
8
9
10
11
int32 HiddenWordLenght = MyHiddenWord.length();	
	for (int32 i = 0; i < HiddenWordLenght; i++) {
		//compare letters against the hidden word
		//if they match then 
			if (Guess [i] == MyHiddenWord[i]) {
					BullCowCount.Bulls++;//increment bu lls
				else {
					BullCowCount.Cows += HiddenWordLenght - 1;
				}
					
			}
Note that in this case the length of Guess must be at least HiddenWordLenght, otherwise you have undefined behavior/crash.
Add some debug print, or follow what goes on by single stepping in a debugger.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
using namespace std;

typedef int int32;

int main()
{
  string MyHiddenWord = "job";
  string Guess = "bot";
  struct {
    int Bulls;
    int Cows;
  } BullCowCount = {
  0, 0};

  //loop through all letters in the guess
  int32 HiddenWordLenght = MyHiddenWord.length();
  for (int32 i = 0; i < HiddenWordLenght; i++) {
    //compare letters against the hidden word
    for (int32 j = 0; j < HiddenWordLenght; j++) {
      //if they match then
      cout << "DEBUG: i=" << i
           << ", j=" << j
           << ", Guess[i]=" << Guess[i]
           << ", HW[i]=" << MyHiddenWord[i] << endl;
      if (Guess[i] == MyHiddenWord[i]) {
        //if theyre in the same place
        if (i == j) {
          BullCowCount.Bulls++; //increment bu lls
        } else {
          BullCowCount.Cows++;  //incriment cows
        }
      }
    }
  }
  cout << "Bulls=" << BullCowCount.Bulls
       << ", Cows=" << BullCowCount.Cows << endl;
  return 0;
}


$ g++ -std=c++11 main.cpp
$ ./a.out 
DEBUG: i=0, j=0, Guess[i]=b, HW[i]=j
DEBUG: i=0, j=1, Guess[i]=b, HW[i]=j
DEBUG: i=0, j=2, Guess[i]=b, HW[i]=j
DEBUG: i=1, j=0, Guess[i]=o, HW[i]=o
DEBUG: i=1, j=1, Guess[i]=o, HW[i]=o
DEBUG: i=1, j=2, Guess[i]=o, HW[i]=o
DEBUG: i=2, j=0, Guess[i]=t, HW[i]=b
DEBUG: i=2, j=1, Guess[i]=t, HW[i]=b
DEBUG: i=2, j=2, Guess[i]=t, HW[i]=b
Bulls=1, Cows=2

// Receives a Valid Guess incriements Turns and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
//incriment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;


//loop through all letters in the guess
int32 HiddenWordLenght = MyHiddenWord.length();
for (int32 MHWChar = 0; MHWChar < HiddenWordLenght; MHWChar++){
//compare letters against the hidden word
for (int32 GChar = 0; GChar < HiddenWordLenght; GChar++){
//if they match then
if (Guess [GChar] == MyHiddenWord[MHWChar]) {

if (MHWChar == GChar) {//if theyre in the same place
BullCowCount.Bulls++;// increment bulls
}

else {
BullCowCount.Cows++;//incriment cows
}

}

}

}
is the G char and the MHW char temporary variables that are only the scope of submit guess function ? and so are we declaring it at
or (int32 MHWChar = 0; MHWChar < HiddenWordLenght; MHWChar++)
that its value is this method or what is it exactly this is a tricky one to really comprehend fully for me thanks if anyone can break it down even further i am total newb to coding
> is the G char and the MHW char temporary variables that are only the scope of submit guess function ?
Yes they are limited to the scope of the function.

They're also limited to the scope of the for loop they're declared in.
1
2
3
4
5
6
7
for (int32 MHWChar = 0; MHWChar < HiddenWordLenght; MHWChar++){
    //compare letters against the hidden word
    for (int32 GChar = 0; GChar < HiddenWordLenght; GChar++){
    }
    // no more GChar here
}
// no more MHWChar here 


thanks i think i understand this snippet of code
Topic archived. No new replies allowed.