|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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); |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
I have fixed the errors and it is now compiling. But it is not outputting anything. What have I done wrong?
|
|
#4
|
||||
|
||||
|
Can't do that without your code! Be sure to enclose it in "code" or "php" tags!
|
|
#5
|
|||
|
|||
|
<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> |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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.
|
|
#8
|
|||
|
|||
|
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); |
|
#9
|
||||
|
||||
|
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;
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > question about code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|