c ++错误 - 需要帮助

时间:2009-12-02 09:08:47

标签: c++

感谢eveyerone给我一些想法尝试,我会试试。 它现在运行的代码。  ****它像这样运行

Input room dimensions
Length?  ( I enter 12 )
Width?  ( I enter 10)
Height? ( I enter 8)
MY Answer = You will need the following amount of paint in gallons:  3
Press any key to continue ….

但我需要它说(你需要以下加仑油漆量:3.25) 所以我几乎有了它,我会尝试你的想法。


#include <conio.h>
#include <iostream>
#include <stdio.h>
#define N_DECIMAL_POINTS_PRECISION (100) // n = 2. two decimal points.

using namespace std;

void main (void)
{

int paint_cover = 100.0;
int exc_area1 = 27.0;

float length, width, height;
int wall_area;
int cans;

printf ("Input room dimensions\n");
printf ("length? ");
scanf ("%f", &length);
printf ("width? ");
scanf ("%f", &width);
printf ("height? ");
scanf ("%f", &height);

wall_area = ((length + length + width + width) * height) - exc_area1;
//wall_area = ((2*length) + (2*width) *8) - exc_area1;
cans = (wall_area / paint_cover) + 0.999;
cout << "You will need the following amount of paint in gallons: " << cans <<endl;
//printf ("You need = %d\n", "cans of paint");
system("pause");
}

9 个答案:

答案 0 :(得分:3)

不确定您的问题是什么,但至少将void main()更改为int main()

答案 1 :(得分:3)

void main()

无效的C ++。你应该使用

int main() { /* ... */ }
int main(int argc, char* argv[]) { /* ... */ }

请注意,在C ++中,您不必显式地“返回0;”

C99-Style,其中“int”假设为

main(){/*...*/}

也不允许使用C ++。

编辑:你的代码有很多问题,但这可能会引发你的编译错误。

答案 2 :(得分:2)

cans = (wall_area / paint_cover) + 0.999;

是一个int除以一个int,它会给你另一个int - 更改wall_area和paint_cover到浮点数,它应该可以工作

答案 3 :(得分:0)

那里没有明显的错误。这不编译吗?不运行吗?你能粘贴一个确切的消息吗?

答案 4 :(得分:0)

看起来你错过了演员:

cans =(int)(wall_area / paint_cover);

答案 5 :(得分:0)

这是C ++?你不能在一个int中存储一个浮点数,因为你正试图在你的主要开始时做。在这种情况下,这不是问题,但我认为你不想这样做。

答案 6 :(得分:0)

如果要输出具有给定小数精度的罐,可以使用setprecision mnipulator进行输出。你必须包括:

#include <iomanip>

将您的cans更改为float。然后您可以使用以下命令设置输出精度:

cout << setprecision(N_DECIMAL_POINTS_PRECISION) << cans << endl;

答案 7 :(得分:0)

由于wall_areapaint_cover是整数,因此除以它们将返回int,而不是float。你需要将其中一个投射到浮动或加倍。

添加0.999不是我所知道的将浮点数舍入到一些小数精度的方法。添加0.5(或类似)并转换为int将为您提供舍入浮点数。但是你仍然可以得到精度错误,漂浮物是漂浮物,它们会出现精确错误。

我能给出的最好的建议是,如果你想要一个特定的精度,你应该完全忘记花车。

// Here you get 100th of cans
cans = 100 * wall_area / paint_cover;
// Here you display your cans number as a decimal
cout << "You will need the following amount of paint in gallons: " 
     << (cans / 100) << "." << setfill('0') << setw(2) << (cans % 100) << endl;

答案 8 :(得分:-2)

你去........

#include "stdafx.h"
#include <iostream>
using namespace std;

#define N_DECIMAL_POINTS_PRECISION (100) // n = 2. two decimal points.

void main ()
{
    int paint_cover = 100;
    int exc_area1 = 27;
    float length, width, height;
    int wall_area;
    int cans;

    printf ("Input room dimensions\n");
    printf ("length? ");
    scanf ("%f", &length);
    printf ("width? ");
    scanf ("%f", &width);
    printf ("height? ");
    scanf ("%f", &height);
    wall_area =(int)((length + length + width + width) * height) - exc_area1;
    cans =(int) (wall_area / paint_cover) +(int) 0.999;
    cout << "You will need the following amount of paint in gallons: " << cans <<endl;
    system("pause");
}

此代码段将删除所有警告。我仍然不确定你的要求。如此多的类型演员并不好。 为什么你需要一个如下声明: int paint_cover = 100.0;

请重新说明问题。你会找到一个快速的解决方案。

相关问题