main() and jousting project

I'm having trouble writing a main() function for my program... It needs to include
1. ask 10 questions
2. create two knights
3. Have each knight display.
4. have the two knights joust until one is unhorsed or one is exhausted
A. While loop: ask 4 questions in the condition of the while statement:
1. knight1 is not exhausted and
2. knight2 is not exhausted and
3. knight1 is on horse and
4. knight2 is on horse
B. Inside while loop, have knights attack each other, unhorse when appropriate.
C. Inside while loop, have each knight display.
D. (end of loop. Back up to 4A)

Here is what I have:
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
93 int main()
 94 {
 95   cout << "What is the first knights name?" << endl;
 96   cin >> Knight k1;
 97   cout << "What is" << name << "stamina?" << endl;
 98   cin >> k1_stamina;
 99   cout << "What is knights weapon?" << endl;
100   cin >> k1_weapon;
101   cout << "What is the weapon's hit chance?" << endl;
102   cin>> hc;
103   cout << "What is the weapon's stamina required to use?" << endl;
104   cin >> sr;
105   cout << "What is the second knights name?" << endl;
106   cin >> Knight k2;
107   cout>> "What is" << name << "stamina?" << endl;
108   cin >> k2_stamina;
109   cout >> "What is knights weapon?" << endl;
110   cin>> k2_weapon;
111   cout << "What is weapon's hit chance?" << endl;
112   cin >> hc;
113   cout << "What is weapon's stamina required?" << endl;
114   cin >> sr;
115 
116  Knight k1;
117  Weapon k1_weapon;
118  int k1_stamina;
119 
120  Knight k2;
121  Weapon k2_weapon;
122  int k2_stamina;
123 
124   while(k1=on_horse)
125   {
126     k1.attack;
127     if(did_you_hit=false)
128     {
129       k2.attack;
130     }
131     else
132 
133   }
134 
135 }

I've been trying for a while and just am not sure about my while loop...
here's the rest of the code which I haven't finished yet either... Please help if you can

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
#include<iostream>
  2 #include<string>
  3 #include"Random.h"
  4 using namespace std;
  5 
  6 class Weapon{
  7   private:
  8     int hit_chance;
  9     int stamina_required;
 10     string weapon_type;
 11  public:
 12     void display (void);
 13     Weapon(string type, int sr, int hc);
 14     int get_stamina_required(void);
 15     bool did_you_hit(void);
 16 };
 17 Weapon:: Weapon(string type, int sr, int hc)
 18   :hit_chance(hc), stamina_required(sr), weapon_type(type)
 19 {
 20 }
 21 
 22 void Weapon::display(void)
 23 {
 24   cout<< "hit chance=" << hit_chance << endl;
 25   cout<< "stamina required=" << stamina_required << endl;
 26   cout<< "weapon type is" << weapon_type<< endl;
 27 
 28 }
 29 
 30 int Weapon::get_stamina_required(void)
 31 {
 32    return stamina_required;
 33 }
 34 
 35 bool did_you_hit(void)
 36 {
 37  Random r(1,100)
 38  int prob=r.get();
 39 
 40  if (hc<prob)
 41  {
 42    return false;
 43  }
 44  else
 45    return true;
 46 }
48  class Knight{
 49       private:
 50         int stamina;
 51 
 52       public:
 53         bool attack();
 54         knight(string n, int stam);
 55         Weapon weapon_in_hand;
 56         bool on_horse;
 57         string name;
 58  };
 59 
 60 Knight:: knight(string n, int stam)
 61   :name(n), stamina(stam)
 62 {
 63 }
 64 
 65 bool Knight::on_horse
 66 {
 67   if (stam<=0)
 68   {
 69     return false;
 70  }
 71   if(false)
 72   {
 73     cout << n << "is exhausted" << endl;
 74   }
 75 
 76   }
 77   else
 78   return true;
 79 }
 80 
 81 bool Knight::attack()
 82 {
 83 
 84   if ( on_horse=true)
 85   {  get_stamina_required;
 86      stamina-=sr;
 87      did_you_hit;
 88 
 89   }
 90 
 91 }
One obvious problem. You're confusing asignment and equality operators.
 
while(k1=on_horse)

This is assigning the value of on_horse to k1. The result of that that assignment will always be true. If you want to test that the value of k1 is equal to on_horse, use the == operator. This also applies to if(did_you_hit=false)

Also, the following code:
{ get_stamina_required; is bogus. It's not a valid function call. () needed and you're calling a getter function, but not doing anything with the result.

Please do NOT include line numbers when using code tags.


Yes I was having some trouble with my attack function..
The function has to get the stamina required and subtract it from the knight's stamina... so should i do it more like this:
<code>
bool Knight:: attack()
{
(get_stamina_required);
stamina-=sr;
did_you_hit;
} </code>

I need to call these functions from the weapons class and member declarations but I'm not sure if I'm doing that right
get_stamina_required is a function of Weapon, so you need to use an instance of some Weapon.
1
2
3
4
bool Knight:: attack(const Weapon &w)
{ stamina -= w.get_stamina_required();
   did_you_hit;  // this is also bogus
}

did_you_hit is also not a valid function call.
It needs to be:
1
2
3
4
5
6
  if (did_you_hit())
  {  // do something
  }
  else
  { // do something else 
  }


BTW, code tags use [ and ], not < and >.
The () of a function call are the parameters you are passing to that function. You still need a function call to pass the parameters to. If get_stamina_required is a function, what parameters does it take? Also, what is did_you_hit? If both are functions that take no parameters, you would call them like this: get_stamina_required(). But functions usually return values and you aren't assigning the returns to anything. This is fine if they aren't supposed to, but from the general gist of the code it seems like they should be. for instance, the did_you_hit might return a bool value, true if you did and false if you didn't. If so, the call should look like

1
2
bool hit;
hit = did_you_hit();
.

Also, on line 71 your if is using false as a test expression. If you test something that is false to see if it's true or false, it's always going to BE false. I'm assuming you meant to test if the first if was false, but having 2 if statements in a row testing the same basic thing is pointless. You could have just written:

1
2
3
4
5
if (stam <= 0)
{
    cout << n << " is exhausted." << endl;
    return false;
}


If you are trying to test some other bool value in line 71 you forgot to add it.
Last edited on
Topic archived. No new replies allowed.