2012-09-22 27 views
-1
#include <iostream> 
#include <iomanip> 
using namespace std; 

int main() // print to console: 3.0*5.0=15.00 
{ 
double a; 
double b; 
a =(3.0); 
b =(5.0); 
cout << " " << fixed << setprecision (1) << a << "\n" << endl; 
cout << "* " << b << "\n" << endl; 
cout << "------" << endl; 
cout << fixed << setprecision (2) << a*b << "\n" << endl; 

return 0; 
} 

int calculate (int a, int b, int c) // print to console: (7.1*8.3)-2.2=56.73 
{ 
double a; 
    double b; 
    double c; 
    a = (7.1); 
    b = (8.3); 
    c = (2.2); 
    cout << " " << fixed << setprecision (1) << a << "\n" << endl; 
    cout << "* " << b << "\n" << endl; 
    cout << "- " << c << "\n" << endl; 
    cout << "------" << endl; 
    cout << setprecision(2) << (a*b)-c << "\n" << endl; 
} 
int calculation (int a, int b, int c) // print to console: 3.2/(6.1*5.0)=0.10 
{ 
double a; 
double b; 
double c; 
a=(3.2); 
b=(6.1); 
c=(5.0); 
cout << " " << fixed << setprecision (1) << a << "\n" << endl; 
    cout << " /(6.1*5.0)" << endl; //how can I use variables instead of using quotes? 
cout << "------" << endl; 
cout << setprecision(2) << a/(b*c) << "\n" << endl; 

system("PAUSE"); 

return 0; 
} 

我已经使用了一个重复的布局,我希望能够垂直打印3个函数,以便小数点全部排队。我似乎无法打印没有错误,并认为我不明白错误输出足以进行必要的更改。我不知道我是否正确地重新定义了变量,或者我是否正确地将它们组合在一起(使用{})。这个C++程序中缺少什么?打印计算

感谢任何能够帮助我实现这个目标的人。

这里是输出:

(7): error C2082: redefinition of formal parameter 'a' 
(8): error C2082: redefinition of formal parameter 'b' 
(9): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data 
(10): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data 
(21): error C2082: redefinition of formal parameter 'a' 
(22): error C2082: redefinition of formal parameter 'b' 
(23): error C2082: redefinition of formal parameter 'c' 
(24): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data 
(25): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data 
(26): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data 
(34): error C2601: 'calculation' : local function definitions are illegal 
(20): this line contains a '{' which has not yet been matched 
(51): fatal error C1075: end of file found before the left brace '{' 

我将如何解决这些错误?

+0

会做,我会尝试:-s –

+0

是的我是新来这个网站,现在我明白了。我在你的评论旁边做了一个检查。感谢您的帮助btw! –

回答

0

首先:

INT主(INT,INT) - 非常不标准程序入口点

下一个:

int main (int a, int b) // print to console: 3.0*5.0=15.00 
{ 
double a; 
double b; 

您重新定义形式参数a和b

就是这样,这就是你的编译器所说的:

(7): error C2082: redefinition of formal parameter 'a' 
(8): error C2082: redefinition of formal parameter 'b' 

int calculate (int a, int b, int c) // print to console: (7.1*8.3)-2.2=56.73 
{ 
double a; 
    double b; 
    double c; 

你有相同的局部变量和形式参数名称,我不认为这是你所期待的。

还有最后一:

你缺少右括号“}”在末计算功能。

#include <iostream> 
#include <iomanip> 
using namespace std; 

int main() // print to console: 3.0*5.0=15.00 
{ 
double a; 
double b; 
a =(3.0); 
b =(5.0); 
cout << " " << fixed << setprecision (1) << a << "\n" << endl; 
cout << "* " << b << "\n" << endl; 
cout << "------" << endl; 
cout << fixed << setprecision (2) << a*b << "\n" << endl; 

return 0; 
} 

int calculate() // print to console: (7.1*8.3)-2.2=56.73 
{ 
    double a; 
    double b; 
    double c; 
    a = (7.1); 
    b = (8.3); 
    c = (2.2); 
    cout << " " << fixed << setprecision (1) << a << "\n" << endl; 
    cout << "* " << b << "\n" << endl; 
    cout << "- " << c << "\n" << endl; 
    cout << "------" << endl; 
    cout << setprecision(2) << (a*b)-c << "\n" << endl; 
} 
int calculation() // print to console: 3.2/(6.1*5.0)=0.10 
{ 
    double a; 
    double b; 
    double c; 
    a=(3.2); 
    b=(6.1); 
    c=(5.0); 
    cout << " " << fixed << setprecision (1) << a << "\n" << endl; 
    cout << b << "*" << c << endl; //how can I use variables instead of using quotes? 
    cout << "------" << endl; 
    cout << setprecision(2) << a/(b*c) << "\n" << endl; 

    system("PAUSE"); 

    return 0; 
} 
+0

非常感谢您的帮助。我有一个问题:我已经将1st改为int(),并将第二个函数作为calculate(int a,int b ...)。如何重新定义最终计算的a/b/c变量(int a,int b,int c)函数?这是我迷路的地方? –

2

你错过了右括号后:

int calculate (int a, int b, int c) // print to console: (7.1*8.3)-2.2=56.73 
{ 
    .... 
    .... 
    cout << setprecision(2) << (a*b)-c << "\n" << endl; 
} 

^^^^

既然你已经被使用在两个功能缺失的支柱导致符号名同名的符号不止一次违反以上一个定义规则,因此重新定义错误。

另外,请注意@ Ed.S在评论中正确指出了什么。

此外,请注意类型转换的警告,您可能需要考虑在程序逻辑中。

+0

也注意到'a'是该函数的一个参数,也被声明为一个变量。 'b'和'c'相同 –

+0

@EdS .:很好的发现:) –

0

您在int calculate函数后缺少}。虽然不相关,但您也不必多次拨打fixedsetprecision

+0

ohhhh我不知道!谢谢 –

+0

我用你的建议做了编辑! –

相关问题