![]() |
|
|
#1 | ||
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
C++: Why is my function being bypassed?
Code:
vector_verify() was working well until I tried to implement pass by reference in operation(). The reason I wanted to do this is because I wanted the 6 components of the vectors to end up in main() so I could then pass them onto another function to do some calculations rather than have everything build up in operation(). The six components are correctly being returned to main as I wanted but now it seems vector_verify is never called in the while loop. Everything else in the loop works fine. I'm not sure what is going on. Any help would be greatly appreciated. Last edited by Karim : 03-16-2004 at 01:24 AM. |
||
|
|
|
|
|
#2 |
|
Solecismic Software
Join Date: Oct 2000
Location: Canton, OH
|
Hard to say, not knowing the exact syntax of the compiler. All that jumps out at me is the use of | instead of || for the "or" clauses.
|
|
|
|
|
|
#3 |
|
SI Games
Join Date: Oct 2000
Location: Melbourne, FL
|
I notice that:
<< double dx1, dy1, dz1, dx2, dy2, dz2; double lambda1x, lambda1y, lambda1z, lambda2x, lambda2y, lambda2z; double distance1, distance2, vector1, vector2; >> Doesn't initialise any variable values, I haven't put the code into a compiler but at first glance it doesn't appear that the values used in the while loop are initialised - potentially with unusual consequences? ... I'm also somewhat dubious about the | instead of a || in various parts of the code (but it might just be that you're cleverer than me and meant to do that) ... |
|
|
|
|
|
#4 | |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Right. Using | instead of || was just a mistake on my part but it still compiled. I'm using G++.
As far as not initialising, if I initialised all the variables to lets say 0, I can no longer compile; I get the following error: In function `void operation (int, double &, double &, double &, double &, double &, double &)': vectors.cpp:104: jump to case label vectors.cpp:61: crosses initialization of `double vector2' vectors.cpp:61: crosses initialization of `double vector1' vectors.cpp:61: crosses initialization of `double distance2' vectors.cpp:61: crosses initialization of `double distance1' vectors.cpp:60: crosses initialization of `double lambda2z' vectors.cpp:60: crosses initialization of `double lambda2y' vectors.cpp:60: crosses initialization of `double lambda2x' vectors.cpp:60: crosses initialization of `double lambda1z' vectors.cpp:60: crosses initialization of `double lambda1y' vectors.cpp:60: crosses initialization of `double lambda1x' vectors.cpp:59: crosses initialization of `double dz2' vectors.cpp:59: crosses initialization of `double dy2' vectors.cpp:59: crosses initialization of `double dx2' vectors.cpp:59: crosses initialization of `double dz1' vectors.cpp:59: crosses initialization of `double dy1' vectors.cpp:59: crosses initialization of `double dx1' The values are being stored properly inside the loop and being returned properly to main(). However, even if I enter crazy values, vector_verify() doesn't engage to let me know that I need to re-enter the data. If all else fails, I suppose I could forget about passing the values back to main and just call another function after the loops are done. I wanted to avoid that because operation() just becomes too messy. Quote:
Not at all. I'm a complete newbie here. But in January my skill set was limited to "Hello world" so I think I'm making progress. Thanks for your help. |
|
|
|
|
|
|
#5 |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Well, I figured out the error although I don't really understand why...
It turns out that I cannot include the calculations: distance1 = dx1*dx1 + dy1*dy1 + dz1*dz1; distance2 = dx2*dx2 + dy2*dy2 + dz2*dz2; inside the while loop. If anyone can explain why it wouldn't work the other way, it would be greatly appreciated. |
|
|
|
|
|
#6 |
|
SI Games
Join Date: Oct 2000
Location: Melbourne, FL
|
I had five minutes while waiting for FM to build so I chucked your code snippet into the compiler and made it compile cleanly under VC++ - couldn't run it without the rest of your code, but thought it might help ...
Code:
|
|
|
|
|
|
#7 |
|
SI Games
Join Date: Oct 2000
Location: Melbourne, FL
|
PS. Sorry for the anal reformatting & headers etc., force of habit .... just makes it easier for me to look at code.
|
|
|
|
|
|
#8 |
|
College Starter
Join Date: Dec 2000
Location: Sweden
|
Memo to self: If any bugs are found in Marc's code for SI Games' Football Manager, blame Karim.
... /Just kidding)
__________________
San Diego Chargers (HFL) - Lappland Reindeers (WOOF) - Gothenburg Giants (IHOF) Indiana: A TCY VC - year 2044 - the longest running dynasty ever on FOFC! |
|
|
|
|
|
#9 |
|
College Starter
Join Date: Dec 2003
Location: Chula Vista, CA
|
@Woo! Nice headers!
![]() Very pretty. ![]()
__________________
...what we have here is a man who looks like Tarzan, but fights like Jane! My VG collection | Xbox 360 Gamertag: ManThol | PS3 Network ID: hukarez Doce Pares International - San Diego Council Filipino Martial Arts Digest tweet tweet twitter |
|
|
|
|
|
#10 |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
Turn on all warnings, and it should complain to you about using vector1 before it is initialized. Marc's VC version fixes the problem by initializing it to 0.0.
You should declare your variables outside the switch statement, or wrap the case block in a pair of braces, in order to avoid the error it was giving when you tried to inialize on declaration. Keep in mind that a case statement is basically just a target label for a goto statement (implicit in the "switch"), it doesn't create a "real" block of code. |
|
|
|
|
|
#11 | |
|
Pro Rookie
Join Date: Oct 2000
|
Quote:
I wish everyone made a habit of that. |
|
|
|
|
|
|
#12 |
|
College Prospect
Join Date: Oct 2000
Location: Baltimore, MD
|
some things I noticed...
I don't think you can declare variables inside of a switch statement. That's probably where you are getting all those cross initialization errors. Use mark's code and declare the variables before the switch. Instead of using all those lines of code for initialization, you could just put them on the same line like this.... double dx1 (0.0), dy1 (0.0), dz1 (0.0); // etc... or double dx1 = 0, dy1 = 0, dz1 = 0; // same as above, but slightly diff. syntax Your project is kind of ambiguous, so it's tough for me to actually try and debug. The best thing you can probably do is put in priming reads every time you calculate something new. That way you can check what your variables are every time they change. If you know what you are looking for, you can track down your error(s) this way. distance1 = ( dx1 * dx1 ) + ( dy1 * dy1 ) + ( dz1 * dz1); cout << "in first while loop distance1 is " << distance1 << endl; vector1 = vector_verify( lambda1x, lambda1y, lambda1z ); cout << "vector 1 is " << vector1 << " after vector_verify" << endl; Also use a priming read in vector_verify() if you don't think it is being called. Simply putting cout << "hi" << endl; inside the function, you will see whether it is called or not. ps....this seems to be a pretty tough program for an absolute newbie. Is this for a class, or are you getting this from a book and doing it on your own? |
|
|
|
|
|
#13 | |
|
Head Coach
Join Date: Oct 2002
Location: Colorado Springs
|
Quote:
I officially love you Marc. Someone who formats/indents {}'s properly. ![]() |
|
|
|
|
|
|
#14 |
|
Coordinator
Join Date: May 2003
Location: Utah
|
Im still sitting here, with a tear in my eye, looking at the beauty...MV is a true artist!
__________________
"forgetting what is in the past, I strive for the future" |
|
|
|
|
|
#15 | |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
Quote:
Why do you think they call it code? ![]() |
|
|
|
|
|
|
#16 |
|
College Starter
Join Date: Jun 2002
|
Oh, by the way, Karim thanks you all for doing his homework for him.
![]()
__________________
Ability is what you're capable of doing. Motivation determines what you do. Attitude determines how well you do it. - Lou Holtz Last edited by Buzzbee : 03-16-2004 at 03:32 PM. |
|
|
|
|
|
#17 | ||||
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Quote:
Thanks for this MV. If you don't mind, I think I'm going to adopt this style as I think it will make things easier as I progress onto more difficult things. Quote:
I know I can rely on you to track down my errors. Thanks for your help again.Quote:
The class is supposed to be an introduction to C++ for engineers and scientists. The actual program is meant to calculate the angle between a pair of vectors depending upon whether the input data are distances, direction angles or direction cosines. Quote:
![]() |
||||
|
|
|
|
|
#18 | |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
Quote:
Oh, and something else it appears you missed -- You're using cout, cin, and endl without a namespace qualifier and without either using them or using the std namespace. You didn't say which compiler you were using, but any self-respecting C++ compiler ought to squawk about not knowing what cin, cout, or endl are. Edit: Or else maybe you're including <iostream.h>? If so, I recommend using <iostream> (note the lack of extension) (the biggest reason for this is that it supports strings natively, as opposed to classical iostreams that only support char* strings), but until you do so, disregard the rest of this message since the classical iostreams came about before namespaces and thus dump everything into the global namespace. This is easily fixed -- just add the following line at the top of your file, after the includes and before any of the code: using namespace std; As a matter of personal style, I rarely "use" a namespace, particularly not globally, but it won't hurt anything as long as you don't do it in a header file (don't ever do it in a header file). Usually, I just use the qualifier in my code, on occasion I'll "use" frequently-used symbols, on rare occasions I'll use a full namespace within a function or block scope. Last edited by Mr. Wednesday : 03-16-2004 at 09:06 PM. |
|
|
|
|
|
|
#19 | |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
Taking this one off topic kind of (since the original question has been answered it would seem).
Quote:
I've seen this brought up before but never really paid attention to it. Why should people use namespace? What are the advantages? I always just included the file with the extension and went on from there... |
|
|
|
|
|
|
#20 | |
|
SI Games
Join Date: Oct 2000
Location: Melbourne, FL
|
Quote:
![]() |
|
|
|
|
|
|
#21 | |
|
SI Games
Join Date: Oct 2000
Location: Melbourne, FL
|
Quote:
![]() As with many things there is no 'right' way to format code, over time you've find a layout which works for you and which you find easiest to read. Indenting is the most important thing imho as this allows you to easily visually tell which parts of a function are related to particular if/switch/while statements. I also advocate headers, decent commenting (I didn't add comments to your functions because they're relatively straight forward) and function/file headers. I've also got into the habit of putting the function name on the closing brace for a function as I find this helps when rummaging around in large code file. In case you're interested I'd be happy to email you the coding standard I use, you can see a lot of it from the reformatting of your program - but some things aren't visible from a simple code snippet - your call entirely, as I indicated earlier htere is no 'right' way to code. |
|
|
|
|
|
|
#22 | |
|
High School Varsity
Join Date: Jul 2003
|
Quote:
See... I have mixed opinions on this. Nothing pisses me off more (well some things do) than crappy unformatted, uncommented code. But one thing that DOES piss me off more is when everybody on a project (most notably open source projects with no coding standards) adopts their own style and everytime they work on a file, they reformat it to suit themseleves. |
|
|
|
|
|
|
#23 | |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
Quote:
With the iostreams, there are a couple of big reasons to use standard rather than classical iostreams. The first is standard C++ string support in the standard iostreams (rather than char* strings, which are kludgy and error-prone). The second is that support for classical iostreams is waning -- I believe Microsoft no longer provides the classical iostream library as of VC 7.1, and if they still do supply it, I would not be surprised if they pulled it from VC 8 (aka VC 2005, aka Whidbey). |
|
|
|
|
|
|
#24 |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
So, to be clear on this I guess...
if I use: #include (iostream.h) (replace with proper notation) I will be including the classical versions? (BTW, I'm using VC++ 6). But if I use: #include (iostream) using namespace std I will be using the standard? (Sometimes it takes me awhile to 'get it'. ) |
|
|
|
|
|
#25 |
|
College Prospect
Join Date: Oct 2000
Location: Baltimore, MD
|
#include iostream (surrounded by greater than and less than signs) is the new ANSI compliant standard, and requires "using namespace std;" to be declared globally.
#include iostream.h is an older un-revised library, and doesn't require the using namespace std; You can create your own namespaces and use them. std is just one that is written for you. I believe it just uses operator overloading to define cin, cout, endl etc.. If you wrote a namespace called Space1 you could declare it using "using namespace Space1;" - this would only be used within it's scope if you declared it within the program, or would be declared globally if you declare it before main. by declaring using namespace std; before main, anytime you use cin cout or endl (not within the scope of say Space1) you resort back to the global defined namespace (std). Hope that made sense. The other guys can correct me if I made a mistake somewhere. Last edited by Raven : 03-17-2004 at 03:25 PM. |
|
|
|
|
|
#26 | |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
Quote:
I use VC6 at work. I have both VC6 and VC7.1 at home. Edit: Raven, I didn't see anything wrong in your explanation. Last edited by Mr. Wednesday : 03-18-2004 at 01:11 AM. |
|
|
|
|
|
|
#27 | |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Quote:
I used &it <#iostream> Last edited by Karim : 03-18-2004 at 09:11 PM. Reason: removed html tags |
|
|
|
|
|
|
#28 |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
Don't forget that the board eats stuff between < and > (which I got up using the HTML escapes for them e.g. & lt; [without the space in between]).
Anyway, if you're using g++ and it's letting you use cin and cout without namespace qualification from <iostream>, then I'd guess it's 2.95, maybe? I think the standard library on that version still leaves a bit to be desired. Last edited by Mr. Wednesday : 03-18-2004 at 10:33 AM. |
|
|
|
|
|
#29 | |
|
Grizzled Veteran
Join Date: Sep 2003
Location: Fresno, CA
|
Quote:
Geeze That was the only thing I didn't like about his reformatting. I used to code with like that function x() { null; } Just because it seemed easier to make sure my braces were matched. Then I discovered this was the grounds for a debate of religious proportions. I looked at it, and decided that if the guys who wrote the book on C. Kernighan and Ritchie were adamant that the starting curly brace belonged on the line declaring the function, that maybe it wouldn't be such a bad thing. so I started doing it like function x(){ null; } It took a couple of months for me to get used to the switch, but now the way I used to code is clearly wrong .Regarding Yossarian's bit about everybody reformatting things to their own standards. I require that my team employs a uniform standard. Since a lot of the code they are maintaining was written by yours truly, it only makes sense that that standard be mine. There are very few things a developer can do that pisses me off more than unindented code. |
|
|
|
|
|
|
#30 | |
|
Head Coach
Join Date: Oct 2002
Location: Colorado Springs
|
Quote:
Just try reading code with the { on the line of the call, nested about 20 times in. You'll be driven mad. ![]() |
|
|
|
|
|
|
#31 | ||
|
Grizzled Veteran
Join Date: Sep 2003
Location: Fresno, CA
|
Quote:
Actually, I think the nesting 20 times alone would drive me mad. I just went back and saw my original post. I am so anal about indenting, that the editor moving my "null" is really driving me nuts. Quote:
|
||
|
|
|
|
|
#32 |
|
Head Coach
Join Date: Oct 2002
Location: Colorado Springs
|
Heh. Remind me to show you my latest application sometime. Last check, there's parts that nest at least 12 levels deep. And I can't do a damned thing about it to simplify it.
![]() On a side note, I feel kinda cool. That particular app, a healthcard admissions thing (which I wrote about 90% of the code), was demoed today at Cedar Sinai Hospital, and was a hit, from what I told. |
|
|
|
|
|
#33 | |
|
Grizzled Veteran
Join Date: Sep 2003
Location: Fresno, CA
|
Quote:
Very nice about the demo. Regarding "I can't do a damned thing about it.", I have a story. About ten years ago I wrote a data loading system, it was an ugly assignment, one of those "we need this yesterday if we are to stay in business" things. Well two weeks later the system was in place. I was reasonably happy with it, but it was obviously lacking because it was very operator intensive. The requirements were rather complex, and I had an "if-else ladder" that nested pretty darned deep. It had 28 distinct outcomes. I tried and tried, but couldn't pare it down. With the time crunch I just pressed on. Over the years the code was in production, I looked with trepidation at maintaining that section of code. It was akin to voodoo. That company was purchased, and the platform eventually scrapped. Less than a week after it was scrapped, along with my job, I went back to work for the fellow who had originally built up and sold the old company. I actually got the opportunity that all developers dream about. I got to build the whole application a second time. That particular "data loading" module included. This time around, I was given six weeks to do the job. The decision tree that I couldn't pare down below 28 brances now has 7. Talk about embarrassing. NOTE: I am not telling you this story to say I think you are wrong. Just that it's best never to say never |
|
|
|
|
|
|
#34 |
|
Head Coach
Join Date: Oct 2002
Location: Colorado Springs
|
Oh I agree, and if I had the time to rewrite it, (and I want to anyway), I could prolly pare it down a bit.
But, it works, it's ugly, yet elegant, and it's my ticket to job security until I flat out quit in a few months. (I despise my job.) ![]() edit: Yes, I know, writing a really complex app that only I understand and planning to jump ship soon is a very assholish thing to do. Believe me, the fuckers deserve it. Worthless hellhole of a job. Last edited by Coffee Warlord : 03-18-2004 at 10:38 PM. |
|
|
|
|
|
#35 | |
|
Hall Of Famer
Join Date: Apr 2002
Location: Back in Houston!
|
Quote:
I like to stab people like you if I see code like that. ![]() SI
__________________
Houston Hippopotami, III.3: 20th Anniversary Thread - All former HT players are encouraged to check it out! Janos: "Only America could produce an imbecile of your caliber!" Freakazoid: "That's because we make lots of things better than other people!" |
|
|
|
|
|
|
#36 | |
|
Head Coach
Join Date: Oct 2002
Location: Colorado Springs
|
Quote:
Try building a healthcare admissions application that validates patient information, has approximatly 1200 dynamically controlled fields that store various bits of patient records, that one can alter what fields can be entered where, when, how, in what format, etc, etc, etc...without entering Recursion From Hell. ![]() |
|
|
|
|
|
|
#37 | |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Quote:
At this point in my life, I eventually hope to be given the opportunity to try... ![]() |
|
|
|
|
|
|
#38 |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
My personal preferance is to put function opening braces on the next line, and block statement opening braces on the same line EXCEPT WHEN the initial statement takes multiple lines, in which case I will also put the opening brace on the next line.
e.g.
Last edited by Mr. Wednesday : 03-21-2004 at 05:18 PM. |
|
|
|
|
|
#39 |
|
High School Varsity
Join Date: Jul 2003
|
do y'all use anything like Hungarian Notation?
I feel like its something I SHOULD use but I don't.... like it. |
|
|
|
|
|
#40 |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
Not really. I use "common sense" names for my variables. Generally I don't have to look at a variable and say "I wonder if that is a float or an integer". I give it a name like StadiumAttendance. You can't have a float, so I know it's an int.
Now, if I ever work in a group, I would probably like it if there was some standard we used in naming variables. But for my personal projects, I don't bother unless I think I'm going to need an indication of what it is. |
|
|
|
|
|
#41 |
|
Grizzled Veteran
Join Date: Sep 2003
Location: Fresno, CA
|
I do actually use a trimmed down version of hungarian notation. I selectively use the first two letters of a variable name to indicate the datatype. Selective because I only do it if it seems questionable. I always identify variables I have passed into a function or procedure using similar notation.
|
|
|
|
|
|
#42 | |
|
Hall Of Famer
Join Date: Apr 2002
Location: Back in Houston!
|
Quote:
Color me ignorant and curious. What's Hungarian notation? SI
__________________
Houston Hippopotami, III.3: 20th Anniversary Thread - All former HT players are encouraged to check it out! Janos: "Only America could produce an imbecile of your caliber!" Freakazoid: "That's because we make lots of things better than other people!" |
|
|
|
|
|
|
#43 |
|
Grizzled Veteran
Join Date: Sep 2003
Location: Fresno, CA
|
Google is your friend. And mine because it would be really hard to explain, beyond a naming convention that seeks to identify the datatypes of the variable being named. It actually goes well beyond that.
http://web.umr.edu/~cpp/common/hungarian.html |
|
|
|
|
|
#44 | |
|
SI Games
Join Date: Oct 2000
Location: Melbourne, FL
|
Quote:
There is imho no 'right' notation or layout to code, the most important thing imho is to determine a clean style which is applied consistently to a program. |
|
|
|
|
|
|
#45 | |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
Quote:
Also, I do keep Hungarian out of the public interface as much as I can, since it's excess clutter in Visual Studio's automatic assistance features. |
|
|
|
|
|
|
#46 |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Do any of you find advanced calculus to be an aid in algorithm design? Or would this be more limited to scientific and engineering applications?
Just trying to get motivated... ![]() |
|
|
|
|
|
#47 |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
I don't know... I've taken advanced calculus, being an engineer by training, but I don't think it's really done anything for my design skills. I may be an exception, though... the only thing I really think has done anything for my design skills is the programming classes I took in school and a lot of experience. Even when I took a class in logic, I generally found that it was all pretty self-evident; I already thought that way.
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|