I am trying to replace all characters in text with char
[*]. It only displays the first four. If i type anything after 6 digits it displays 5
[*]. Could anyone help me?. Look at the function twoplayer(w). Thats the one i am concerned with now

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

#define infile "music.txt"

char oneplayer(string z);
float twoplayer(float w);
float chances(int y);

int main()
{
    int x,y;
    string z;
    
    cout << "Welcome to hangman Game" << endl;
    cout << "For one player type 1, two player type 2" << endl;
    cin >> x;
    
    switch(x)
    {
             case 1:
                  
             cout << "Entering 1 player mode" << endl << endl;
             chances(y);
             oneplayer(z);
             break;
             case 2:
             cout << "Entering 2 player mode" << endl;
             chances(y);
             twoplayer(x);
             cout << endl;
             break;
             default: cout << "This isn't no nintendo Wii, we use flash drives";
             
             return 0;
             }
}
char oneplayer(string z)
{
      ifstream fin;
      ofstream fout;
      
      fin.open(infile);
      if(fin.fail())
      {
                    cout << "What" << endl;
                    }
      cout << "one" << endl;
      cin >> z;
      }
float twoplayer(float w)
{
      string p;
      string x;
      int charcount;
      x = '*';
      int z;

      do
      {
      cout << "Two player mode" << endl << endl;
      cout << "Enter a word two be guessed" << endl;
      cin >> p;
      cout << p.length() << endl;
      for(int i = 0; i < p.length(); ++i)
      { 

      cout << p.replace(i, i, x) << endl;   

      }
      cin>> z;
      }while (z!= z);
      }

Your problem is here:

cout << p.replace(i, i, x) << endl;

The first argument specifies the starting index. The second character does not specify the ending index, as you assumed here. It's the number of characters to replace, which in this case is 1. Replace the i with a 1, and it should work as expected.

What you were essentially doing here was changing the length of the string as you looped, replacing several characters with 1 *.

Also, please use code tags. They make code much easier to read.

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.