C++ 错误 : Redeclaration of double TOTAL_CALORIES previously redclare here

标签 c++

#include <iostream>
#include <iomanip> //round numbers
#include <cstdlib>

using namespace std;

int main( )
{

 //Declare
const double ONE_COOKIES = 75.0;
double ATE_COOKIES;
double TOTAL_CALORIES;

 //Output of program
    cout << " Input how many cookies you ate (eg, 3 & 5): " << endl; 
    cin >> ATE_COOKIES;


 // Round numbers
    cout << fixed << setprecision(1) << ATE_COOKIES << endl;

 //Formula  
    double TOTAL_CALORIES = ATE_COOKIES * ONE_COOKIES;

 //Results
 cout << " One cookie is equal to " << ONE_COOKIES << " caloreis " << " And you ate "     << double caloreis << " of cookies" << endl; 



 system ("pause");
    return 0;
}

这是一个 C++ 程序,我试图计算该程序。我在计算程序时遇到问题,所以我正在寻求帮助。提前致谢。

最佳答案

您不能重新申报 TOTAL_CALORIES。 double 标识符仅用于创建变量的原因。

语句应该TOTAL_CALORIES = ATE_COOKIES * ONE_COOKIES;因为你只是给TOTAL_CALORIES赋值,而不是声明变量。

这似乎也适用于您的 cout 语句,当您只是使用您不需要需要指定它的类型的变量时,您告诉编译器一次,这就是您需要做的全部。

int thing  = 0;
thing = 4; //just assign a value
cout << thing << endl; //print the value

另一方面,您应该避免在变量中使用全部大写。这通常表示程序中的常量。尝试使用驼峰式或全部小写命名可变变量。

关于C++ 错误 : Redeclaration of double TOTAL_CALORIES previously redclare here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26836011/

相关文章:

C++ getline(cin, variable) 行为异常。网站上的解决方案没有帮助

c++ - 如何为“(1+3 * ( 5/4)) 之类的字符串编写求值器并获得数值结果

c++ - 为什么 C++ 参数类型匹配中的 `"文字 "` encouraged to decay to ` const char*`?

c++ - 如何使用 openGL 正确定位天空盒相机

c++ - 在 Delphi 中实现 C DLL

c++ - 动态分配内存给字符指针

c++ - C++ 中的数组和函数

c++ - unordered_map 中迭代器的效率 (C++)

c++ - 语法错误编译 header 包含 "char[]"

c++ - Aligned_alloc 行为