简体   繁体   English

C++货车加载程序

[英]C++ Van loading program

I am stuck.我被困住了。

I have been given this problem: http://i.imgur.com/1U8PjY4.png?1遇到了这个问题: http : //i.imgur.com/1U8PjY4.png?1

The code I've written so far is this:到目前为止我写的代码是这样的:

#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;

char cIn;
char full;

int capacity = 750;
int numVans = 1;

float heaviestVan = 0;
float payload = 0;
float parcelWeight;

bool emptyBelt = false;

int main()
{
    bool end = false;
        while(!end)
        {
            start:
            cout << string(25, '\n');
            cout << endl << "Pauls Premier Parcels (PPP)" << endl << endl;
            cout << "Van being loaded is number: " << numVans << endl << endl;
            cout << "The payload of van " << numVans << " is currently " << payload << " / 750kg" << endl << endl;
            cout << "Is the belt full? ('Y' or 'N'): ";
            cin >> full;


        if (full == 'Y' || 'y')
        {
            while (!emptyBelt)
            {
                cout << endl << endl << "Please enter the weight of the next parcel: ";
                cin >> parcelWeight;

                if (parcelWeight > 120)
                {
                    cout << "The maximum parcel weight is 120kg, please weigh a different parcel: ";
                    cin >> parcelWeight;
                }

                if (payload + parcelWeight <= capacity)
                {
                    payload = payload + parcelWeight;
                    cout << endl << "The parcel has been loaded onto the van" << endl << endl;
                    goto start;
                }

                else
                {
                    cout << endl << "The current van has reached capacity and is being dispatched" << endl;
                    //numVans = numVans + 1;

                    if(payload > heaviestVan)
                    {
                        heaviestVan = payload;
                    }
                    payload = 0;

                    cout << endl << endl << endl << "Vans dispatched: " << numVans;
                    cout << endl << endl << "Weight of heaviest van: " << heaviestVan;

                } 
            }   
        }

    }
    return 0;
}

I need to implement a statement asking the user to place parcels on the belt if the belt is empty, right now It just continues running the program.我需要实现一个语句,如果皮带是空的,要求用户将包裹放在皮带上,现在它只是继续运行程序。

Also the user could enter anything besides Y or y and the program would still run.此外,用户可以输入Yy之外的任何内容,程序仍将运行。

Try rewriting尝试重写

if (full == 'Y' || 'y')

to

if ((full == 'Y') || (full == 'y'))

Some explanation:一些解释:

if (full == 'Y' || 'y')

is the same as是相同的

if ((full == 'Y') || ('y'))

which is the same as这与

if ((full == 'Y') || true)

which is the same as这与

if (true)

regardsless of the value of the variable full.无论变量 full 的值如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM