| OleFowdie (14) | |||
Ok this program is supposed to be a POS program in a paint shop. There's a section where I am using if else if selection/branching structures and the program is dumping everything into "other" on the end of day report screen before program termination.
| |||
|
|
|||
| Zaita (2301) | |||||||
Firstly, you need to fix your syntax. Your If-Else statements are almost un-readable. Placing the closing bracket at the end of a statement is not any known coding-standard I am aware of.return;} = BAD
Good Example:
Some other issues to point out. char sFirstName[16], sLastName[16], sState[3], cSalesDay, sFirstHigh[16], sLastHigh[16];All of these are fixed length. Yet at no point do you verify the length of input before putting it into these. If I enter a 5 letter state code, that should constitute a buffer overflow bug in your application. The next issue, is that you are not giving any default values to the above character arrays. Therefore when doing a check against sState == "ga" this can never be true because sState[2] == UNDEFINED. You need to set it to a proper value before use.I would also suggest once you have received your sState that you iterate through and use the toLower() function on it. This would save you having such large if (sState == "XX") functions.strcpy_s is not part of the ANSI C++ (that I am aware). For a safe strcpy function you should use strncpy().As a professional developer. I HIGHLY HIGHLY suggest that you use the string datatype instead of character arrays. They have built in allocation so you will not have to worry about buffer overflows etc. e.g
| |||||||
|
Last edited on
|
|||||||
| OleFowdie (14) | |
| Thanx for the tip, but C++ is not space/indent sensitive like Python or anything else. I use a method that works for me. I can change my code for posts, but in my actual source code documents on my machine won't change ;) | |
|
|
|
| guestgulkan (2831) | |
|
Ah I see - the problem is that sState is an array of chars . So for example in line 97 it is legal for you to say if ((sState == "ga") BUT YOU ARE DOING THE WRONG THING, because that means that you are comparing the ADDRESS of the array not it's contents, that is why the tests fails and all the stuff ends up in the 'other' bin.You need to use a string COMPARISON function. You can use the C function strcmp or as you are using c++ use one of the string functions to do this. | |
|
|
|
| Zaita (2301) | |||
Correct. But it is important (VERY) for your code to be maintainable. Even for assignments it's a good habbit to get into. If you presented that code to me in a portfolio for a job here; I would turn you down on the presentation of your code alone. That code to me, would be extremely difficult to maintain. You have to be aware that other people will be expected to work on your code should something happen to you. It's important to write code in a consistent manner that conforms to a known/common coding standard. (and by looking at your code, your convention is Hungarian notation.) The layout I posted above is the (Very common) K&R Method.
That's like saying "I don't need to wear a seatbelt, because the car runs without it plugged in". Sure it's true, but definitely not the best course of action. | |||
|
Last edited on
|
|||
| Zaita (2301) | |||
|
@guestgulkan: Correct :) Here is my sample modified to use a char array. Again, it's not safe because cin (AFAIK) doesn't check input length before assigning it.
| |||
|
|
|||
| OleFowdie (14) | |
|
Thank you both! I have been puzzling over this problem for quite a while and couldn't figure it out. Zaita - I understand what you are saying, but if I comment everything I ought to be ok, right? (I had to remove a lot of the commenting to make the program code fit into the posting area) | |
|
|
|
| Zaita (2301) | ||||||||
Nope. There is alot of fanboi-ism over different coding styles and namng conventions. Showing your ability to follow a standardized one correctly is definitely going to help you professionally. Comments are extremely important. Just don't over-comment also. You really should stick to 1 of the 2 major layout conventions for braces though. Way 1:
Way 2
Now, take your style and a few indentations and this is what we'd end up with.
That is almost un-followable without analysing each line individually. You have no ability to isolate blocks of logic mentally and determine the pattern of flow. Only because my IDE has strict Indentation does it actually help. | ||||||||
|
Last edited on
|
||||||||
| jsmith (5804) | |
|
IMHO, comments should explain why the code is doing what it is doing, not what it is doing. If it becomes necessary to explain what the code is doing because it is not obvious, then you should consider rewriting it to simplify. As Zaita points out, maintainability in the professional environment is very important --not only for others to understand what your code does, but also to help you remember what your code does months or years after you originally wrote it. (Take it from someone who has firsthand experience with that... code I wrote a couple of years ago seemed so simple at the time is much harder to understand once I've forgotten all the fine details). | |
|
|
|
| Faldrax (324) | |
|
Personally I find that it can be helpful to have comments for both what and why, but better still is to write code that keeps use fo comments to a minimum through careful choice of names for classes, methods, varaibles, etc. Comments can also be used to help section your code - but this is more relavant to functional rather than OO design. | |
|
|
|
| toshiro (51) | |||
Zaita, a quick question: Dev-C++ seems to use its own way, namely:
This is horrible to read, and I, myself, use the first way you described. I just thought it odd that an auto-indentation feature in an IDE should behave that badly (it even says it will indent to the first non-whitespace char of the preceding line). It just led me to turning off auto-indentation :/ | |||
|
|
|||
| mikeb570 (188) | |
I don't know what Dev-C++ you're using but mine seems to work fine. For me, lines 7, 11, and 12 would have gone to the right places. More precisely when you press } it jumps back a tab. I have smart tabs off though which is on by default, so maybe that's why.Edit, I just tried it and with smart tabs and auto indent on it acts funny. But works fine with smart tabs off and auto indent on. Anyways it's not perfect, but if it saves a keypress 85% of the time I think it's worth it. | |
|
Last edited on
|
|
| toshiro (51) | |
| Hmm, I did what you suggested. Thanks a bunch, it works fine now, as you said... but I'm not going to check the rest of my code (half of which is neither written by me, nor in C++, but C). | |
|
|
|