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.
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.
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.
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. :/
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. :)
@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.
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.