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()..

#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;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

1. when using string you also need

#include <string>

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

example char fighter1Attack = ' ';

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

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

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;
}

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!

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