未在作用域中声明的变量和已定义的函数不能用作函数



这就是当前代码的样子。

#include <iostream>
#include <iomanip>
using namespace std;
int cos(angle)
{
float rad = angle;
float radPh = 1;
float facto = 1;
float tOld = 1;
float tNew = 0;
float tPh = 1;
float tDiff = 1;
float cosAns;
precision = precision + 1;
float x = 10;
float finalPr = 1;
for (int counter = 0; counter < precision; ++counter)
{
finalPr /= x;
}
finalPr = 0.5 * finalPr;
while (tDiff > finalPr)
{
tOld = tPh;
tNew = ((-1)*tOld*rad*rad)/((facto+1)*(facto+2));
tPh = tNew;
radPh = tNew;
facto = facto + 2;
tDiff = tOld - tNew;
cosAns = radPh;
}
return cosAns;
}
int main()
{
int precision = 10;
int choice;
//Title and Menu
cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
cout << endl << endl << "Current Precision: " << precision;
cout << endl << "Select:";
cout << endl << "1. Change Decimal Precision";
cout << endl << "2. Calculate Cos and Sin";
cout << endl << "9. Exit";
while (true)
{
//User Prompt
cout << endl << endl << "Please enter your choice. => ";
cin >> choice;

if (choice == 1)
{
cout << endl << "Please enter a value between 2 and 10. => ";
cin >> precision;
cout << "Current precision is " << precision;
}
if (choice == 2)
{
float angle, anglePh;
float pi = 3.14159265358979323846264338327950288419716;
char angleType;
cout << endl << "Please enter an angle. => ";
cin >> angle;
cout << endl << "Is the angle in degrees or in radian?";
cout << endl << "Input D if it's in degrees,";
cout << endl << "Input R if it's in radian. => ";
cin >> angleType;
if (angleType == 'D')
{
anglePh = angle;
angle = angle*pi/180;
cout << anglePh << " degrees = " << angle << " radian";
}

cout << endl << "Calculating Cos...";
cos(angle);
cout << endl << "Calculating Sin...";
}

if (choice == 9)
{
break;
}
}

}

我正在构建一个程序,从用户那里获得角度,然后在计算正弦和余弦值之前将其转换为弧度。当我运行这个代码时,它告诉我;int cos(角度(";没有定义,所以我尝试将它移到选项2的if循环中,但它没有解决问题。它还说,当我试图从if循环触发选项2时,cos(角度(不能用作函数。对此有什么想法吗?

事实上,当时我用Python制作了一个类似的程序,我用来编写程序的这种方法奏效了。我知道Python和C++之间有一些区别,但我认为总体思路仍然适用。

提前谢谢。

干杯。

在函数定义/声明中,必须提到参数数据类型。

int cos(浮动角度,int精度(

#include <iostream>
#include <iomanip>
using namespace std;
int cos(float angle, int precision)
{
float rad = angle;
float radPh = 1;
float facto = 1;
float tOld = 1;
float tNew = 0;
float tPh = 1;
float tDiff = 1;
float cosAns;

precision = precision + 1;
float x = 10;
float finalPr = 1;
for (int counter = 0; counter < precision; ++counter)
{
finalPr /= x;
}
finalPr = 0.5 * finalPr;
while (tDiff > finalPr)
{
tOld = tPh;
tNew = ((-1)*tOld*rad*rad)/((facto+1)*(facto+2));
tPh = tNew;
radPh = tNew;
facto = facto + 2;
tDiff = tOld - tNew;
cosAns = radPh;
}
return cosAns;
}
int main()
{
int precision = 10;
int choice;
//Title and Menu
cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
cout << endl << endl << "Current Precision: " << precision;
cout << endl << "Select:";
cout << endl << "1. Change Decimal Precision";
cout << endl << "2. Calculate Cos and Sin";
cout << endl << "9. Exit";
while (true)
{
//User Prompt
cout << endl << endl << "Please enter your choice. => ";
cin >> choice;

if (choice == 1)
{
cout << endl << "Please enter a value between 2 and 10. => ";
cin >> precision;
cout << "Current precision is " << precision;
}
if (choice == 2)
{
float angle, anglePh;
float pi = 3.14159265358979323846264338327950288419716;
char angleType;
cout << endl << "Please enter an angle. => ";
cin >> angle;
cout << endl << "Is the angle in degrees or in radian?";
cout << endl << "Input D if it's in degrees,";
cout << endl << "Input R if it's in radian. => ";
cin >> angleType;
if (angleType == 'D')
{
anglePh = angle;
angle = angle*pi/180;
cout << anglePh << " degrees = " << angle << " radian";
}

cout << endl << "Calculating Cos...";
cout <<cos(angle, precision);
cout << endl << "Calculating Sin...";
}

if (choice == 9)
{
break;
}
}

}

最新更新