C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 21st, 2003, 08:38 PM
cavecrazy cavecrazy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 16 cavecrazy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question question about code

Can someone tell me why I am getting the following errors? Any advice would be appreciated. The program I am writing for the header file uses long as a data type for the returned fibonacci number. The cpp file reads in a long number and returns the correspondeng fibonacci number. I am limiting the read numbers 0 to 40.


FIBOPRE3.CPP(20) : error C2065: 'cout' : undeclared identifier
FIBOPRE3.CPP(20) : error C2065: 'endl' : undeclared identifier
FIBOPRE3.CPP(20) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
Error executing cl.exe.

My code is as follows:

#include "fibonacci.h"


long fibonacci( long n )
{
if ( n == 0 || n == 1 ) // base case
return n;
else // recursive case
return fibonacci( n - 1 ) + fibonacci( n - 2 );
}

void main ()
{
cout << fibonacci(40) << endl << endl;
}


Here is the code in my header file:

// Provides function to compute Fibonacci numbers

// Recursive version
long Fibonacci_Rec(int n);

Reply With Quote
  #2  
Old September 21st, 2003, 08:41 PM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 51 m 4 sec
Reputation Power: 21
First, you must include iostream. If it is not included in fibonacci you will have to add it. Second, you will have to set your namespace or preface the use of cout/endl with std:: to get the compiler to resolve the name.
__________________

Left DevShed May 28, 2005. Reason: Unresponsive administrators.
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Last edited by mitakeet : September 21st, 2003 at 08:47 PM.

Reply With Quote
  #3  
Old September 21st, 2003, 09:22 PM
cavecrazy cavecrazy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 16 cavecrazy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have fixed the errors and it is now compiling. But it is not outputting anything. What have I done wrong?

Reply With Quote
  #4  
Old September 21st, 2003, 09:25 PM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 51 m 4 sec
Reputation Power: 21
Can't do that without your code! Be sure to enclose it in "code" or "php" tags!

Reply With Quote
  #5  
Old September 21st, 2003, 09:27 PM
cavecrazy cavecrazy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 16 cavecrazy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
<code>
#include "fibonacci.h"


long fibonacci( long n )
{
if ( n == 0 || n == 1 ) // base case
return n;
else // recursive case
return fibonacci( n - 1 ) + fibonacci( n - 2 );
}

void main ()
{
cout << fibonacci(40) << endl << endl;
}

</code>

Here is the code in my header file:

<code>
// Provides function to compute Fibonacci numbers

// Recursive version
long Fibonacci_Rec(int n);
</code>

Reply With Quote
  #6  
Old September 21st, 2003, 09:29 PM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 51 m 4 sec
Reputation Power: 21
Look at http://forums.devshed.com/misc.php?s=&action=bbcode for explanations on codes.

I will look at what you have, but please post again while I do so.

Reply With Quote
  #7  
Old September 21st, 2003, 09:31 PM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 51 m 4 sec
Reputation Power: 21
You need to add "#include <iostream>" to either your cpp file or your header file, then prepend cout and endl with "std::". You could also use namespaces, but I will leave that as an exersize for the student.

Reply With Quote
  #8  
Old September 21st, 2003, 09:35 PM
cavecrazy cavecrazy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 16 cavecrazy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry I posted my previous code. I fixed the iostream problem and the namespace problem. I am compiling with no errors but it is not outputting anything.

Code is as follows:

Code:

// Recursive definition of function fibonacci

#include "fibonacci.h"
#include <iostream>

using namespace std;
long fibonacci( long n )
{
   if ( n == 0 || n == 1 )  // base case
      return n;
   else                     // recursive case
      return fibonacci( n - 1 ) + fibonacci( n - 2 );
}
 
void main ()
  {
      cout << fibonacci(40) << endl << endl;
  }


My header file code:

Code:

// Provides function to compute Fibonacci numbers 

// Recursive version 
long Fibonacci_Rec(int n); 

Reply With Quote
  #9  
Old September 21st, 2003, 09:43 PM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 51 m 4 sec
Reputation Power: 21
When I run it it appears to go into an infinite loop. Check your code for correctness (I got no idea what you are trying to do).

BTW: You don't need the header file for what you are doing.

I got this to compile and run fine:

Code:
#include <iostream>

using namespace std;

long fibonacci( long n ){
/*
   if ( n == 0 || n == 1 )  // base case
      return n;
   else                     // recursive case
      return fibonacci( n - 1 ) + fibonacci( n - 2 );
*/
    return n;
}
 
void main (){
    long value = fibonacci(40);
    cout << value << endl << endl;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > question about code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT