Variables not passing correctly

Well I have my program running and the Variables are not passing correctly and the return statements are not returning correctly.
Here is the parts that are not working.
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
#include <iostream>
#include "fight.h"
#include <time.h>
#include "player.h"
#include "stdlib.h"
#include <cstdlib>

using namespace std;

 
 combat A;
	

combat::combat(void)
{
	hit = 10;
	zombiehitmod = 1;
	zombiehitchance = 1;
	zombiechop = 2;
	playerhealth = 10;
	zombiehealth = 3; 
	   
}
combat::~combat()
{
}


int combat::fight_combat()
{
	
	  

	 
	cout << "               Choose your attack. " << endl;
	cout << "     Enter 1 to shoot or 2 of Hand to Hand" << endl;
	cout << " " << endl;
	//cout << " prime is" << this -> prime << endl;
	//cout << "ammo is "<< this -> ammo << endl;
	//cout << "second is " << this -> second << endl;
	//cout << "hitchance is " << this -> hitchance << endl;
	//cout << "     player health is " << A.playerhealth << endl;
	//cout << "     zombie health is " << A.zombiehealth << endl;
	//cout << "randhit mod is" << randhitmod << endl;
	cin >> A.attack;
	
cout << "Player health is " << A.playerhealth << endl;
		cout << "zombie health is " << A.zombiehealth << endl;
		cout << "random hit mod is " << A.randhitmod << endl;
			
	 A.playerAttack(); // call playerAttack function
	 	  if (A.zombiehealth <= 0)	  //move on
			{
				cout << "That was close hopefully there will not be too many of those." << endl;
			}
	 A.deadAttack(); // call zombieAttack function

	  while (A.playerhealth > 0, A.zombiehealth > 0); //Loop until one or the other is dead
		
		
			  if (A.playerhealth <= 0)		  //game over
			{
			cout << "The zombie pulls you to the ground and starts to devour your flesh. Hopefully you will not feel this too long. " << endl;
				cout << "Game over" << endl;
				 cin.get();
				cin.get();
				return 0;
			}

		
		
			cin.get();
			cin.get();
	return zombiehealth;
}

int combat::playerAttack() //player Attack function 
{
		//cout << "Player health is " << A.playerhealth << endl;
		//cout << "zombie health is " << A.zombiehealth << endl;
		//cout << "random hit mod is " << A.randhitmod << endl;
 switch (A.attack)//if (A.attack == 1)

		   {		

			case 1:			  //pirmary attack
				if (A.randhitmod + A.hitchance >= A.hit)
					{
						  A.ammo --;
						cout << "You hit the zombie" << endl; //Player hit zombie
						A.zombiehealth = A.zombiehealth - A.prime;
					}
				else //if (randhitmod + this -> hitchance < A.hit, this -> ammo --)
				
					{
						A.ammo--;
						cout << "You missed the zombie" << endl; // player misses zombie
					}
				

				  if (A.ammo == 0)	 //ammo check
						{
						cout << "Click, click you are out of ammo." << endl;
						A.hitchance = -9999;
						}
				  break;
		//if ( A.attack == 2)
			case 2:			 //secondary attack

				if (A.randhitmod +  A.hitchance >= A.hit)
					{
						cout << "You hit the zombie" << endl;
						A.zombiehealth = A.zombiehealth -  A.second;
					}
				else //if (A.randhitmod + A.hitchance < A.hit)
					{
						cout << "You missed the zombie" << endl;
					}
				break;
				
			default:
				cout << "Please enter either 1 or 2." << endl;
				break;

		   }
		   return A.zombiehealth;
}

 int combat::deadAttack()//zombie attacks
{

		   //cout << "Player health is " << A.playerhealth << endl;
		//cout << "zombie health is " << A.zombiehealth << endl;
		//cout << "random hit mod is " << A.randhitmod << endl;
	if (A.zombiehitmod + A.randhitmod >= A.hit) //zombie hits player
		{
			cout << "The zombie hits you" << endl;
			A.playerhealth = A.playerhealth - A.zombiechop;
			cout << "playerhealth is " << playerhealth <<endl;
		}
	else //if (A.zombiehitmod + A.randhitmod <= A.hit) //zombie deals damage
		{
			cout << "The zombie missed you" << endl;
		}
	return A.playerhealth;
}
Last edited on
A functioning function with return:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int a = 1;

int func(int a)
{
      a = a + 1;
      return a;
}

int main()
{
       cout << a << endl;
       a = func(a)
       cout << a << endl;
       system("PAUSE");
       return 0;
}

but this needs to return a variable and the variable A.randhitmod ant the health variables are not passing correctly they end up being 0 somewhere nin the running of the program.
When they go to 0, don't you die?

just do A.randhitmod = func(stuff) // A.randhitmod will now get whatever func() returns

int func(int stuff)// the func()
{
stuff = stuff + 1; // modify stuff
return stuff; // return stuff
}

yes 0 is supposed to be dead, which is why each attack function is supposed to return the others health but if it is 0 in the function and doesn't return properly
A.randhit mod is supposed to be a random number set up earlier in this function.
Last edited on
do

1
2
3
4
5
6
7
8
9
10
#include <ctime>
#include <cstdio>
#include <cstdlib>
int func(int stuff)
{
     srand((unsigned)time(0)); // seed the timer
     int r = 1 + rand() % (5);  /* make random number, put a "1 +" if you want from 1 - 5, remove if you want 0 - 5, "% (5)" is max number */
     stuff = stuff + r;
     return stuff;
}
still doesnt help much as that was the least of my problems
I don't see randhitmod anywhere in your program, is it in a header?

and why do you use a class? are you trying to have like save files for multiple players?
it was there and this is not the whole program but yes it has to be its own calss for various reasons not yet implemented.
You return zombiehealth in the battle functions. They're supposed to be implemented once as 10 and they will stay at 10 unless you wanted to change it. If you return it, the new value will turn into A.zombiehealth
exactly one returns player and the other returns zombie but it doesn't loop back like it supposed to should neither one be at zero and the end of the first run through the battle.
But I though that you should only return player, not zombie, and the fight sequence should have a loop withing itself and only return after the battle is done. Which it returns player health
Topic archived. No new replies allowed.