Help with code

Hi I was asked to do this assignment:

A machine needs screws to be built. The screws are sold in boxes of 20. Write an instruction that gives you the number of screws that are going to be left at the end of the construction. Screws -> n; number of screws in a box -> m.

I already did this:

#include <iostream>

using namespace std;

int main()
{
cout <<"There are boxes of 20 screws."<<endl<<"Enter the number of scres that you need:" << endl;
int n;
cin>>n;
int m = 20;
int left;
int numberofboxes;

if (n<=m)
{
left = m-n;
cout << "The number of screws left is "<<left;
}

if (n>m)
{
numberofboxes = n/m;
left = numberofboxes*20-n;
cout<< "The number of boxes utilized were "<<numberofboxes<<" and the number of screws left were "<<left;
}
return 0;
}


I have a problem. When the number of screws is 50(for example). When it divides by the number of screws in a box, to give me the number of boxes needed the answer will be 2.5 boxes (wich will be 2 because the variable is an integer) and by the end the number of screws left is going to be negative. I would like to know how can I make round a number like 2.5 to 3 or 3.2 to 4.
you have to include <cmath> library at the start of your program.
Then you can round the number with a function called "ceil" which rounds the number upwards or "floor" to round a number downward.


Example of rounding a number upwards:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
cout<<"type in the first number to be multiplied."<<endl;
double number1;
cin>>number1;
cout<<"type in the second number to be multiplied."<<endl;
double number2;
cin>>number2;
cout<<"The product of your two numbers is: ";
cout<<(number1*number2)<<endl;
cout<<"Rounded answer: ";
cout<<ceil(number1*number2)<<endl;
cin.get();
}



Thats all i can make.
Please post if i helped
Last edited on
Thank you very much it's already working ;D
Last edited on
Topic archived. No new replies allowed.