Problem with functions, loops, etc.

Pages: 12
Ok, so I have a few problems with my program. This program is meant to simulate a battle. The user chooses which action he/she wants and the program executes that decision.

Here is the program:
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void displayhealth (int x, int y)
{
    cout << "\nYou ----- " << x << " Health\n";
    cout << "Enemy --- " << y << " Health\n";
}

int choices (int choice)
{
    string letter;
    cout << "Would you like to:\nA. Punch\nB. Kick\nC. Slash\nD. Use Magic\nE. Use A Potion\nF. Commit Suicide\nPlease enter the letter of your choice:";
    // A=1 B=2 C=3 D=4 E=5 F=6
    cin >> letter;
    if (letter == "a" || letter == "A")
    {
        choice = 1;
    }
    else if (letter == "b" || letter == "B")
    {
        choice = 2;
    }
    else if (letter == "c" || letter == "C")
    {
        choice = 3;
    }
    else if (letter == "d" || letter == "D")
    {
        choice = 4;
    }
    else if (letter == "e" || letter == "E")
    {
        choice = 5;
    }
    else if (letter == "f" || letter == "F")
    {
        choice = 6;
    }
    else
    {
        while (choice > 0 || choice < 7)
        {
            cout << "You did not enter a valid choice. Please try again: ";
            cin >> letter;
            if (letter == "a" || letter == "A")
            {
                choice = 1;
                break;
            }
            else if (letter == "b" || letter == "B")
            {
                choice = 2;
                break;
            }
            else if (letter == "c" || letter == "C")
            {
                choice = 3;
                break;
            }
            else if (letter == "d" || letter == "D")
            {
                choice = 4;
                break;
            }
            else if (letter == "e" || letter == "E")
            {
                choice = 5;
                break;
            }
            else if (letter == "f" || letter == "F")
            {
                choice = 6;
                break;
            }
            else
            {

            }
        }
    }
    return choice;
}
int damage1(int choice, int damage)
{
    srand(time(NULL));
    if (choice == 1)
    {
        damage = rand() % 10 + 5;
    }
    else if (choice == 2)
    {
        damage = rand() % 15 + 5;
    }
    else if (choice == 3)
    {
        damage = rand() % 30 + 1;
    }
    else if (choice == 4)
    {
        damage = rand() % 75 + -50;
    }
    else if (choice == 5)
    {
        damage = rand() % 100 + 25;
    }
    else if (choice == 6)
    {
        damage = rand() % 100 + 50;
    }
    return choice, damage;
}
int computer(int choice, int damage)
{
    if (choice = 1)
    {
        damage = damage1 (choice, damage);
        cout << "You punched the enemy for " << damage << " healh";
    }
    else if (choice = 2)
    {
        damage = damage1 (choice, damage);
        cout << "You kicked the enemy for " << damage << " healh";
    }
    else if (choice = 3)
    {
        damage = damage1 (choice, damage);
        cout << "You slashed the enemy for " << damage << " healh";
    }
    else if (choice = 4)
    {
        damage = damage1 (choice, damage);
        cout << "You used magic on the enemy for " << damage << " healh";
    }
    else if (choice = 5)
    {
        damage = damage1 (choice, damage);
        cout << "You used a potion which restored " << damage << " healh";
    }
    else if (choice = 6)
    {
        cout << "You commited suicide. Now that wasn't very smart";
    }
    return damage;
}

int main()
{
    int yhealth = 999, ehealth = 999, choice, damage;
    cout << "Battle: Begin!" << endl;
    displayhealth(yhealth, ehealth);
    choices (choice);
    computer (choice, damage);
    ehealth - damage;
    displayhealth(yhealth, ehealth);
    return 0;
}


As you can probably tell, it's not yet finished
Problems:
1. When the user chooses a decision, the program automatically chooses decision A

2. The program never subtracts damage from the enemies health so the health always reads 999.
First off, your code is a mess. Try using a switch statement, it will make everything much easier to read. Secondly, your program structure is strange. Why not execute the users choice in the actual if statements, instead of setting choice to a value and then passing that value to a function. It doesn't make sense. In your computer function, you're using the = operator, so your first if statement will always evaluate to true. Your main function needs some work too. You're not passing choice by reference to choices(), so it's value never gets changed in that function. This means you're passing an uninitialized variable to computer. Line 157 doesn't do anything...
And you have no looping, so you only actually execute one "stage" of the fight.
The function choices returns an integer of the user's choice but you don't make use of it.

You are passing an integer called choice into this function but not making use of it also.

I think you mean to do something like this:

1
2
choice = choices();
computer(choice, damage);
Ok, so i'm kind of new to programming. I don't really understand what either of you are trying to say. As i stated, this is a work in progress so that is why there is no loop of the fight. Also line 157 i messed up. I meant to do
ehealth = ehealth - damage;
The problem with this though is that the enemies health turns into -214 and a bunch of numbers after that. Correct me if I am wrong but this is sometimes the number you get with a unassigned integer variable. Also, I have never tried working with switch statements. I could try this but so far, I want to fix the original problems first.
Last edited on
Structural problems aside, have you fixed the incorrect use of operator= in computer() yet?
How would i fix it?
?
By replacing it with ==. Based on the rest of your code, it seemed like you know the difference between those two.
Last edited on
switch to if statement is personal preference most of the time

@ascii
O_o the ability to following along yourself doesn't mean the code is messy. Messy code is when you written statements out far longer then they needed to be ( at least thats how I known messy coding to be).

Answer 1) the reason the program always choices attack one is because.. why not. You didn't tell the computer that the choicing of attack she be based on anything all you did was say here are options pick one. The computer will always pick option 1 because to the computer there no difference between choice 1 to x number of choice they are all the same( also a computer does things in procedure 1 to last so if the choice are all the same to the computer it will choice the first). You have to remember that unless you give parameter to the computer ( as in AI and a computer in general) it will do nothing. Remember either define what exactly the computer must do or sent up guidelines that the computer must follow in order to proceed.( just sit back and actually think it must be taken for computer to do what you want it to do)
.
.
.
.
-___- think about it...
.
2) the reason you don't get a change in the enemys health is becomes your not thinking of this like math

I will rewrite what you did in a math example

4-5
..... ok what about 4-5 yes we and the computer knows that 4-5 =9 but thats great the 9 is produce but where does that nine go to... no where because you haven't assign a symbol to = or equate to 9 you notice in math everything always equals to the variable that your looking for be it x,y,z etc.

had you written the following;

4+5=x
9=x
:O!!!!!! now the computer nows where to put the new found variable.
what you should of done is the following



int Newhealth //or variable/name
Newhealth= ehealth-damage


Since your battle is only one round this is what I would personally do
then you can either manually type the final couts to show the final health or you can create another function for finalhealth to display.

*** sorry for typos x_x doing this at night***
Last edited on
doing this at night

Hm yeah, it shows:
9=x
4-5 =9


Messy code is when you written statements out far longer then they needed to be

In that case:
1
2
3
4
5
6
7
8
9
10
11
12
int choice()
{
  cout << "Would you like to:\nA. Punch\nB. Kick\nC. Slash\nD. Use Magic\nE. Use A Potion\nF. Commit Suicide\nPlease enter the letter of your choice: ";
  for(;;)
  {
    char letter;
    cin >> letter;
    if      (letter>='a' && letter<='f')return letter-'a'+1;
    else if (letter>='A' && letter<='F')return letter-'A'+1;
    else cout << "You did not enter a valid choice. Please try again: ";
  }
}


ability to following along yourself doesn't mean the code is messy.

Oh, but it does.
See also: http://www.thefreedictionary.com/messy
Last edited on
@Athar

doing this at night

Hm yeah, it shows:

Messy code is when you written statements out far longer then they needed to be


ability to following along yourself doesn't mean the code is messy.

Oh, but it does.
See also: http://www.thefreedictionary.com/messy

I didn't write 4-5=9 but is know that it is not but when you have a equations your finding the solution to thet variable so you would have 4+5( lol I put minus)=x

4+5=x
9=x
but if you do not have the x then the 9 can not be equated to anything. hence why I wrote 9=x...

Hm yeah, it shows:
9=x
4-5 =9

Don't see what your trying to point out with 9=x. It was written on the right so it was kept in the format of the equation


Also I never said it wasn't messy don't get me wrong.. but I was refering to the example he used. Also you gave the definition of a word used in general. Outside the context of programming then yes the can be used in such a way but using a general meaning of a word when not in the right context doesn't mean your using the word correct. If you would like I can provide numerous example of cases where a word is define to be used in such a way under whatever context you are using it in.
Last edited on
Thanks for the replies. I will answer a few questions and ask a few too.

Please read all of my posts. I stated that this is the basics of my program and that I will make a loop. Also, I changed ehealth - damage; to ehealth = ehealth - damage;

Stormhawk, can you give my a example of your first answer? I understand more by seeing than other's words.

Athar, I know you are trying to clean up my code in your post but I don't entirely understand what you did. How can you add a number to a string and how can you have one string that is greater than another string.

Thank you for considering
-DaPasta
Well, the meanings I had in mind when I posted that link were "confusing, difficult" and "disorganized, untidy".

I didn't write 4-5=9 but is know that it is not

Hm? Did you copy & paste that? If so, it's a good idea to post the link where you got it from.

but if you do not have the x then the 9 can not be equated to anything. hence why I wrote 9=x...

...?!
See:
Stormhawk wrote:
9=x
now the computer knows where to put the new found variable.
lmao sorry about that I meant to write I didn't mean to write 4-5=9 and that you know when you have a equation your trying to solve for the variable.

You got me there (even though I didn't proof read xD old habit.

ok but I am curious then. how come you pointed out 9=x O_o?
Last edited on
If you want to assign 9 to x, you need to write x=9;
Variables (lvalues) go the left side, values (l- or rvalues) go to the right side.
ohhh I totally agree with you. I am sorry I didn't say that I was talking about in a math equation if you were to find the x on the left you should just keep it there. but for programing then yes I totally agree with you.
Athar, I know you are trying to clean up my code in your post but I don't entirely understand what you did. How can you add a number to a string and how can you have one string that is greater than another string.

It's not a string, it's just a single character and characters are nothing but numbers in C++.
On most platforms ASCII is used: http://www.asciitable.com/
So 'a' equals the number 97, 'b' equals 98 and so on. That also means you can do arithmetic with them, e.g. 'c'-'a' equals 2 (99-97) and 'c' is greater than 'a' (99>97).
Last edited on
Herm.. ok. I used string variables in my code. The reason I did that was because what if the user enters a random string. The program will only take the first letter of the string. (Correct me if I am wrong on this) Sure I can change it.
ok So far these are the problems that I have encountered;

1) you forgot to added #include <string> when using strings

2) The variable 'choice' is being used without being initialized.
The variable 'damage' is being used without being initialized.
The program will only take the first letter of the string. (Correct me if I am wrong on this)

No, you're right. You can still use a string and then extract the first character as follows:
char letter=str[0];
This thread is getting off-topic, and dare I reuse the word... messy.

Just go through my post. If you understand something look it up or ask me. I'm happy to help, but you don't seem to have actually taken into consideration anything that anyone here has said yet.
Pages: 12