*Problem Returning A String Function* Please Help

This is a discussion on *Problem Returning A String Function* Please Help within the C++ Programming forums, part of the General Programming Boards category; Okay, so the source computes a program that stimulates a duel. Basically, the cycle will complete until there is one ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    14

    *Problem Returning A String Function* Please Help

    Okay, so the source computes a program that stimulates a duel. Basically, the cycle will complete until there is one man standing.. though the problem is, in order to return a winners name, we must use a string function. I've had this working when I made it a void function, but now, since its a string it doesnt even work. So if you guys were to overlook my code and tell me where I am going wrong, it will be much appriciated. thanks a lot!

    Code:
    #include <iostream>
    #include <string>
     
    using namespace std;
    using std::string;
     
    void shoot(bool& targetAlive, double accuracy) // Single Shot Function
    {
    	double r = 0.1*(rand()%11); // Computes Random Number
    
    	if (r < accuracy) // Random Number Less Then Accuracy
    	{
    		targetAlive = false;		
    	}
    }
       
    string startDuel(bool aaronAlive, bool bobAlive, bool charlieAlive, double aa, double bb, double cc)
    { 
      
      int round = 1;
       
     do
     {
      cout << "Round #" << round << " : " << endl;  
      if (aaronAlive)
      {  
            
           if (charlieAlive)
       { 
             shoot(charlieAlive, .33);      
       }
       else
       {  
           shoot(bobAlive, .33);       
       }    
      }
      
      if (bobAlive)
      {
                 
       if (charlieAlive)
       {
        shoot(charlieAlive, 0.50);    
       }
       else
       {
        shoot(aaronAlive, 0.50);    
       }  
      }
        
      if (charlieAlive) 
      { 
                         
       if (bobAlive)
       {
        shoot(bobAlive, 1.00);        
       }
       else
       {
        shoot(aaronAlive, 1.00);    
       }   
      }    
      round ++;  
      
     } 
     
    while ( (aaronAlive && bobAlive) || (aaronAlive && charlieAlive) || (bobAlive && charlieAlive)); 
    
     if (aaronAlive) 
     {  
      return "Aaron ";                     
     }
     else if (bobAlive)
     {
      return "Bob ";       
     }
     else if (charlieAlive) 
     {
      return "Charlie ";        
     }
    }
     
    int main ()
    {     	
        srand((unsigned)time(NULL));
        
        string winner_;
        int Aaron = 0, Bob = 0, Charlie = 0, i;
        
        winner_ = startDuel(true, true, true , 0.33, 0.50, 1.00);
        
        for (i=0;i<=1000;i++)
        {
        winner_; 
        }
    
    	cout << endl;
    	cout << " -- RESULTS -- " << endl;
        cout << endl;
        cout << "Aaron won "   << Aaron   << " times !" << endl;
    	cout << "Bob won "	   << Bob     << " times !" << endl;
    	cout << "Charlie won " << Charlie << " times !" << endl;
    	cout << endl;
         
        system("pause");
    	return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    So.... what's the question? What's broken?

    Your code compiled on low warnings and gave this output:

    Code:
    Round #1 :
    Round #2 :
    
     -- RESULTS --
    
    Aaron won 0 times !
    Bob won 0 times !
    Charlie won 0 times !
    
    Press any key to continue . . .
    With warnings turned up:

    duel.cpp: In function `std::string startDuel(bool, bool, bool, double, double, double)':
    duel.cpp:81: warning: control reaches end of non-void function
    duel.cpp: In function `int main()':
    duel.cpp:94: warning: statement has no effect
    Fix these.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    14
    well, it's suppose to output each winner for each round.. there are suppose to be 1000 rounds total. I just find it weird when it works w/ a void function.. but we are suppose to use a string function to return the names of the winners.. there is suppose to be a long list of rounds w/ names following each round.. not just 2 rounds.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > duel.cpp:94: warning: statement has no effect
    Your for loop does NOTHING 1001 times.

    It's just a variable followed by ;
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I can't be bothered asking 20 question, so I just guessed this is what you want. I offer no guarentees that this works let alone is anything close to what you are trying to make.

    Code:
    #include <iostream>
    #include <string>
     
    using namespace std;
    
    void status(string &, string &, bool);
    
    void status(string shooter, string target, bool targetAlive)
    {
    	std::cout << shooter << " shoots at " << target << ".  " << target << " is "
    		<< (targetAlive ? "alive" : "dead") << "." << std::endl;
    }
     
    void shoot(bool& targetAlive, double accuracy) // Single Shot Function
    {
    	double r = 0.1*(rand()&#37;11); // Computes Random Number
    
    	if (r < accuracy) // Random Number Less Then Accuracy
    	{
    		targetAlive = false;
    	}
    }
       
    string startDuel(bool aaronAlive, bool bobAlive, bool charlieAlive, double aa, double bb, double cc)
    {
    	int round = 1;
    	do
    	{
    		cout << "Round #" << round << " : " << endl;
    		if (aaronAlive)
    		{
    			std::cout << "Aaron's turn.  ";
    			if (charlieAlive)
    			{
    				shoot(charlieAlive, aa);
    				status("Aaron","Charlie",charlieAlive);
    			}
    			else
    			{
    				shoot(bobAlive, aa);
    				status("Aaron","Bob",bobAlive);
    			}
    		}
    
    		if (bobAlive)
    		{
    			std::cout << "Bob's turn.  ";
    			if (charlieAlive)
    			{
    				shoot(charlieAlive, bb);
    				status("Bob","Charlie",charlieAlive);
    			}
    			else
    			{
    				shoot(aaronAlive, bb);
    				status("Bob","Aaron",aaronAlive);
    			}
    		}
    
    		if (charlieAlive)
    		{ 
    			std::cout << "Charlie's turn.  ";
    			if (bobAlive)
    			{
    				shoot(bobAlive, cc);
    				status("Charlie","Bob",bobAlive);
    			}
    			else
    			{
    				shoot(aaronAlive, cc);
    				status("Charlie","Aaron",aaronAlive);
    			}
    		}
    		round ++;
    	}while((aaronAlive && bobAlive) || (aaronAlive && charlieAlive) || (bobAlive && charlieAlive)); 
    	
    	if(aaronAlive)
    	{
    		return "Aaron ";
    	}
    	else if (bobAlive)
    	{
    		return "Bob ";
    	}
    	else if (charlieAlive) 
    	{
    		return "Charlie ";
    	}
    	else return "No one ";
    }
     
    int main ()
    {     	
    	srand((unsigned)time(NULL));
    	
    	string winner_;
    	int Aaron = 0, Bob = 0, Charlie = 0, i;
    	
    	for (i=0;i<=1000;i++)
    	{
    		winner_ = startDuel(true, true, true , 0.33, 0.50, 1.00);
    		if(winner_.compare("Aarron ") == 0)
    		{
    			Aaron++;
    		}
    		else if(winner_.compare("Bob ") == 0)
    		{
    			Bob++;
    		}
    		else if(winner_.compare("Charlie ") == 0)
    		{
    			Charlie++;
    		}
    	}
    	
    	cout << endl << " -- RESULTS -- " << endl << endl;
    	cout << "Aaron won " << Aaron << " times !" << endl;
    	cout << "Bob won " << Bob << " times !" << endl;
    	cout << "Charlie won " << Charlie << " times !" << endl << endl;
    	
    	system("pause");
    	return 0;
    }
    Edit to add:

    Bob has some serious hax btw. In the gaming community I'm involved in, we'd ban him for having an aimbot.

    Code:
     -- RESULTS --
    
    Aaron won 0 times !
    Bob won 417 times !
    Charlie won 181 times !
    Code:
     -- RESULTS --
    
    Aaron won 0 times !
    Bob won 405 times !
    Charlie won 184 times !
    Code:
     -- RESULTS --
    
    Aaron won 0 times !
    Bob won 385 times !
    Charlie won 221 times !
    Darn those accuracy values.
    Last edited by MacGyver; 05-14-2007 at 11:25 PM.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    14
    thanks a lot for the help ! ..
    yeah, i need to overlook a few things.. when i was testing this early today aaron didnt have zero total wins. thanks though..

    btw, what kind of compiler do you guys use?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yeah, these numbers were off. LOL. I mispelled Aaron in one of the string literals.

    Code:
     -- RESULTS --
    
    Aaron won 404 times !
    Bob won 393 times !
    Charlie won 203 times !
    No one won 0 times.
    
    Press any key to continue . . .
    That's better.

    Using MinGW at the moment, although I've used Borland's C++ compiler. Also used to use MSVS C++ (.NET version), but I don't like IDE's.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    14
    i've redone mine a bit, but i still cant seem to get the string function to return the winners name after he has won the round..

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    It should be returning the name of the winner.

    Code:
    winner_ = startDuel(true, true, true , 0.33, 0.50, 1.00);
    Make sure you have this line in main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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