Hey guys its me again!

I'm stuck on a logic thing of where to put some code. Basically I want to take an input from the keyboard and store that into an array using a class whilst combining it with the other stuff that the class does.

void hangman::getletter(char letter)
{ 
	int i;
   for ( i= 0 ;i < 4 ; i++)
   {
	  
      if( letter ==name[i] )
      {
       encryption[i] = name[i];
       break;	  
      }     
   }

   if (i == 4)
      {
         cout << "not found" <<endl;
         looselife();
	    

      }
   
}

I though of adding it onto the end of here but not sure what the code would be. I've made a buffer to store it in and set it using the constructor, but how would i go about moving the 'i' (counter) everytime this function is called to aviod over writing of the array?

John........

Dani AI

Generated

— the simplest, cleanest fix is to make the write-position a member of your class instead of trying to pass i around. That keeps the state where it belongs (inside the object) and avoids callers having to manage an index. and are both right that you need persistent scope for the counter, but storing it as a private member or using a dynamic container is more idiomatic than returning an index every call.

A few concrete rules to follow:

  • Add a size_t nextIndex (initialize in the constructor) or use std::vector<char> / std::string and call push_back.
  • Only advance the index when you actually store a new guess (and avoid storing duplicates).
  • Check bounds before writing into a fixed array; if you run out of space, either ignore further guesses or resize/replace the buffer.
  • For hangman logic, track guessed letters (e.g. std::bitset<26> or std::set<char>) so repeated guesses do not consume slots or lives.

Example pattern (illustrative):

class Hangman {
  std::string secret;
  std::string visible;
  std::vector<char> guesses;
  std::bitset<26> used;
public:
  Hangman(const std::string &s): secret(s), visible(s.size(), '_') {}
  void addGuess(char c) {
    if (used.test(c - 'a')) return;       // already guessed
    used.set(c - 'a');
    guesses.push_back(c);                 // stores without manual index
    bool found = false;
    for (size_t i = 0; i < secret.size(); ++i)
      if (secret[i] == c) { visible[i] = c; found = true; }
    if (!found) looselife();
  }
};

If you prefer a fixed array, replace guesses.push_back with guesses[nextIndex++] and guard nextIndex < capacity. For container reference see std::vector::push_back.

Recommended Answers

All 2 Replies

Why dont u return the value of i to ur parent function and then whenever u have to perform the operation pass i as a parameter to it??

As shre86 said, if you want to keep the scope of the counter, it might behoove you to pass (and possibly return) i as a parameter.

But really, what do you want this function to do? If the user enters a correct letter, what happens in your overall program? What happens if the user guesses incorrectly? There may be a better way to solve your problem, but we'll need more information before a really useful answer can be given.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.