| | |
My fighting Game
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
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()..
C++ Syntax (Toggle Plain Text)
<span class="ad_notxt"><code class="inlinecode"> #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; } </code></span>
This information will help us help you if you follow the suggestions therein.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Sep 2007
Posts: 1
Reputation:
Solved Threads: 0
1. when using string you also need 2. for char values try char name = ' ';
C++ Syntax (Toggle Plain Text)
#include <string>
C++ Syntax (Toggle Plain Text)
example char fighter1Attack = ' ';
Here is a better indented code {i used astyle to do this...}
c++ Syntax (Toggle Plain Text)
#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; }
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.
by Robert Frost the "The Road Not Taken"
by Robert Frost the "The Road Not Taken"
here is something that works:
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!
c++ Syntax (Toggle Plain Text)
#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!
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.
by Robert Frost the "The Road Not Taken"
by Robert Frost the "The Road Not Taken"
![]() |
Similar Threads
- Word Association Game (Posting Games)
- Need help! ....please help me (C++)
- VB6 oRPG game: Book of Souls (Visual Basic 4 / 5 / 6)
- Anyone Play Crimson Skies (Geeks' Lounge)
- recall a game with mad red monks? (Geeks' Lounge)
- Big Game need progrmmers (C++)
- Your Fav Game (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: Inches to Feet, Fahrenheit to Celsius
- Next Thread: Output formated code:
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






