Reputation:
I was writing a converter form Italian lires to Euros in C++:
#include <iostream>
using namespace std;
float x;
int converter (x)
{
y = x/1936,27;
return y;
}
int main()
{
cout << "Give me the value: ";
cin >> x;
converter (x);
}
I try to compile it and i see two errors. The first one is:
lire-euro.cpp:8: error: expected ‘,’ or ‘;’ before ‘{’ token
I defined a function before the parenthesis. Why should I put ','
or ';'
before '{'
?
The second one is:
lire-euro.cpp: In function ‘int main()’:
lire-euro.cpp:17: error: ‘converter’ cannot be used as a function
Why i can't use converter
as a function? Is this related to the other error?
Upvotes: 0
Views: 2604
Reputation: 227370
Your function parameter list is missing the parameter type:
int converter (float x) { ...
// ^^^^^
Besides that, inside the body of the function you use y
, which is undeclared. You can fix that problem by returning the expression, but you may have to replace the ,
in your floating point literal by a .
, depending on your locale.
return x/1936.27;
Note that it may make more sense to return a floating point number rather than an int
.
Finally, I see no reason for x
to be global. You could declare it inside of main()
:
#include <iostream>
int converter(float x)
{
return x/1936.27;
}
int main()
{
float x;
std::cout << "Give me the value: ";
std::cin >> x;
int z = converter(x);
}
Upvotes: 2
Reputation: 310930
You have two errors in the function definition. First of all its parameter x has no type specifier and its local variable y also was not defined.
I think you meant
float x;
int converter()
{
int y = x/1936,27;
return y;
}
Though I am not sure whether y (and the function return type) should be defined as int.
The corresponding call of the functioo could look as
cout << converter() << endl;
Upvotes: 0