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;
             case 1:
             cout << "Entering 1 player mode" << endl << endl;
             case 2:
             cout << "Entering 2 player mode" << endl;
             cout << endl;
             default: cout << "This isn't no nintendo Wii, we use flash drives";
             return 0;
char oneplayer(string z)
      ifstream fin;
      ofstream fout;
                    cout << "What" << endl;
      cout << "one" << endl;
      cin >> z;
float twoplayer(float w)
      string p;
      string x;
      int charcount;
      x = '*';
      int z;

      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.

This article has been dead for over six months. Start a new discussion instead.