First, a few things:
1) Try using functions
This means that you can reuse your code, say for battles, and makes the whole program easier to read, and takes less code.
2) Use classes for your objects
This is especially useful, as it allows you to easily add in multiple footmen and enemies and things, without having to hard code variables for each one (of course, don't bother if you haven't learnt about classes yet, though I highly recommend that you do).
3) Try to avoid duplication
Basically, this is to allow you to do everything faster, and allows you to read through your code faster, due to less being repeated. The normal way to do this is to use functions, though in your case, the main problem that stands out is using a seperate "if" branch for each option, rather than grouping them together where applicable, i.e. "y" and "Y" sharing a branch, "n" and "N" sharing a branch.
--------------------
The way that I would recommend to do for your question would be to simply use a loop, and break from the loop when a valid answer comes. Here would be how I would do this:
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
//...
cout << "Thats a nice name, can you fight at all? (Y/N):" << endl;
while (true) {
char response;
cin >> response;
if (response == 'y' || response == 'Y') {
// Respond to option...
break; // break from the loop
} else if (response == 'n' || response == 'N') {
// Respond to option...
break;
} else {
cout << "You have selected an unrecognized answer. \n\n";
cout << "Can you fight at all? (Y/N): " << endl;
}
}
// continue on...
|