Disfunctional Function Declaration

Pages: 12
I am trying to figure out why this doesn't work. I thought I was doing it correctly. But the scope seems to be quirky.

It says the variables are not declared in their scope. :/
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
#include <iostream>

using namespace std;


void boxtotalmath();

int main(int argc, char *argv[]) {
//Green Box
	int box1 = 1;
	int GBox = 0;
//Blue Box
	int box1000 = 1000;
	int BBox = 0;
//Red Box
	int box100000 = 100000;
	int RBox = 0;
	bool gameExit = false;

	while(!gameExit) {
		char resp = 'z';
		cout << "Green Box Value: " << GBox << endl << "Blue Box Value: " << BBox << endl<< "Red Box Value: " << RBox << endl << endl;
		cout << "(a)dd 1" << endl << "(i)ncrease by 750" << endl << "(d)rop in 750000" << endl << "e(x)it" << endl;
		cout << "Response : ";
		cin >> resp;
		cout << endl<< endl<< endl;
		if(resp = 'a'|'A') {
			GBox = GBox + 1;
		} else if(resp = 'i'|'I') {
			GBox = GBox + 750;
		} else if(resp = 'd'|'D') {
			GBox = GBox + 750000;
		} else if(resp = 'x'|'X') {
			gameExit = true;
		} else {
			cout << "Authorized responses are a, i, d or x!" << endl;
		}
		boxtotalmath();

	}

	return 0;
}

void boxtotalmath() {

	while(GBox > 1000) {
		GBox = GBox - 1000;
		BBox = BBox + 1;
	}
	while(BBox > 100000) {
		BBox = BBox - 100000;
		RBox = RBox + 1;
	}
}
Last edited on
What is
javascript:PostPreview()
supposed to do?


Things like
if(resp = 'a'|'A')
don't do what you think they do. You probably meant
if ( resp == 'a' || resp == 'A' )
You have quite a lot of these to fix.


Your function boxtotalmath() has no means of accessing variables GBox, BBox, Rbox. You could probably pass them as (reference) arguments if you wished.
Last edited on
you need to review some basic syntax.
if(resp = 'x'|'X') ...
this is a 'dangerous' error because its perfectly legal. Some smart* compilers will warn you, but that is all, its just a warning.
lets translate it so you see:
if (resp is not zero after assigning it the value of x bitwise 'ORed' with X)
the single operators (| & ^ and ~) are bitwise, not logical. (or, and, xor, not)
the logicals are (||, &&, !) with the doubled up symbols where needed. (or, and, not)

then there is the math.
while(GBox > 1000) {
GBox = GBox - 1000;
BBox = BBox + 1;

this looks a LOT like a remainder. If this is what you intended, use the integer remainder and division:
so if gb is 5042
bb adds 5 and gb is 42
so gb is
gb %=1000; //5042 / 1000 is 5 with a remainder of 42.
and bb is
bb += gb/1000; //5042/1000 = 5 in integer math
you do NOT need the loop to do that manually, just make the 2 assignments.

on that note you can use operator and equals together to refer to the initial operand.
that is,
GBox = GBox - 1000;
can be said:
GBox -=1000;//exact same as above
and the ++ -- operators are also good shorthand:
RBox++; //same as RBox += 1 same as RBox = RBox+1

The hand rolled remainder is not wrong, its just unnecessary looping.
the shorthand operations are not wrong, but you need to embrace the shorter ones not just to make the code shorter but because other coders expect to read it that way.

The if statements are totally wrong, as noted above.
Last edited on
The javascript:PostPreview() made it's way in by an accident while trying to post. No idea why. :P

Thank you, lastchance. :)

jonnin,
I have a lot to look at from your response. When you used the word bitwise, I think I may have gone cross-eyed, for a second. I will be spending a lot of time looking at your post to figure out pretty much everything you said.

Thank you. :)

I do, however understand the issue I was having with the ||. So that helped the most.(So far as I understood your answers. I'm just coming back to learning to code. I think the standardization changed a bit since I last was able to continue my education. Self taught as I am.)
Thank you both.
its not that hard :)
a unsigned char is usually an 8 bit integer in c++: one byte.
so a bitwise 'or' of
0111
and
1100
is
1111

(0 or 1 is 1, 1 or 1 is 1, ... )
|| does it by value... zero is false, otherwise true for numeric types. || is called a logical operator (vs bitwise).

It is fairly rare to need bits in modern c++ code. On occasion they are handy for a more efficient algorithm, for example a prime number finder, but you do not require them most of the time and can stow them away to learn later after you have the more important things well understood.
Last edited on
I'm still having problems figuring out how to reference it in the right places. I feel like I am going to have to go back to 0 to relearn C++ because of what appears to be changes from '98 to '11 standards. :/
Modern C++ now is considered to be C++17, with C++20 already on the books and C++23 soon to arrive.

A good online and free tutorial site to relearn C++: Learn C++.
https://www.learncpp.com/

It's structured so you can dip in anywhere and get comfy without a lot of endlessly doing "Hello World!" exercises.
This is my go to site. It's where I learned what little I know...and partially forgot. Thank you, George P. :)
cppreference is a stone-cold excellent resource site, but it ain't for beginners.

https://en.cppreference.com/w/

C++ and C, lots of examples to muck around with, though they are fairly expert-ish in how they are written.
Sounds like a good place to explore code that is more likely to be written correctly. Is that a thing...correct coding? Lol. Thanks again.
Heh, most certainly "correct coding" is a thang.

So much code on the interwebs is.....to be polite....barely rising above being something in urgent need of being flushed.

Writing code, and crafting it well, is as much an art form as paint-by-the-numbers or erector set science.

I am not even Salieri compared to many here who are Mozart.
Paint by numbers...art. I won't argue that point. Everyone's entitled to their opinion. :P I'm going to check out the other site to see what's up. Don't tell Cplusplus I cheated on her. :/

Edit: I feel dirty. As usual the cheating wasn't worth it. Definitely a site for much later.
Nice site. Thanks for the info, George P. :)
Last edited on
@lare26 - please don't modify/update code after posting. Post a new thread with the updated code. Replies that refer to the original posted code now don't make sense and make it hard to follow the flow and the issues.
RE: high quality code (i.e., doing it correctly), a quote from Donald E. Knuth, a pioneer of computer science:

Computer programming is an art,
because it applies accumulated knowledge to the world,
because it requires skill and ingenuity,
and especially because it produces objects of beauty.
A programmer who subconsciously views himself as an artist
will enjoy what he does and will do it better.

As I said earlier, put a different way, I will never be a da Vinci or Michelangelo.

That doesn't mean I don't want to create C++ "art." It just won't be a masterpiece.

What I code is for my own enjoyment and edification, learning how to get a rock -- a computer is a very expensive rock (silicon chips, get it?) -- to do digital tricks.

One of the goals for learning C++, and learning it well, is to recreate an old BASIC Star Trek game I played back in public school on computer terminals, and tweak it with some ideas I have to "improve" the gameplay.

M'ok, so not exactly earth-shattering goals that will transform life as we know it and improve the Human Condition. Meh.
Trying to define into bite-sized concrete rules beforehand what is "correct code" is nigh um-possible, but with experience you'll recognize well-written code that is also elegant in style when you see it.
@OP,
here's the code from the post fixed - kind of, i.e. it compiles. Now it's up to you to check if it is correct.
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
#include <iostream>

using namespace std;

void boxtotalmath(int& GBox, int& RBox, int& BBox) {

	while(GBox > 1000) {
		GBox = GBox - 1000;
		BBox = BBox + 1;
	}
	while(BBox > 100000) {
		BBox = BBox - 100000;
		RBox = RBox + 1;
	}
}

int main() {
//Green Box
	int box1 = 1;
	int GBox = 0;
//Blue Box
	int box1000 = 1000;
	int BBox = 0;
//Red Box
	int box100000 = 100000;
	int RBox = 0;
	bool gameExit = false;

	while(!gameExit) {
		char resp = 'z';
		cout << "Green Box Value: " << GBox << endl << "Blue Box Value: " << BBox << endl<< "Red Box Value: " << RBox << endl << endl;
		cout << "(a)dd 1" << endl << "(i)ncrease by 750" << endl << "(d)rop in 750000" << endl << "e(x)it" << endl;
		cout << "Response : ";
		cin >> resp;
		cout << endl<< endl<< endl;
		if(resp == 'a'|| resp == 'A') {
			GBox = GBox + 1;
		} else if(resp == 'i' || resp == 'I') {
			GBox = GBox + 750;
		} else if(resp == 'd' || resp == 'D') {
			GBox = GBox + 750000;
		} else if(resp = 'x' || resp == 'X') {
			gameExit = true;
		} else {
			cout << "Authorized responses are a, i, d or x!" << endl;
                        continue;
		}
		boxtotalmath(GBox, RBox, BBox);
	}

	return 0;
}


BTW: I recommend to read the book "Clean Code" by Robert C. Martin. Clean code is easier to read, easier to debug a,d easier to extend.
@thmm, Amazon has available an entire "Clean" series by Robert Martin. Looking at what's available I'm adding them to my "to buy" list.

Thanks for the tip!

Here's Amazon's link for "Clean Code":
https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
@George P,
glad you find it useful.
Several reviewers slam the book for being too tightly coupled with Java. Well, if Java code isn't somewhat understandable if one knows C++ then the book would probably be useless. And the coder isn't all that great IMO.

I might not know Java, but I can look at some Java code and kinda get the gist without understanding all of what its doing.
Another good book about Clean Code is "Clean C++" by Stephan Roth.
Pages: 12