Uh, no. Let me clear some of this up for you.
C is a low-level language, meaning it works the same however you indent it.[/b]
All very true, and the same can be said for C++, but code is always read more times than it is written. The next person to see your code will not be able to see the structure of your program easily with for blocks etc if you do not indent in. The compiler is very loose and ultimately the code loses its indentation when it is compiled, but this does not mean that I can read machine code. Always indent it, it's much easier to read: it's easier to get into the habit now. I understand it Firefox did something weird.
And to answer your original question, it's because my compiler requires each function to be defined before it encounters it; in composite(), my compiler doesn't know what main() is yet, so I just have to define it before using it. I wasn't calling it twice.[/b]
You were. I don't believe your program should compile cleanly (or perhaps even at all). Apart from having recursion in your program that would cause a stack overflow if used too much, you have an illegal definition of composite() - you have said it returns an integer value when it does not return anything. If this compiled, it would have surely made a warning.
Also, I feel that if I copy a program, I might as well just download it. You misunderstood my intent with this program; it's to learn, not to create an uber omgI'llfindthelargestprimenumberinzahworld program. Using someone else's code doesn't help you learn. I'm not looking for the best way.[/b]
Uh. You can learn a lot from other people's code - I find learning by example one of the best ways to learn something. It's entirely possible to go it alone and end up making something that gets a value in 100 lines of code when there is a much more elegant solution available. I understand the point was to learn something by doing, here, and you've done so, but your program does not run and would not compile.
A rundown of the problems:
Code:
#include <iostream>
using namespace std;
float x;
float y;
int z;
int main ();
int composite ()
{
****cout << "\nCOMPOSITE";
********main ();
}
int main()
{
****cout << "\n\n-PRIME-\nEnter a number to determine if it is prime.\n";
****cin >> x;
****
****if ( x <= 0 && x != -1 )
********composite ();
****for ( z = 2; z <= ( x / 2 ); ++z )
****{
********for ( y = 2; y <= ( x / 2 ); ++y )
********{
************if ( z == ( x / y ) )
****************composite ();
********}
****}
****
****cout << "\nPRIME";
****
****main ();
}
Code:
#include <iostream>
using namespace std;
This is fine. Great, in fact, that you're using the STL.
Code:
float x;
float y;
int z;
Bad news: you're defining global variables. Most people would consider this the Antichrist, these should be defined in a more local scope - main() itself would be fine for is. Moving these declarations into main() would do you a better service. Because you declare them here they are not initialised which is another bad point and could cause your program to crash/be more insecure than it needs to be.
Code:
int composite ()
{
****cout << "\nCOMPOSITE";
You say this function returns an int. It doesn't, a simple mistake, just add return 0; if you want to use this function.
Why are you calling main again? This will cause a stack overflow very quickly if your program is used too much, and you should never call main more than once (contrary to your insistence, you are calling it more than once). Your function should return 0; and then main should loop again. Else the call stack will grow and grow.
Then in main():
Code:
****main ();
}
You call main () again when you should be looping it, causing the same problem and making your code nigh on impossible to follow.
So your code does not actually compile, although like we've just gone over, some problems that don't cause compiler errors still cause your code to be very hard to follow. Compiling your program in VC gets me this:
------ Build started: Project: Prime, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\users\admin\projects\prime& #092;prime\main.cpp(14) : error C4716: 'composite' : must return a value
c:\users\admin\projects\prime& #092;prime\main.cpp(38) : warning C4717: 'main' : recursive on all control paths, function will cause runtime stack overflow
Build log was saved at "file://c:\Users\Admin\Projects\Prime& #092;Prime\Debug\BuildLog.htm"
Prime - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[/b]
I think that more or less proves my point.
A better program using your code would look like this:
Code:
#include <iostream>
using namespace std;
int main()
{
****float x = 0;
****float y = 0;
****int z = 0;
****/* Realise that 'done' will never become true and that you will never actually end this program in this state because you
****** are using cin as the input instead of any other method. Win32 API would use WM_CLOSE messages for example, to set done
****** to true. Therefore the program will loop forever but it since it never calls any other functions it will never crash
****** or overflow. */
****bool done = false, composite = false;
****while ( !done )
****{
********cout << "\n\n-PRIME-\nEnter a number to determine if it is prime.\n";
********cin >> x;
********/* We don't know at the start. So it isn't. */
********composite = false;
****
********if ( x <= 0 && x != -1 )
********{
************cout << "Composite!";
************composite = true;
************/* It's easy enough to break here. */
************continue;
********}
********for ( z = 2; z <= ( x / 2 ); ++z )
********{
************for ( y = 2; y <= ( x / 2 ); ++y )
************{
****************if ( z == ( x / y ) )
****************{
********************cout << "Composite!\n";
********************composite = true;
********************break;
****************}
****************/* Horrible. The way the program is structured requires this. */
****************if ( composite )
********************break;
************}
************/* Also horrific. Don't do this: it's the only way to stop the program from spewing "composite!" over and over
************** again the amount of times the condition is true. */
************if ( composite )
****************break;
********}
****
********if ( !composite )
************cout << "\nPrime!";
****}
****
****/* '0' is the convention for "success" in C++ style programs. It probably does not matter here, but
****** we return it anyway. */
****return 0;
}
That works. You will notice:
1. I removed the composite() function. I didn't see the point on it in this case: it's called only twice and two cout statements could do just as fine. Encapsulating your code is important, but in this case was moot.
2. The code does not cause a stack overflow on runtime, which is important else the program would crash.
3. There is an ugly use of boolean in there to stop the program from spewing out the wrong message. It works, which is great, but the way the program was structured was kind of hard to do any other way (the continue keyword in C++ doesn't support continue all the way up the loop like PHP does). I didn't want to rewrite your entire program as you don't feel you'd learn a lot from that hence the rather nasty looking solution of guard clauses.
4. It compiles.
Either way, that should help.
|
|
Bookmarks