| Ferrous Moon http://www.ferrousmoon.com:80/forums/ |
|
| Need help on small C++ program... http://www.ferrousmoon.com:80/forums/viewtopic.php?f=52&t=701 |
Page 1 of 2 |
| Author: | eddieringle [Mon Jun 12, 2006 4:59 pm ] |
| Post subject: | Need help on small C++ program... |
Hey all... haven't posted in awhile, but I have still been checking up on the site. : ) I have a little C++ project I was writing just for a little learning experience with C++. When I compile the code, it says that there is a syntax error with return. Here is the code... (don't bother making remarks about the point of the program, I just need a solution to this problem) Code:
// INFINITY
//Include the library file
#include <stdio.h>
#include <iostream>
using namespace std;
//Start the program
int main(int arg, char* pszArgs[])
{
cout << "This program was written by Eddie Ringle, and is suppose to simulate infinity.\n"
<< "The only way to destroy this infinite loop is to shut down the program.\n"
<< "So when you get tired, you know what to do... : )\n";
int infvalue;
do
{
//start the infinite loop process
infvalue = 0;
cout << "\n Start at 0\n";
//loop forever
int addvalue;
for(;;)
{
addvalue = 1;
//Add addvalue to infvalue
infvalue = infvalue + addvalue;
}
//Output
cout << "\n The new total is: "
<< infvalue
<< "\n";
}
return 0;
}
Thanks!
|
|
| Author: | Soldier of Light [Mon Jun 12, 2006 5:29 pm ] |
| Post subject: | |
Probably the for(;;) line, but I don't use C++ so I wouldn't know. If I were you I'd use while(1). |
|
| Author: | ChaosR [Tue Jun 13, 2006 7:07 am ] |
| Post subject: | |
I've written something like this yesterday Code: //forever, c++
#include <stdio.h>
#include <iostream.h>
int main()
{
unsigned int x;
for ( x = 0;; x++ ) {
cout << x << "\n";
}
}
just leave the test away, then it will continue as long as possible.actually, i made it for my new TI calculator in TI-basic and wondered how to do it in C++, so i tried, and that's the result. EDIT: Improved my C++ to an unsigned int, to run even longer EDIT2: infinity on TI-basic, this time, better: TI version: Code: name:forever
:0->X
:While 1
:X + 1 -> X
:Disp X
:End
|
|
| Author: | Soldier of Light [Tue Jun 13, 2006 7:23 am ] |
| Post subject: | |
while(1) While will continue as long as the condition is true. Since 1=true, it will never stop. |
|
| Author: | ChaosR [Tue Jun 13, 2006 7:29 am ] |
| Post subject: | |
a while is not really needed, in my TI, cuz it'll take days for my calculator to complete it, TI-basic is kinda slooooooooooooooooooooooooooooooooooow |
|
| Author: | Soldier of Light [Tue Jun 13, 2006 7:37 am ] |
| Post subject: | |
I'm well aware of that. Just pointing out that there is an infinity. |
|
| Author: | ChaosR [Tue Jun 13, 2006 7:39 am ] |
| Post subject: | |
btw, can you give me an example of an infinite increasing number program in TI-basic using a while(1) |
|
| Author: | eddieringle [Tue Jun 13, 2006 5:20 pm ] |
| Post subject: | |
That's not what I need... thanks for trying... but my problem is that the compiler thinks that return is a syntax error, when it is being used correctly. |
|
| Author: | BK [Tue Jun 13, 2006 6:10 pm ] |
| Post subject: | |
I think you are missing the While part of your do loop. e.g. Code: do{
...
}while(1);
return 0;
}
Good tip for all beginners: Often a compiler error is a problem on the line above where it thinks the problem is. |
|
| Author: | krageon [Wed Jul 05, 2006 8:16 am ] |
| Post subject: | |
Small Addition: Code: //loop forever
int addvalue = 1;
for(infvalue;;)
{
//Add addvalue to infvalue
infvalue = infvalue + addvalue;
cout << infvalue << endl;
}
|
|
| Author: | Burningmace [Fri Jul 07, 2006 9:28 am ] |
| Post subject: | |
The smallest and most simple code to do it is just: Code: void main() {
// Insert the stuff before the loop here.
do {
infvalue = infvalue + addvalue;
cout << infvalue << endl;
} while(1);
}
|
|
| Author: | prophile [Tue Jul 11, 2006 3:04 pm ] |
| Post subject: | |
Code: #include <iostream>
using namespace std;
int main ( int argc, char** argv )
{
static int num = 0;
cout << num << endl;
num++;
main ( argc, argv );
}
Recursion for the win!
|
|
| Author: | Tycho [Tue Jul 11, 2006 5:44 pm ] |
| Post subject: | |
Quote: Code: #include <iostream>
using namespace std;
int main ( int argc, char** argv )
{
static int num = 0;
cout << num << endl;
num++;
main ( argc, argv );
}
Recursion for the win! |
|
| Author: | VITAS [Tue Jul 11, 2006 5:54 pm ] |
| Post subject: | |
maybe thats the reason darwinia is so slow too (a t least last time i tried it) |
|
| Author: | Tycho [Tue Jul 11, 2006 6:27 pm ] |
| Post subject: | |
Quote: maybe thats the reason darwinia is so slow too (a t least last time i tried it)
It's likely.
|
|
| Page 1 of 2 | All times are UTC-05:00 |
| Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |
|