How to make a boolean return if the button 'R' is pressed?

I started learning C++ yesterday and its pretty good. I am reading of this sites' tutorial.

Since I want to test my skills, I want to learn to make a simple Gun Reloading application in the console. For those who play CoD Black ops, or any of the series, you should get a good idea of what I am talking about.

Basically here is my code:

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

#include "stdafx.h"
#include <iostream.h>

bool pressReload
{
	if //press button "R"
        // char x;
        // if x == r
	return 1;
}

int main
{
	int magSize = 30;  //The magazine size which doesn't change
	int magCount = 20; //The number of bullets in a magazine
	int ammo = 90;     //The total amount of bullets

	if pressReload
	{
		if !(magCount == magSize)
			return 1;
		else 
		{
			int numToAdd = magSize - magCount;

			if numToAdd > ammo { 
				return 1;
			}
			else 
			{
				magCount += numToAdd;
				ammo -= numToAdd;
			}

		}

	}


        cout << "Bullets in mag: " << magCount << endl;
        cout << "Mag size: " << magSize << endl;
        cout << "Ammo left: " << ammo << endl;


	return 0;
}





Basically the user starts off with 20 bullets, which is the count, the maximum bullets in a mag is 30. I basically want him to reload so that his count goes to 30, and his ammo loses 10, going down to 80.

Is there some code to trigger or return within a boolean from some keyboard key?

Thanks in advance!
#include <iostream.h>

I didn't catch this the first time I read it. When I did see it, I chuckled a bit. You don't need the .h, and when you create a project in VC++, set it up so that you don't use precompiled headers (#include "stdafx.h" ).

1
2
3
4
5
6
7
8
9
10
//choice a
using namespace std;
//choice b
using std::cin;
using std::cout;
using std::endl;
//choice c
std::cout
std::cin
std::endl


These are all valid ways to use the components of the std namespace that you use.

Okay, functions need brackets.

1
2
3
4
5
6
7
8
9
void myfunc ();
void myfunc (int x, float y)
{
    //code here
}
int main ()
{
    //more code
}


NOT

1
2
3
4
5
6
void myfunc

int main
{
    //more code
}


As do if statements

1
2
3
4
if (a == 5)
std::cout<< "OMG! a is equal to 5?!";
else
std::cout<< "OMG! a doesn't equal five!?";


You also can not use a function as a condition in if statements (I think).

I'm not an expert in programming, and I have no knowledge of COD. With that said, my inexperienced opinion would be to get the user input, run a switch statement to see if the input is 'r' or 'R'. In the case that the user does hit r or R, magCount = whatever; ammo -=20.

Here's a ber simple program (using some of your code) that sets the magCount to 20 and the ammo count down 20 if the user hits 'r' or 'R':

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
#include <iostream> // No ".h"
using namespace std;

int main ()
{
	int magSize = 30;  // I never use this
	int magCount = 0; // I set it to 0 so we can see what it is before the user
                      //inputs his/ her input and afterwards as well to ensure it works
	int ammo = 90;     // The total amount of bullets
    char input;
    
    // Show the current numbers
    cout << "Bullets in mag: " << magCount << endl;
    cout << "Mag size: " << magSize << endl;
    cout << "Ammo left: " << ammo << endl;
    
    // Get the user input (how he/she knows to enter anything is beyond me
	cin>> input;
    
    // Go to a new line
    cout<< endl;
    
    // Checks user input
    switch (input)
    {
        case 'r':
            magCount = 20;
            ammo -= 20;
        break;
        case 'R':
            magCount = 20;
            ammo -= 20;
        break;
        default:
            //I left this blank for some reson
        break;
    }
    
    // Did it work?
    cout << "Bullets in mag: " << magCount << endl;
    cout << "Mag size: " << magSize << endl;
    cout << "Ammo left: " << ammo << endl;
    // It did for me
    
    // End the main function
	return 0;
}


Like I said, I'm no expert, so this is by not the greatest solution. Try re-reading what you already have from the tutorial and see if you can understand what it is you're trying to do, and how it is you are doing it wrong.

Hint:

Functions are on page 41. Don't aim to finish the tutorial in a week, try and read a section or two a day, understand most everything talked about, and play around with the examples a bit to better your understanding and usage of what is taught to you.
Last edited on
Topic archived. No new replies allowed.