If statement HELP PLEASE!

closed account (ENb7ko23)
Hello, I am new to programming in C++. My first project for my CS class is to write a program that will calculate the area of a yard, the area of a house and then figure out how many bags of grass seed are needed for the yard (each bag of seed covers 287 sq. feet). Since you cannot buy a piece of a bag of seed, if there is a remainder, the answer must be rounded up. To do this, I used an if else statement as follows:

if ( (bags_of_seed % 287) == 0 )
{
cout << "\nYou need " << bags_of_seed;
cout << " bags of seed. " << endl;
}
else
{
cout << "You need " << bags_of_seed++;
cout << "bags of seed.\n";
}

I am using nano. For some reason my answer will not round up if there is a remainder. I am simply trying to say that if the remainder is 0, I do not need another bag, but if the remainder is not 0, I do need another bag. Will someone please help me figure out what I'm doing wrong? Thanks a lot for the feedback.
Try moving the ++ to before the bags_of_seed and see if that helps any.

-Albatross
closed account (ENb7ko23)
If I do that it does round up, but it also rounds up for numbers that do not have a remainder- that is do not need another bag.
var++ makes a temporary copy of var, increments var, and gives you the copy (old value)
++var increments var and gives you var.
closed account (ENb7ko23)
right but if I put bags_of_seed++ then it doesn't round up and if I put ++bags_of_seed then everything rounds up (even what I dont want to)
Then why not use bags_of_seed+1?
closed account (ENb7ko23)
it does the same thing
Not here, whether I use ++bags or bags+1 it always gives me the correct result.
The OP might want to use the original value of bags_of_seed later in the program, though. I'm... not sure why, but...

-Albatross
closed account (ENb7ko23)
hey I figured out my problem, I guess it would have helped if I had shown you my other variables but I was using bags_of_seed instead of area_for_seed. Thanks for the help everyone
Topic archived. No new replies allowed.