I have a question

This is a discussion on I have a question within the C++ Programming forums, part of the General Programming Boards category; I just signed up for this site.. so i hope this works Im in the middle of writing a program ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Exclamation I have a question

    I just signed up for this site.. so i hope this works


    Im in the middle of writing a program and i kinda hit a small brick wall..

    I need to be able to replace all the letters in a string/array to '*'. Itried many ways and I have had no luck.. if you guys know how could u help a brother out

    thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Post what you have tried in [code][/code] tags, explain what is not working, including any compiler errors or input and incorrect output.

    The best way to accomplish your task depends a lot on what you are using and what you are familiar with.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    k

    well ill explain my problem.

    Im doing a hangman game where the user inputs a name and the amount of tries it takes to reach the answer. now so far I have this.

    int main ()

    {

    std::cout<<"enter:"<<endl;
    std::cin>>string1;
    int len = string1.length();
    cout<<len<<endl;
    int j =0;
    for (j=0; j <len; j++)
    {
    cout<<"*";
    }
    cout<<endl;
    cout<<"enter a char to be guessed: "<<endl;
    cin>> n;
    cout<<endl;

    as you can see from the code I need to put '*' in replace of the letters. I did that.. Now all I need to know is how to replace each * with the quessed letter. I hope this makes sence. man im no good at this.
    lol

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    int main( void )
    {
    	std::string Word = "WWWWWWWWWWWWWWWWW";
    
    	std::cout<< '\n' << "Enter a word: ";
    //	std::cin >> Word; // I would use a getline for this
    //	getline( cin, Word );
    
    	Word.replace( Word.begin(), Word.end(), Word.size(), '*' );
    
    	std::cout	<< '\n' << Word
    			<< '\n';
    
    	return 0;
    }
    is an option too ...

    Edit -

    Please use code tags -

    [code]Put these around whatever code you're posting so make it nice and neat![/code]

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can't really replace the *'s in the output with letters very easily in standard code. You can look into non-standard console functions (search for adrianxw's console tutorial) or you can just re-output the whole thing each time.

    I might have two strings, one with *'s and one with the actual word. When the user gets a letter right, change the * to that letter in the first string. Then output the string each time.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    You'll need a second string to be the guessing string. As the game progresses, you need to change it to the word string.

    For evey guessed letter, you need to search the word for that letter. If it is, you'll need the index of that letter in the string to tell you were to put it in you guessing string. For some letters there may be more than one index, so you'll need to work that in too.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    ooo ic

    thanks

    man this thing rules.. imma use it more oten now hehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21