954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

My fighting Game

OK so here is a fighter game I made..except my fighters power won't decrease and I can't get my fighters names to show up....*sigh*... any idea.. My functions could be wrong.. but I always thought it went above int main()..

<code>#include&lt;iostream&gt;
using namespace std;
int fighterOne (char fighter1Attack){
string fighter1Name;
// char fighter1Attack; 
int fighter2Power=1000;
string fighter2Name;
cout &lt;&lt; fighter1Name &lt;&lt; &quot; please choose an attack (P) = Punch, (K) = Kick =&gt; &quot;;
cin &gt;&gt; fighter1Attack;
if (fighter1Attack == 'P' || fighter1Attack == 'p') {
fighter2Power -= 50;
cout &lt;&lt; fighter2Name &lt;&lt; &quot;'s health reduced to &quot; &lt;&lt; fighter2Power &lt;&lt; endl;
}
else if (fighter1Attack == 'K' || fighter1Attack == 'k') {
fighter2Power -= 100;
cout &lt;&lt; fighter2Name &lt;&lt; &quot;'s health reduced to &quot; &lt;&lt; fighter2Power &lt;&lt; endl;
}
else {
cout &lt;&lt; &quot;Invalid attack, you lose a turn!&quot; &lt;&lt; endl;
}
}
int fighterTwo (char fighter2Attack) {
string fighter2Name;
//char fighter2Attack; 
int fighter1Power=1000;
string fighter1Name;
cout &lt;&lt; fighter2Name &lt;&lt; &quot; please choose an attack (P) = Punch, (K) = Kick =&gt; &quot;;
cin &gt;&gt; fighter2Attack;
if (fighter2Attack == 'P' || fighter2Attack == 'p') {
fighter1Power -= 50;
}
else if (fighter2Attack == 'K' || fighter2Attack == 'k') {
fighter1Power -= 100;
cout &lt;&lt; fighter1Name &lt;&lt; &quot;'s health reduced to &quot; &lt;&lt; fighter1Power &lt;&lt; endl;
}
else {
cout &lt;&lt; &quot;Invalid attack, you lose a turn!&quot; &lt;&lt; endl;
}
}
int main() {
string fighter1Name = &quot;Greg&quot;; 
string fighter2Name = &quot;Casey&quot;; 
int fighter1Power = 1000; 
int fighter2Power = 1000; 
char fighter1Attack;
char fighter2Attack;
 
do {
fighterOne (fighter1Attack);
fighterTwo (fighter2Attack);
}while((fighter1Power &gt; 0) &amp;&amp; (fighter2Power &gt; 0));
if (fighter1Power &gt; fighter2Power) {
cout &lt;&lt; &quot;Fighter 1 is the winner!&quot; &lt;&lt; endl;
}
else if (fighter2Power &gt; fighter1Power) {
cout &lt;&lt; &quot;Fighter 2 is the winner!&quot; &lt;&lt; endl;
}
else {
cout &lt;&lt; &quot;The game ended in a draw!&quot; &lt;&lt; endl;
}
return 0;
}</code>
cjwenigma
Newbie Poster
8 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

This information will help us help you if you follow the suggestions therein.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

It looks as badly indented here as it does on other forums.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

1. when using string you also need

#include <string>

2. for char values try char name = ' ';

example char fighter1Attack = ' ';
kim165
Newbie Poster
1 post since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

>cin >> fighter2Attack;
if (fighter2Attack == 'P' || fighter2Attack == 'p')

Seeing as fighter2attack is a string that won't work.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Here is a better indented code {i used astyle to do this...}

#include<iostream>

using namespace std;

int fighterOne (char fighter1Attack)
{

  string fighter1Name;
// char fighter1Attack;
  int fighter2Power=1000;
  string fighter2Name;

  cout << fighter1Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  cin >> fighter1Attack;

  if (fighter1Attack == 'P' || fighter1Attack == 'p')
    {
      fighter2Power -= 50;
      cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
    }
  else if (fighter1Attack == 'K' || fighter1Attack == 'k')
    {
      fighter2Power -= 100;
      cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
    }
  else
    {
      cout << "Invalid attack, you lose a turn!" << endl;
    }


}


int fighterTwo (char fighter2Attack)
{

  string fighter2Name;
//char fighter2Attack;
  int fighter1Power=1000;
  string fighter1Name;

  cout << fighter2Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  cin >> fighter2Attack;

  if (fighter2Attack == 'P' || fighter2Attack == 'p')
    {
      fighter1Power -= 50;
    }
  else if (fighter2Attack == 'K' || fighter2Attack == 'k')
    {
      fighter1Power -= 100;
      cout << fighter1Name << "'s health reduced to " << fighter1Power << endl;
    }
  else
    {
      cout << "Invalid attack, you lose a turn!" << endl;
    }

}


int main()
{


  string fighter1Name = "Greg";
  string fighter2Name = "Casey";
  int fighter1Power = 1000;
  int fighter2Power = 1000;
  char fighter1Attack;
  char fighter2Attack;

  do
    {
      fighterOne (fighter1Attack);
      fighterTwo (fighter2Attack);
    }
  while ((fighter1Power > 0) && (fighter2Power > 0));
  if (fighter1Power > fighter2Power)
    {
      cout << "Fighter 1 is the winner!" << endl;
    }
  else if (fighter2Power > fighter1Power)
    {
      cout << "Fighter 2 is the winner!" << endl;
    }
  else
    {
      cout << "The game ended in a draw!" << endl;
    }


  return 0;
}
n.aggel
Posting Whiz in Training
203 posts since Nov 2006
Reputation Points: 23
Solved Threads: 12
 

here is something that works:

#include <iostream>
#include <string>
 
using namespace std;
 
/*although i didn't understand why you wanted to pass string fighter1Attack as an argument, i passed it as a reference*/
int fighterOne (string& fighter1Attack)
{
 
  string fighter1Name = "Subzero"; /*i added the names*/
  // char fighter1Attack;
  int fighter2Power=1000;
  string fighter2Name;
 
  cout << fighter1Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  cin >> fighter1Attack;
 
/*here i made the comparisons from a_string=='char' to a_string=="another_string"*/
  if (fighter1Attack == "P" || fighter1Attack == "p")
    {
      fighter2Power -= 50;
      cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
    }
  else if (fighter1Attack == "K" || fighter1Attack == "k")
    {
      fighter2Power -= 100;
      cout << fighter2Name << "'s health reduced to " << fighter2Power << endl;
    }
  else
    {
      cout << "Invalid attack, you lose a turn!" << endl;
    }
 
 
}
 
 
int fighterTwo (string& fighter2Attack)
{
 
  string fighter2Name = "Scorpio";
  //char fighter2Attack;
  int fighter1Power=1000;
  string fighter1Name;
 
  cout << fighter2Name << " please choose an attack (P) = Punch, (K) = Kick => ";
  cin >> fighter2Attack;
 
  if (fighter2Attack == "P" || fighter2Attack == "p")
    {
      fighter1Power -= 50;
    }
  else if (fighter2Attack == "K" || fighter2Attack == "k")
    {
      fighter1Power -= 100;
      cout << fighter1Name << "'s health reduced to " << fighter1Power << endl;
    }
  else
    {
      cout << "Invalid attack, you lose a turn!" << endl;
    }
 
}
 
 
int main()
{
 
 
  string fighter1Name = "Greg"; /*you don't pass the names to the function, so this is why you see subzero and scorpion when the program is running! Hint:: try to this like i did the string fighterXattack, if you can't i 'll help you.*/
  string fighter2Name = "Casey";
  int fighter1Power = 1000;
  int fighter2Power = 1000;
  string fighter1Attack;   /*i changed this part*/
  string fighter2Attack;   /*i changed this part */
 
  do
    {
      fighterOne (fighter1Attack);
      fighterTwo (fighter2Attack);
    }
  while ((fighter1Power > 0) && (fighter2Power > 0));
  if (fighter1Power > fighter2Power)
    {
      cout << "Fighter 1 is the winner!" << endl;
    }
  else if (fighter2Power > fighter1Power)
    {
      cout << "Fighter 2 is the winner!" << endl;
    }
  else
    {
      cout << "The game ended in a draw!" << endl;
    }
 
 
  return 0;
}


Basically the error that i foud was that you were using c++ string facilities with chars...this is why nothing worked....

Check the comments i made on your code.

Also you are starting object oriented programming maybe you should check about encapsulation 1 and 2

Another thing is we don't you implement the fighters as one class,and just instantiate 2 objects fighter1 and fighter2

Last but not least maybe you should check for a tutorial on strings:: http://www.cppreference.com/cppstring/index.html and http://www.cprogramming.com/tutorial/string.html

PS::it is the first time i help someone else, in this fourm, and i am relative noob so i don't promise that this the best thing you could do!

n.aggel
Posting Whiz in Training
203 posts since Nov 2006
Reputation Points: 23
Solved Threads: 12
 

description on fighter game

aqeeb
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 
description on fighter game


You better to explain why did you bump 2 years old thread with such poor request

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You