Thread: Trouble Looping!

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    15

    Trouble Looping!

    I am designing a simple game that takes random word from a file, turns the string into stars and then the user guesses the string character by character, so basically like wheel of fortune!

    I have everything else working but I am currently trying to design a function that will take the string of stars(which is passed by reference) and replace it with the guess character if it is right by searching the phrase before it was turning into a string of stars.

    Code:
    int find_replace(string& stars, string clear, char guess)// Clear =phrase before changed to stars
    {
    
    	
    	
    	int times=0, n=0;
    	string guess1; 
    	
    	
    	while( n <= clear.length() -1)
    	{
    		
    		if (clear[n] == guess )
    		{
    			clear[n]= guess; n++; times ++;
    		}
    			else if (clear[n] != guess )
    				{
    					
    					cout << "WRONG!" << endl;
    					n++;
    					
    				}
    					else if(clear[n] == ' ')
    						{
    							clear[n] = clear[n]; n++;
    						}
    
    	
    	
    }
    	cout << stars << endl; 
    	
    	return times; 
    	
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    I just want to see if anyone can point out the flawed logic in this. I'm pretty sure it's something obvious but I've been looking at it too long too see it!
    Thanks

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. You never try to change stars, so consequently it does not change.
    2. You probably don't want to print WRONG for every letter in the word, only if no matches are found.
    3. I wish you the best of luck in getting to that third branch.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I looked at it for 5 seconds, then decided the indentation was too confusing to waste more time on it.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with nesting in looping
    By wobbles in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2010, 02:30 AM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. Looping trouble - Spitting out too many lines at once
    By Venicia in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2007, 04:45 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM