function value won't apply to loop

I am trying to create a game and I have come across a problem I've been trying to fix for a few days now. Just an FYI, I put if(shotgun == 'A'), and realized I have nothing that indicates that I'm using the shotgun value for my answer while I have "if (shotgun == 'A')" in the function, could that be my problem? and if so how could I do that
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
#include <iostream>

//function one (don't do much)
void item_drops(char  &shotgun, char &shotgun_shells)
{
	std::cout << std::endl << "A: Shotgun" << std::endl << "B: Empty" << std::endl << "C: Shotgun Shells " << std::endl;

	shotgun = NULL;
	shotgun_shells = NULL;
}
//function 2 that has the main functionality
  void equipped(char& shotgun, char & shotgun_shells, char & shotgun_helper, char & shotgun_shells_helper)
{
	  
	  shotgun_helper = NULL;
	  shotgun_shells_helper = NULL;

	  if (shotgun == 'A')
	  {
		  shotgun_shells_helper = false;
	  }

	  if (shotgun_shells == 'C')
	  {
		  shotgun_helper = false;
	  }
}

int main()
{
	
	std::cout << "Search the house for equipment " << std::endl;
	int search_2 = 2;
	//I don't think the infinite do-while loop has anything to do with my issue
	do {
		std::cout << " (1)A: Cabinets " << std::endl << "(2)B: Desks " << std::endl << "(3)C: Garage " << std::endl << "(4)D: Oven " << std::endl << "(5)E: Bed Sheets " << std::endl;

		//nested swich statement
		char search;
		std::cin >> search;
		switch (search)
		{
		case 'A':
		{
			std::cout << "Opens cabinet* " << std::endl;
			//Don't take my naming too seriously lol
			char koala;
			char shotgun_shells;
			char shotgun;
			char shotgun_shells_helper;
			char shotgun_helper;
			//calling functions
			item_drops(shotgun, shotgun_shells);
			equipped(shotgun, shotgun_shells, shotgun_shells_helper, shotgun_helper);

		    std::cin >> koala;
			switch (koala)
			{
			case 'A':
					if (shotgun_helper == NULL)
					{
						std::cout << "I found a shotgun but no shotgun shells. " << std::endl;
					}
					else if(shotgun_helper == false) {
						std::cout << "I found a shotgun to go with my shells. " << std::endl;
					}
				
					break;
				case 'B':
					std::cout << "There's nothing there " << std::endl;

					break;
				case 'C':
					if (shotgun_shells_helper == NULL)
					{
					std::cout << "I found some shotgun shells , but no gun. " << std::endl;
					 }
					else if(shotgun_shells_helper == false) {
						std::cout << "I found some shotgun shells to go with my gun . " << std::endl;
					}
					break;
			}
		}
		}
	} while (search_2 == 2);
}

I've marked some of it so it's a little more clear, but when I apply my value, or enter 'A' in the nested switch, it enters the correct statement and the code sets you back to the main menu, but then I press 'C' in the nested switch and it says the default. It's basically a chain reaction that's supposed to happen in the switch statement as seen in the code (but doesn't) and when I press 'A' (in the nested switch) the text for 'C' (also in the nested switch) is supposed to change but doesn't, could someone help guide me with this problem?. My apologies if it isn't specific enough, I can change it if that is the case.
Last edited on
In equipped(), shotgun_shells_helper is a char, but you assign it NULL (a pointer) or false (a boolean). Both of these things will assign the same value (0) to it.

So what do you really want shotgun_shells_helper to contain? A character like 'A' , 'B' or 'q'? A boolean (true or false)? Something else?

In main you have
1
2
3
4
5
                    if (shotgun_helper == NULL) {
                        std::cout << "I found a shotgun but no shotgun shells. " << std::endl;
                    } else if (shotgun_helper == false) {
                        std::cout << "I found a shotgun to go with my shells. " << std::endl;
                    }


This is equivalent to:
1
2
3
4
5
                    if (shotgun_helper == 0) {
                        std::cout << "I found a shotgun but no shotgun shells. " << std::endl;
                    } else if (shotgun_helper == 0) {
                        std::cout << "I found a shotgun to go with my shells. " << std::endl;
                    }

Sinced the if and the else if conditions are the same, the else if will never be true.
My first question is in which scenario is shotgun not equal to null?
@markyrocks Ok I misunderstood, I am not too sure honestly i just added that last minute, but it's been removed, I can't make it to = 'A' though because it would mess things up so I just did that not thinking.
@dhayden I'm not too sure how replies work here so bear with me, but I want shotgun_shells_helper to equal a changeable value that if, lets say shotgun equals something by pressing the 'A' key, I want shotgun_shells_helper to notice that, and change its value to equal something different, kind of like a domino effect.
I only ask bc shotgun has a direct effect on the value of shotgunhelper and/shellhelper...

As far as I can tell from your code there's no initial value (or initialization) for either of those variables and I wouldn't be surprised if they were just equal to junk data. So that being said the chances of either being equal to false or null is probably slim. Maybe your compiler is different but I know visual studio would definitely complain.

scratch that I do see that you give a default value of null in the one function...
Last edited on
Topic archived. No new replies allowed.