|cant use arrays because health, attacking, taking damage are class functions. |
To add to what @Ganado said... Huh????
In my post, player is an array of Character objects, and Character is a class. Arrays and classes are 2 different things that are not mutually exclusive. It would even be possible, if you wanted to (although not likely efficient), to have another class that contains an array of Character objects.
If I did use arrays for the heroes, it can also be
using namespace std;
cout<<"choose a hero :a b c"<<endl;
lname = l;
xname = l;
fname = l;
if (choicename == lname)
I'm not sure what point you are trying to make. What (mis)understanding of arrays are you trying to demonstrate here?
- Arrays can only be used for strings?
- Arrays can only be used to contain names for a specific hero?
- Arrays can only be used for "simple" data types? (note: string is actually a class and not a simple data type)
- Arrays can only be used to define fields that you didn't mention in the initial question?
Instead of telling us that arrays can't be used, why don't you explain to us why you think there is a problem and we can help you figure out how they can be applied. Do you think it's possible for an experienced programmer to have a better understanding of how the language works than a beginner who is confused by a class assignment? We want to help you learn C++, but you have to trust that we have some idea what we are talking about (especially in the basic, foundational stuff).
|When the user enters a word or letter, the program gets stuck in an infinite loop. This works fine with numbers though. How to prevent the user from entering a word and breaking the program? |
cin >> c;
is executed, because
is an integer, anything not looking like an integer remains in the input stream. So next time through the loop, the same input is there, and nothing is read (returning '0'). You need to perform an ignore on the input stream to flush all of the content. Try something like
This will ignore up to 256 characters or until it reaches a new line character. You can bullet-proof your code by using max int or max cin buffer size if you want to, but some sufficiently large number will work for your current class project.