Last visit was: It is currently Sat Sep 23, 2023 3:23 pm


All times are UTC-05:00




Post new topic Reply to topic  [21 posts ] 
Author Message
 Post subject:Need help on small C++ program...
PostPosted:Mon Jun 12, 2006 4:59 pm 
User avatar
 

Joined:Sun Feb 12, 2006 8:56 pm
Posts:1019
Website:http://eddieringle.com
Location:Detroit, MI
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!

_________________
-- Eddie Ringle

Check out Elysian Shadows and consider backing us on Kickstarter!

====================================

Image


Last edited by eddieringle on Fri Aug 14, 2009 10:00 pm, edited 1 time in total.

Top
Offline  
 Post subject:
PostPosted:Mon Jun 12, 2006 5:29 pm 
 

Joined:Thu Apr 28, 2005 3:57 pm
Posts:175
AOL:DKWiz214
Probably the for(;;) line, but I don't use C++ so I wouldn't know.

If I were you I'd use while(1).

_________________
The light shall be your demise.


Top
Offline  
 Post subject:
PostPosted:Tue Jun 13, 2006 7:07 am 
External Project Staff
User avatar
 

Joined:Sun Oct 30, 2005 3:40 pm
Posts:371
Website:http://idlesoft.net
Location:~/
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

_________________
-- ChaosR

Image


Last edited by ChaosR on Sun Dec 30, 2007 1:30 pm, edited 7 times in total.

Top
Offline  
 Post subject:
PostPosted:Tue Jun 13, 2006 7:23 am 
 

Joined:Thu Apr 28, 2005 3:57 pm
Posts:175
AOL:DKWiz214
while(1)
While will continue as long as the condition is true. Since 1=true, it will never stop.

_________________
The light shall be your demise.


Top
Offline  
 Post subject:
PostPosted:Tue Jun 13, 2006 7:29 am 
External Project Staff
User avatar
 

Joined:Sun Oct 30, 2005 3:40 pm
Posts:371
Website:http://idlesoft.net
Location:~/
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

_________________
-- ChaosR

Image


Top
Offline  
 Post subject:
PostPosted:Tue Jun 13, 2006 7:37 am 
 

Joined:Thu Apr 28, 2005 3:57 pm
Posts:175
AOL:DKWiz214
I'm well aware of that. Just pointing out that there is an infinity.

_________________
The light shall be your demise.


Top
Offline  
 Post subject:
PostPosted:Tue Jun 13, 2006 7:39 am 
External Project Staff
User avatar
 

Joined:Sun Oct 30, 2005 3:40 pm
Posts:371
Website:http://idlesoft.net
Location:~/
btw, can you give me an example of an infinite increasing number program in TI-basic using a while(1)

_________________
-- ChaosR

Image


Top
Offline  
 Post subject:
PostPosted:Tue Jun 13, 2006 5:20 pm 
User avatar
 

Joined:Sun Feb 12, 2006 8:56 pm
Posts:1019
Website:http://eddieringle.com
Location:Detroit, MI
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.

_________________
-- Eddie Ringle

Check out Elysian Shadows and consider backing us on Kickstarter!

====================================

Image


Top
Offline  
 Post subject:
PostPosted:Tue Jun 13, 2006 6:10 pm 
 

Joined:Tue Jun 13, 2006 6:07 pm
Posts:1
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.

;-)


Top
Offline  
 Post subject:
PostPosted:Wed Jul 05, 2006 8:16 am 
External Project Staff
 

Joined:Mon Oct 31, 2005 2:40 pm
Posts:62
Small Addition:
Code:
//loop forever int addvalue = 1; for(infvalue;;) { //Add addvalue to infvalue infvalue = infvalue + addvalue; cout << infvalue << endl; }


Top
Offline  
 Post subject:
PostPosted:Fri Jul 07, 2006 9:28 am 
User avatar
 

Joined:Mon Mar 07, 2005 9:32 am
Posts:600
Location:localhost
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); }

_________________
BAZINGA.


Top
Offline  
 Post subject:
PostPosted:Tue Jul 11, 2006 3:04 pm 
Connoisseur of the Godawful
User avatar
 

Joined:Tue Mar 01, 2005 9:00 am
Posts:456
ICQ:286315965
Website:http://rabidtinker.mine.nu/
Yahoo Messenger:alistair_lynn
AOL:Agent_Vast@mac.com
Location:127.0.0.1
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!

_________________
Alastair Lynn / Alumnus / Onlink Team


Top
Offline  
 Post subject:
PostPosted:Tue Jul 11, 2006 5:44 pm 
Literally Nine
User avatar
 

Joined:Sat Apr 02, 2005 3:31 pm
Posts:1171
Location:The vicinity of an area adjacent to a location.
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!
BAD idea. Do you realize that one of the main reasons Chris Delay's BTree is so slow is due to using recursion? The reason is that it keeps filling the call stack with more and more function calls. Eventually, you get a call stack overflow. NEVER use recursion. EVER.

_________________
- Tycho

Image


Top
Offline  
 Post subject:
PostPosted:Tue Jul 11, 2006 5:54 pm 
User avatar
 

Joined:Wed Feb 01, 2006 4:28 pm
Posts:97
maybe thats the reason darwinia is so slow too (a t least last time i tried it)

_________________
Onlink all-purpose Weapon.


Top
Offline  
 Post subject:
PostPosted:Tue Jul 11, 2006 6:27 pm 
Literally Nine
User avatar
 

Joined:Sat Apr 02, 2005 3:31 pm
Posts:1171
Location:The vicinity of an area adjacent to a location.
Quote:
maybe thats the reason darwinia is so slow too (a t least last time i tried it)
It's likely.


Top
Offline  
Display posts from previous: Sort by 
Post new topic Reply to topic

All times are UTC-05:00


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created by Miah with assistance from hyprnova