What values does i hold in int i?

This is the bleep project in codecademy ( references and pointers ). in the line asterisk(std::string word, std::string &text, int i) what values does i have in int i?

#include <iostream>

#include <string>

#include "index.hpp"

int main() {

  

  std::string word = "broccoli";

  

  std::string text = "I hate broccoli.";

  

  bleep(word, text);

  

  for (int i = 0; i < text.size(); i++) {

    

    std::cout << text[i];

    

  }

  

  std::cout << "\n";

  

}

void asterisk(std::string word, std::string &text, int i) {

  for (int k = 0; k < word.size(); ++k) {

  

    text[i+k] = '+';

          

  }

  

}

void bleep(std::string word, std::string &text) {

    

  for (int i = 0; i < text.size(); ++i) {

        

    int match = 0;

        

    for (int j = 0; j < word.size(); ++j) {

    

      if (text[i+j] == word[j]) {

        

        ++match;

          

      }

    

    }

        

    if (match == word.size()) {

            

      asterisk(word, text, i);

        

    }

      

  }

    

}

There are a couple ways we could look at this.

We can try tracing the value back through the program.

Given this void asterisk(std::string word, std::string &text, int i) we know it is an int. Then we look at were it is called:

asterisk(word, text, i);

Here we see asterisk is given the value of i, so see were i is in your program:

for (int i = 0; i < text.size(); ++i) Now we see that i is the value being used in your for loop.


Or if you simply want to know specifically at one point, we could try using std::cout on it. You could add one at the top of your asterisk() function to see what it prints:

std::cout << i;
1 Like