tags:

views:

191

answers:

9

Thanks eveyerone for giveing me some idea's to try and I'll try them. The code it runs now. **** IT RUNS LIKE THIS

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 ….

But I need it to say ( You will need the following amount of paint in gallons: 3.25 ) So I almost have it an I’ll try u’er ideas.


#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");
}
+3  A: 

Not sure what your question is, but at least change void main() to int main().

Michael Krelin - hacker
A: 

No obsious error there. Doesn't this compile? Doesn't it run? Can you paste an exact message you're getting here?

zaharpopov
A: 

Looks like you're missing a cast:

cans = (int)(wall_area / paint_cover);

Dan Cristoloveanu
A: 

This is C++? You can't store a float in an int as you're trying to do right at the start of your main. In this case it's not a problem but I don't think you want to do that normally.

adam_0
A: 

If you want to output your cans with given decimal precision, you can do it with setprecision mnipulator. You have to include:

#include <iomanip>

Change your cans to float. Then you can set precision of output with:

cout << setprecision(N_DECIMAL_POINTS_PRECISION) << cans << endl;
qba
+3  A: 
void main()

is not valid C++. You should use

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

Note that in C++ you don't have to explicitly "return 0;"

The C99-Style where "int" is assumed as in

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

is not allowed C++ either.

EDIT: There are more things wrong with your code, but this probably triggers your compilation error.

Lucas
A: 

Here you go.....

#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");
}

This code snippet will remove all the warnings. I'm still not sure about your requirements. So many type-casts are not good. And why do you need a statement like: int paint_cover = 100.0;

Please re-phrase the problem. You will find a quick solution.

webgenius
I wonder about void main()...
nhaa123
A: 

Since wall_area and paint_cover are ints, dividing them will return an int, not a float. You need to cast one of them to float or double.

Adding 0.999 is not a method I know of to round a float to some decimal precisions. Adding 0.5 (or similar) and casting to int will give you the rounded float. But you can still get precision errors, floats are floats and they come with precision errors.

The best advice I can give is that if you want a specific precision, you should forget floats entirely.

// 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;
Vincent Robert
prissy's adding 0.99 and then rounding down to the int so that they've got enough cans of paint. e.g. if it takes 12.1 cans of paint to paint a room you still need to buy 13 cans:)
queBurro
If it is about rounding to next integer, then why want a 2 decimal precision?
Vincent Robert
+2  A: 
cans = (wall_area / paint_cover) + 0.999;

is an int divided by an int which will give you another int - change wall_area and paint_cover to floats and it should work

queBurro