这个 c++ 程序中缺少什么?打印计算
What is missing in this c++ program? printing calculations
#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 个函数,以便小数点都对齐。我似乎无法在没有错误的情况下进行打印,并且认为我对错误输出的理解不足以进行必要的更改。我不知道我是否正确地重新定义了变量,或者我是否正确地将它们放在一起(使用 {})。
感谢任何可以帮助我解决这个问题的人。
这是输出:
(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 '{'
我将如何修复这些错误?
您错过了以下之后的右大括号:
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在评论中正确指出的内容。
另外,请注意类型转换的警告,您可能需要在程序逻辑中考虑这一点。
首先:
intmain(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;
}
您在int calculate
函数之后缺少}
。虽然不相关,但您也不必多次拨打fixed
和setprecision
。
相关文章:
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 按升序打印矢量的所有元素直到它为空而没有重复项的最有效方法是什么?
- 当我们声明 cin 为 int 并从 cin 中获取输入并在 cout 中打印 cin 时会发生什么?
- "1L << count"是什么意思?如何使用超出"unsigned long long int? "范围的语句编号打印?
- 打印一个带有静态 int 的函数,有一个 std::cout 和多个 std::cout 有什么区别?
- 在 c++ 中打印到控制台的最佳方式是什么?
- 这个随机字符串打印代码有什么问题?
- 当我打印字符串时,我不知道它是什么输出
- C++代码不打印信息,有什么问题?
- 我只是期望打印炭值.正在发生什么
- 打印矢量时最后的'%'是什么?
- 尝试使用类,但未打印任何内容.不会生成任何错误.我错过了什么吗?
- 打印此矩阵的 if 语句是什么
- 当我在C 中打印一个非初始化的变量时会发生什么
- 这个 c++ 程序中缺少什么?打印计算
- 打印函数会返回一个意外值,原因是什么
- 用"fprintf"打印错误后调用"getchar()"有什么意义?
- c++中使用的缩进和漂亮打印之间的区别是什么
- 从C++在预打印卡上打印的最简单方法是什么?
- 在打印之前将内容存储在内存中的目的是什么