Issue When running code in Visual Studio Code

Hey Geeks,

I an trying to do an assignment to run a recursive algorithm:

#include <iostream>
using namespace std;
float fact (float n);
int main () {
float n,r;
cout <<"Calculates factorial, whole number: "<< endl;
cin >> n;
r = fact(n);
cout <<"El resultado es: "<< r << endl;
return 0;
}

The problem is I am getting this error an i dont know how to fix it!

Undefined symbols for architecture x86_64:
"fact(float)", referenced from:
_main in ExtraCredit1-b26fe0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


Do you guys have any ideas as to what it could be?
Last edited on
definition function 'fact(n)' .... ??
what do you mean?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
float fact (float n);
int main () {
float n,r;
cout <<"Calculates factorial, whole number: "<< endl;
cin >> n;
r = fact(n);
cout <<"El resultado es: "<< r << endl;
return 0;
}


line 3: declaration of function 'fact(n);

but where is...

float fact( float n)
{
// source code
}
Beware quantization error
float fact(float n) { return n > 1.f? (n * fact(n - 1.f)): 1.f; }

Not recursive, but this might make more sense in certain ways
1
2
#include <cmath>
float fact(float n) { return std::tgamma(n + 1.f); }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void  fact(int n, int& r){
r *=n;
If(n==2 ){  return;}
else{fact(n-1,r);}

}

int main(){
unsigned long long r=1;
int n{};

cin>>n;
fact(n,r);
cout<<r;
}


My best guess. Not exactly sure what you're trying to do. Untested. Having computer issues rn so doing this on my phone. So sry for any typos etc.
Why float? factorial works with integers.

Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

inline long long unsigned int fact(int n)
{
	return (n > 1) ? fact(n - 1) * n : 1;
}

constexpr int MAXN = 20;

int main()
{
	int n = 0;

	while ((cout << "Enter n: (2 - " << MAXN << "): ") && (!(cin >> n) || ((n < 2) || (n > MAXN)))) {
		cout << "Invalid number\n";
		cin.clear();
		cin.ignore(1000, '\n');
	}

	cout << fact(n) << endl;
}

Hello Guys,

I made some changes to the code, but im still getting a few errors:

#include <iostream> // adds the reference to the input/output stream library
using namespace std; // the using directive
float fact (float n);
int main {
float n, r;
cout<<"Calcula Factorial, Entre Numeros: "<< endl;
cin >> n;
r = fact(n);
cout <<"El resultado es: "<< r << endl;
return 0;
}

float fact (float n){
if (n==0)
return 1;
else
return n* fact(n-1);
}


This are the errors I am getting:

ExtraCredit1.cpp:10:1: error: main cannot be declared as global variable
int main {;
^
ExtraCredit1.cpp:10:9: error: expected ';' after top level declarator
int main {;
^
;


Any Ideas?
Hello rodriguez135994,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream> // adds the reference to the input/output stream library

using namespace std; // the using directive

float fact(float n);

int main{

float n, r;

cout << "Calcula Factorial, Entre Numeros: " << endl;
cin >> n;

r = fact(n);

cout << "El resultado es: " << r << endl;

return 0;
}

float fact(float n)
{
    if (n == 0)
        return 1;
    else
        return n * fact(n - 1);
}

Line 7 defines a global variable "main", but where is the "main" function?

You will also find that any number greater than 34 exceeds what a float can hold. A "long long" would be a better choice.

Andy
Thanks for the input.

I have NO idea how to define main and call in the "main function". This was all the infirmation given to me. So, I have no IDEA how to make it run!

C

PS... thanks for the tip on how to writ my code.
1
2
3
4
int main()
{
   // the code for the main function goes here
}


In the above code, the () is missing after main. See my example above.
You guys are awesome! That worked!

CeErre
Topic archived. No new replies allowed.