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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
If I did use arrays for the heroes, it can also be
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(){
string l[]={"a","b","c"};
string choicename;
cout<<"choose a hero :a b c"<<endl;
cin>>choicename;
string lname,xname,fname;
lname = l[0];
xname = l[1];
fname = l[2];
if (choicename == lname)
cout<<"yes";
else
cout<<"no";
}
|
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? |
When
cin >> c;
is executed, because
c
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
cin.ignore(256, '\n');
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.