mardi 20 juin 2017

Computing using if statements

The desription of what I am supposed to do is listed below. THe program runs but I can't get the right numbers to come out in the output. I think the logic is right but it is not working.

We get the # of cookies, the boxes will = # of cookies divided by 24 and if there are extra cookies > than 0 we add one box.

We take the # of boxes, extra boxes is > 0 we add one shipping container .

We then have the total number of shipping containers and boxes.

I don't know why it is not working and have been stuck on this problem for the past 3 hours and finally given up and asked for help.

A box of cookies can hold 24 cookies, and a container can hold 75 boxes of cookies. Write a program that prompts the user to enter the total number of cookies, the number of cookies in a box, and the number of cookie boxes in a container. The program then outputs the number of boxes and the number of containers to ship the cookies. Note that each box must contain the specified number of cookies, and each container must contain the specified number of boxes. If the last box of cookies contains less than the number of specified cookies, you can discard it and output the number of leftover cookies. Similarly, if the last container contains less than the number of specified boxes, you can discard it and output the number of leftover boxes. Because this is a chapter on Selectional Control Structure, if there are no cookies or no boxes remaining then the remaining cookies or remaining boxes must not be output.

#include<iostream>
#include<cmath>

using namespace std;

int main()

{
// Declare variables
    int cookies, extraCookies;
    int boxes,totalBoxes;
    int leftOverBoxes;
    int containers;
    boxes = (cookies/24);

    extraCookies = cookies%24;
    containers = boxes/75;
    leftOverBoxes = boxes%75;


//  Ask for the user input ( doesnt make sense because we are given the capacities)
    cout << " How many cookies do you have ?"<< endl;
    cin >> cookies;
    cout << " You have this many cookies: " << cookies << endl;

    cout << " How many cookies can you fit in one box ?" << endl;
    cin >> cookies;

    cout << " How many cookie boxes can you fit into a container ?" << endl;
    cin >> boxes;

    // Computer number of boxes needed
    if(extraCookies > 0)
      totalBoxes = (boxes + 1);
    cout <<" We need this many boxes:" << totalBoxes << endl;

    if(leftOverBoxes > 0)
      containers = ( containers + 1);
    cout << "We need this many containers: " << containers << endl;


    return 0;



    }

Aucun commentaire:

Enregistrer un commentaire