Starting Attempt at Menu with Win / Loss Results

Hello,

So essentially I am taking the knowledge I have acquired so far to put together a small game for a homework assignment. My goal is to give the player a set number of "lifepoints." When these lifepoints reach "0" you lose. My problem? I am a noob! But I am trying to learn. And I don't quite understand what I am doing in some of my code. This is where I am at:

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
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int checker (lifepoints)
{
    if (lifepoints == 0)
    {
        cout << "Game over.";
        return 0;
    }
}

int main()
{

    int response = 0;
    int lifepoints = 5000;

    cout << "You have " << lifepoints << " total.\n"
        << "When these run out, you lose the game.\n";
        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);
        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";
        }
        return 0;
}


I am trying to create a function that, after every question and response, checks whether said "lifepoints" are at "0" and, if so, declares the player a loser. As it stands, C++ doesn't understand what I am trying to do with the "checker" function.

As I am lost, I can't explain it any better. So... any help would be appreciated!! Sorry for being so newish to the language.
Hello JDRhoads10,

First line 12 is not needed, The function will automatically return when it ends.

As it stands, C++ doesn't understand what I am trying to do with the "checker" function.

That is because you never tell the program to use the checker function. You would defiantly want to call the checker function at line 47 and anywhere else you feel the need. Just having the function in the program does not mean that it is automatically used.

Hope that helps,

Andy

Edit: I noticed the checker function returns an "int" this is not necessary the way the function is written. A void function is all that is necessary here. And you may want to put an if statement around line 48 to print only if lifepoints i above 0.

Line 5 should be the end of line 33. Indenting lines 35 - 55 does not associate it with the while that is end of the do/while loop.
Last edited on
for a homework assignment.
Does it means you do not want the solution, but just hints?

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;
}

Last edited on
Topic archived. No new replies allowed.