1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
#include <string>
// Microsoft specific header - it makes your programs not portable
// to other systems; avoid it
// #include <windows.h>
// To add a general solution that makes your program wait for the user to press
// ENTER before exiting, you need to include the following header:
//#include <limits>
// getting used to writing std::cout, std::cin, std::string will make your life
// easier in a very little time.
using namespace std; // try to avoid this
// You need to specify the type of the parameter
int checker (lifepoints) // <-- there's an error here
{
// Check you function: it can return 0 or...??
// What's the point of putting an "if" condition if there aren't two
// alternatives?
// I suppose you meant: if lifepoints are [...] then return a value that
// inform me about it; otherwise, returns another value that guarantees me
// I can safely go on.
// what if you decided to decrease lifepoints by 300 every time?
// 5000 - (300 * n) will never result 0, if 'n' is an integer.
if (lifepoints == 0) // <-- you can improve this, making it more general
{
cout << "Game over.";
return 0;
}
}
int main()
{
// Check main: you never invoke your function checker()
// A function that's never invoked, never runs.
// This is not C, you don't need to put all your variable declarations
// at the beginning of funtions. In general, you ought better stuck to
// the opposite practice: don't define a variable until you use it.
int response = 0;
int lifepoints = 5000;
cout << "You have " << lifepoints << " total.\n"
<< "When these run out, you lose the game.\n";
// Not portable statement: avoid it.
//Sleep(500);
do
{
cout << "\nWhat action would you like to take? \n"
<< " 1) Attack the evil rogues!!! \n"
<< " 2) Run from the onslought \n"
<< " 3) Try to talk to the rogues \n";
cin >> response;
}
while (response < 1 || response > 3);
// All the following if-else blocks should be placed inside the
// do-while loop.
// At the end, you should restore 'response' to a value that makes your
// loop cycle another time or exit.
// It means: if the loop must cycle another time, response must be
// less then 1 or bigger then 3; if loop must exit...
// (Note: secondary point, but it's worth a mention. A "switch" statement
// would be far easier to manage and read.)
if (response == 1)
{
cout << "The battle drags into the night "
<< "and by sunset no one knows \n"
<< "who is still alive!\n";
}
else if (response == 2)
{
cout << "You lose 200 lifepoints!\n";
lifepoints = lifepoints - 200;
cout << "You now stand at " << lifepoints;
}
else
{
cout << "You try talking to them but they seem unlikely to listen. \n"
<< "They take all your money and depart happy and you poor.\n";
}
// After having moved the above blocks inside the do-while loop,
// before getting to the "while" condition you need to invoke
// cheker() and decide an action based on its return value.
// If you want that your program asks for ENTER before closing,
// un-comment the following lines (don't forget to include
// <limits> at the beginning):
//std::cout << "\nPress ENTER to continue...\n";
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
|