PDA

View Full Version : C++: Why is my function being bypassed?


Karim
03-16-2004, 01:23 AM
#include <iostream><IOSTREAM>
#include <cmath><CMATH>

int menu();
void operation(int, double&, double&, double&, double&, double&, double&);
double vector_verify(double, double, double);

int main()
{
int selection;
double lambdaAx, lambdaAy, lambdaAz, lambdaBx, lambdaBy, lambdaBz;

selection = menu();
operation(selection, lambdaAx, lambdaAy, lambdaAz, lambdaBx, lambdaBy, lambdaBz);

return 0;
}

void operation(int choice, double& Ax, double& Ay, double& Az,
double& Bx, double& By, double& Bz)
{
switch (choice)
{
case 1:
double dx1, dy1, dz1, dx2, dy2, dz2;
double lambda1x, lambda1y, lambda1z, lambda2x, lambda2y, lambda2z;
double distance1, distance2, vector1, vector2;

while (vector1 <= 0.8945 | vector1 >= 1.1055)
{
cout << endl << "FOR THE FIRST VECTOR" << endl;
cout << "Enter the distance in the x-direction: ";
cin >> dx1;
cout << "Enter the distance in the y-direction: ";
cin >> dy1;
cout << "Enter the distance in the z-direction: ";
cin >> dz1;

distance1 = dx1*dx1 + dy1*dy1 + dz1*dz1;
lambda1x = dx1/(sqrt(distance1));
lambda1y = dy1/(sqrt(distance1));
lambda1z = dz1/(sqrt(distance1));

vector1 = vector_verify(lambda1x, lambda1y, lambda1z);
}

while (vector2 <= 0.8945 | vector2 >= 1.1055)
{
cout << endl << "FOR THE SECOND VECTOR" << endl;
cout << "Enter the distance in the x-direction: ";
cin >> dx2;
cout << "Enter the distance in the y-direction: ";
cin >> dy2;
cout << "Enter the distance in the z-direction: ";
cin >> dz2;

distance2 = dx2*dx2 + dy2*dy2 + dz2*dz2;
lambda2x = dx2/(sqrt(distance2));
lambda2y = dy2/(sqrt(distance2));
lambda2z = dz2/(sqrt(distance2));

vector2 = vector_verify(lambda2x, lambda2y, lambda2z);
}

Ax = lambda1x; Ay = lambda1y; Az = lambda1z;
Bx = lambda2x; By = lambda2y; Bz = lambda2z;

//ignoring cases 2, 3 and 4 as I haven't gotten to them yet

break;
return;
}

double vector_verify(double x, double y, double z)
{
double check;

check = (x*x + y*y + z*z);

if (check <= 0.8945 | check >= 1.1055)
{
cout << endl << "Your input does not correspond to a vector.";
cout << endl << "Please try again..." << endl;
}

return check;
}



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.

Solecismic
03-16-2004, 01:34 AM
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.

Marc Vaughan
03-16-2004, 03:35 AM
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) ...

Karim
03-16-2004, 04:32 AM
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.

but it might just be that you're cleverer than me and meant to do that) ...
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.

Karim
03-16-2004, 04:53 AM
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.

Marc Vaughan
03-16-2004, 05:57 AM
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 ...


// ---------------------- //
// Standard Include Files //
// ---------------------- //
#include <iostream.h>
#include <math.h>

// ------------------- //
// Function Prototypes //
// ------------------- //
int menu( void );
void operation( int choice, double& Ax, double& Ay, double& Az,
double& Bx, double& By, double& Bz);
double vector_verify(double x, double y, double z);

// -----------------------------------------------------------------------
// Function: main()
// Purpose: Main function for the vector program.
// -----------------------------------------------------------------------
int main( void )
{
int selection;
double lambdaAx, lambdaAy, lambdaAz, lambdaBx, lambdaBy, lambdaBz;

selection = menu();
operation(selection, lambdaAx, lambdaAy, lambdaAz, lambdaBx, lambdaBy, lambdaBz);

return 0;

} // main()

// -----------------------------------------------------------------------
// Function: operation()
// Purpose: Performs the vector operation
// -----------------------------------------------------------------------
void operation( int choice, double& Ax, double& Ay, double& Az,
double& Bx, double& By, double& Bz )
{
double dx1 = 0.0;
double dy1 = 0.0;
double dz1 = 0.0;
double dx2 = 0.0;
double dy2 = 0.0;
double dz2 = 0.0;
double lambda1x = 0.0;
double lambda1y = 0.0;
double lambda1z = 0.0;
double lambda2x = 0.0;
double lambda2y = 0.0;
double lambda2z = 0.0;
double distance1 = 0.0;
double distance2 = 0.0;
double vector1 = 0.0;
double vector2 = 0.0;

switch( choice )
{
case 1:

while (vector1 <= 0.8945 || vector1 >= 1.1055)
{
cout << endl << "FOR THE FIRST VECTOR" << endl;
cout << "Enter the distance in the x-direction: ";
cin >> dx1;
cout << "Enter the distance in the y-direction: ";
cin >> dy1;
cout << "Enter the distance in the z-direction: ";
cin >> dz1;

distance1 = ( dx1 * dx1 ) + ( dy1 * dy1 ) + ( dz1 * dz1 );
lambda1x = dx1 / ( sqrt( distance1 ) );
lambda1y = dy1 / ( sqrt( distance1 ) );
lambda1z = dz1 / ( sqrt( distance1 ) );

vector1 = vector_verify( lambda1x, lambda1y, lambda1z );
}

while (vector2 <= 0.8945 || vector2 >= 1.1055)
{
cout << endl << "FOR THE SECOND VECTOR" << endl;
cout << "Enter the distance in the x-direction: ";
cin >> dx2;
cout << "Enter the distance in the y-direction: ";
cin >> dy2;
cout << "Enter the distance in the z-direction: ";
cin >> dz2;

distance2 = ( dx2 * dx2 ) + ( dy2 * dy2 ) + ( dz2 * dz2 );
lambda2x = dx2 /( sqrt( distance2 ) );
lambda2y = dy2 /( sqrt( distance2 ) );
lambda2z = dz2 /( sqrt( distance2 ) );

vector2 = vector_verify(lambda2x, lambda2y, lambda2z);
}

Ax = lambda1x; Ay = lambda1y; Az = lambda1z;
Bx = lambda2x; By = lambda2y; Bz = lambda2z;

break;
default:
break;
}

} // operation()


// -----------------------------------------------------------------------
// Function: vector_verify()
// Purpose: Validates that the vectors input were sensible and indicates
// a warning if not.
// -----------------------------------------------------------------------
double vector_verify( double x, double y, double z )
{
double check;

check = ( ( x * x ) + ( y * y ) + ( z * z ) );

if (check <= 0.8945 || check >= 1.1055)
{
cout << endl << "Your input does not correspond to a vector.";
cout << endl << "Please try again..." << endl;
}

return check;

} // vector_verify()

Marc Vaughan
03-16-2004, 05:58 AM
PS. Sorry for the anal reformatting & headers etc., force of habit .... just makes it easier for me to look at code.

3ric
03-16-2004, 07:19 AM
Memo to self: If any bugs are found in Marc's code for SI Games' Football Manager, blame Karim.


...



:D <small>/Just kidding)</small>

hukarez
03-16-2004, 08:28 AM
@Woo! Nice headers! ;)

Very pretty. :D

Mr. Wednesday
03-16-2004, 12:24 PM
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.

primelord
03-16-2004, 12:28 PM
PS. Sorry for the anal reformatting & headers etc., force of habit .... just makes it easier for me to look at code.
I wish everyone made a habit of that.

Raven
03-16-2004, 12:54 PM
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?

Coffee Warlord
03-16-2004, 02:24 PM
PS. Sorry for the anal reformatting & headers etc., force of habit .... just makes it easier for me to look at code.

I officially love you Marc. Someone who formats/indents {}'s properly. :)

MacroGuru
03-16-2004, 03:13 PM
Im still sitting here, with a tear in my eye, looking at the beauty...MV is a true artist!

sabotai
03-16-2004, 03:24 PM
I wish everyone made a habit of that.

Why do you think they call it code? :D

Buzzbee
03-16-2004, 03:32 PM
Oh, by the way, Karim thanks you all for doing his homework for him. :p

Karim
03-16-2004, 08:38 PM
PS. Sorry for the anal reformatting & headers etc., force of habit .... just makes it easier for me to look at code.
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.


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.
I know I can rely on you to track down my errors. :) Thanks for your help again.


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?
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.


Oh, by the way, Karim thanks you all for doing his homework for him. :pShhhhhhhh............. :)

Mr. Wednesday
03-16-2004, 09:02 PM
Oh, by the way, Karim thanks you all for doing his homework for him. :pFor my part, it certainly wasn't doing his homework for him. It was imparting wisdom that he will hopefully use again down the road.


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 &lt;iostream.h&gt;? If so, I recommend using &lt;iostream&gt; (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.

sabotai
03-16-2004, 09:25 PM
Taking this one off topic kind of (since the original question has been answered it would seem).

You're using cout, cin, and endl without a namespace qualifier and without either using them or using the std namespace.

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...

Marc Vaughan
03-17-2004, 03:21 AM
I wish everyone made a habit of that.
Heh - its part of the coding standard at SI, looking at unformatted code drives me mad ... not clever enough to work it out in my head automatically :)

Marc Vaughan
03-17-2004, 03:26 AM
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.
Cool by me :D

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.

Yossarian
03-17-2004, 03:54 AM
Originally Posted by primelord
I wish everyone made a habit of that.

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.

Mr. Wednesday
03-17-2004, 01:05 PM
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...The advantage of namespaces is that it eliminates problems with name collisions. This is particularly at issue when you consider pre-standard third-party libraries for stuff like vectors and lists -- with the the standard versions going into the std namespace, there are no issues there with name collisions.

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).

sabotai
03-17-2004, 02:54 PM
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'. :D )

Raven
03-17-2004, 03:21 PM
#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.

Mr. Wednesday
03-18-2004, 01:10 AM
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'. :D )That's correct. The big win for you using the standard iostreams is that you can extract into a standard string, rather than having to use char*'s. Plus the point that I already mentioned, I think Microsoft will stop supporting the classical iostreams library, if they haven't already done so.

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.

Karim
03-18-2004, 09:19 AM
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.Neither our instructor nor our text discusses namespace although cin/cout were among the first things taught. On a C++ forum I visit, namespace is one of the first things in the newbie faq, so the discussion above was very helpful.

I used &it <#iostream><IOSTREAM>. The compiler is g++ although I don't know what version.

Mr. Wednesday
03-18-2004, 10:32 AM
Don't forget that the board eats stuff between &lt; and &gt; (which I got up using the HTML escapes for them e.g. &amp; 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 &lt;iostream&gt;, then I'd guess it's 2.95, maybe? I think the standard library on that version still leaves a bit to be desired.

Glengoyne
03-18-2004, 05:57 PM
I officially love you Marc. Someone who formats/indents {}'s properly. :)
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:D.

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.

Coffee Warlord
03-18-2004, 06:44 PM
Geeze That was the only thing I didn't like about his reformatting.

Just try reading code with the { on the line of the call, nested about 20 times in. You'll be driven mad. :)

Glengoyne
03-18-2004, 10:03 PM
Just try reading code with the { on the line of the call, nested about 20 times in. You'll be driven mad. :)
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.

function x()
{
null;
}

Coffee Warlord
03-18-2004, 10:07 PM
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.

Glengoyne
03-18-2004, 10:31 PM
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.
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

Coffee Warlord
03-18-2004, 10:36 PM
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.

sterlingice
03-18-2004, 10:51 PM
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. :)
I like to stab people like you if I see code like that. :mad:

SI

Coffee Warlord
03-18-2004, 10:57 PM
I like to stab people like you if I see code like that. :mad:

SI

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. ;)

Karim
03-19-2004, 12:07 AM
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. ;)
At this point in my life, I eventually hope to be given the opportunity to try... :)

Mr. Wednesday
03-21-2004, 05:16 PM
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.
<pre>
void foo()
{
// ...
if (bar) {
// ...
}

if ( bar
|| baz)
{
// ...
}
}</pre>

Yossarian
03-22-2004, 04:12 AM
do y'all use anything like Hungarian Notation?

I feel like its something I SHOULD use but I don't.... like it.

sabotai
03-22-2004, 02:59 PM
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.

Glengoyne
03-22-2004, 03:51 PM
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.

sterlingice
03-22-2004, 03:53 PM
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.
Color me ignorant and curious. What's Hungarian notation?

SI

Glengoyne
03-22-2004, 03:58 PM
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

Marc Vaughan
03-22-2004, 06:29 PM
Hungarian notation?
I personally prefer not to use Hungarian notation upon projects where I have a say in the coding standard as I find it long-winded and cumbersome, however I have used it in the past and such things are largely a matter of personal preference.

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.

Mr. Wednesday
03-22-2004, 08:53 PM
do y'all use anything like Hungarian Notation?

I feel like its something I SHOULD use but I don't.... like it.I use it sparingly. The thing that I try to apply most consistently is prefixes to strongly indicate scoping (m_, g_). Beyond that... I'll usually stick a 'p' on pointers (including smart ones), an 'n' on integers, a 'd' on doubles, an 's' on normal strings, a bstr on BSTR-type strings, a 'c' on single characters. I don't generally go farther than that.

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.

Karim
03-23-2004, 12:17 AM
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... ;)

Mr. Wednesday
03-23-2004, 11:37 PM
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.