我在第4行收到错误:
expected constructor, destructor, or type conversion before ';' token
当涉及到函数时,我现在很弱,所以我知道我的函数声明(?)有问题。有人有什么建议吗?提前谢谢......
#include <iostream>
using namespace std;
shapeDetermine (char letter);
int main()
{
char letter;
int side, area, base, height; // lengths to be used in calculating area
cout << "Enter the first letter of the shape:";
cin>> letter;
system ("pause");
return 0;
}
添加了:
void shapeDetermine (char shape)
{
int side, area, base, height; // lengths to be used in calculating area
if (letter == 's') //determine what shape is used - square
{
cout<< " Enter the length of side of square:";
cin>> side;
area = side * side; // formula for area of square
cout<< " The area of the square is "<< area<< " cm."<<endl;
}
else if (letter =='t') // triangle
{
cout<< " Enter the height of triangle:";
cin>> height;
cout<< " Enter length of base of triangle:"<< endl;
cin>> base;
area = (base * height) / 2; // formula for area of triangle
cout<< " The area of the triangle is "<< area<< " cm."<<endl;
}
else
{
cout<<" Invalid shape entered."<< endl; // for any character other than s||t
}
}
答案 0 :(得分:4)
您没有声明shapeDetermine
的返回类型。例如,如果它应该返回一个int,则应声明:
int shapeDetermine(char letter);
正在更新以响应OP发布的新代码:
新代码很好。但是,如果在文件(或其他文件)中 main()
后出现,则在调用之前仍需要为其声明函数原型。鉴于您发布的函数定义,原型将是:
void shapeDetermine(char shape);
另一次更新以解决评论:
您需要实际调用该函数。在您发布的main()
代码中,您没有在任何地方调用shapeDetermine()
。尝试将main()
更改为如下所示:
cout << "Enter the first letter of the shape:";
cin>> letter;
shapeDetermine(letter);
system ("pause");
答案 1 :(得分:0)
您需要定义函数的返回值(void?)
答案 2 :(得分:0)
到目前为止,我可以看到你已经声明了函数shapeDetermine但是在声明中你没有指定返回类型。我认为你必须指定一个,即使它是无效的。