我的程序在运行时不会打印任何内容,输入一些随机数后它会工作(有很多错误)

时间:2014-09-06 02:52:38

标签: c++ object

我正在为我的编程课做作业。我对发生的事感到困惑。我的代码编译没有问题,但是当我运行它时,它没有做任何事情。我决定进入几次,仍然没有。然后我输入“1”并按Enter键,四次之后,最终显示程序运行时应该显示的菜单。任何人都可以帮我发现这个错误吗?

部首:

/* 
+-----------------------------------+
|               Fruit               |
+-----------------------------------+
| -fruitName : String               |
| -priceOfFruit : Double            |
| -numberOfFruit : Integer          |
| -numberSold : Integer             |
+-----------------------------------+
| <<constructor>>                   |
|   Fruit(name: String              |
|         price: Double             |
|         num : Integer)            |
| <<constructor>>                   |
|   Fruit(name: String)             | 
| <<constructor>>                   | 
|   Fruit()                         |
| +setFruitName(name : String)      |
| +setPriceOfFruit(price : Double)  |
| +setNumberOfFruit(num : Integer)  |         
| +setNumberSold(num : Integer)     |
| +getFruitName() : String          | 
| +getPriceOfFruit() : Double       |
| +getNumberOfFruit() : Integer     |
| +getNumberSold() : Integer        |
| +amountSold() : Double            |
| +buy() : Boolean                  |
+-----------------------------------+
*/

#include <string>

using namespace std;

#ifndef FRUIT_H
#define FRUIT_H

class Fruit
{
private:

   string fruitName;
   double priceOfFruit;
   int numberOfFruit;
   int numberSold;

public:
   Fruit(string name, double price, int num);
   Fruit(string name);
   Fruit();
   void setFruitName(string name);
   void setPriceOfFruit(double price);
   void setNumberOfFruit(int num);
   void setNumberSold(int num);
   string getFruitName();
   double getPriceOfFruit();
   int getNumberOfFruit();
   int getNumberSold();
   double amountSold();
   bool buy();
};

#endif

实现:

#include <iostream>
#include <iomanip>
#include <string>
#include "Fruit.h"

using namespace std;

Fruit::Fruit(string name, double price, int num)
{
    fruitName = name;
    priceOfFruit = price;
    numberOfFruit = num;
    numberSold = 0;
}

Fruit::Fruit(string name)
{
    fruitName = name;
    priceOfFruit = 0;
    numberOfFruit = 0;
    numberSold = 0;
}

Fruit::Fruit()
{
    fruitName = "";
    priceOfFruit = 0;
    numberOfFruit = 0;
    numberSold = 0;
}

void Fruit::setFruitName(string name)
{
    fruitName = name;
}

void Fruit::setPriceOfFruit(double price)
{
    if (price >= 0)
        priceOfFruit = price;
    else
        while (price < 0)
            cout << "\nThe price cannot be negative. Please try again: " << endl;
            cin >> price;
                if (price >= 0)
                    priceOfFruit = price;

}

void Fruit::setNumberOfFruit(int num)
{
    if (num >= 0)
        numberOfFruit = num;
    else
        while (num < 0)
            cout << "\nThe number of fruit cannot be negative. Please try again: " << endl;
            cin >> num;
                if (num >= 0)
                    numberOfFruit = num;
}

string Fruit::getFruitName()
{
    return fruitName;
}

double Fruit::getPriceOfFruit()
{
    return priceOfFruit;
}

int Fruit::getNumberOfFruit()
{
    return numberOfFruit;
}

int Fruit::getNumberSold()
{
    return numberSold;
}

double Fruit::amountSold()
{
    return numberSold * priceOfFruit;
}

bool Fruit::buy()
{
    bool transaction;
    int buying, available = 0;

    numberOfFruit = available;

    cout << "\n" << fruitName << " .......... " << "$" 
         << setfill('0') << setw(4) << priceOfFruit
         << "\nPlease enter the number of "<< fruitName 
         << "s to purchase: " << endl;
    cin >> buying;

    if (buying > available) {
        transaction = false;
        return transaction;
    } 
    else {
        numberOfFruit = available - buying;
        numberSold = buying;
        transaction = true;
        return transaction;
    }
}

主:

#include <iostream>
#include <iomanip>
#include <string>
#include "Fruit.h"

using namespace std;

int main()
{
    int selection = 5;
    bool transaction;
    double total;
    Fruit apple("Apple", 1.99, 10);
    Fruit banana("Banana");
    Fruit orange;

    banana.setPriceOfFruit(0.79);
    banana.setNumberOfFruit(8);

    orange.setFruitName("Orange");
    orange.setPriceOfFruit(1.49);
    orange.setNumberOfFruit(7);

    do {

        cout << "  Fruit Stand  "
             << "\n---------------"
             << "\n1. Buy Apple"
             << "\n2. Buy Banana"
             << "\n3. Buy Orange"
             << "\n4. Print Total"
             << "\n0. Quit" 
             << "\nPlease make a selection: " << endl;
        cin >> selection;

        while (selection < 0 && selection > 4) {
            cout << "\nSorry, that was an invalid selection."
                 << "Please try again: " << endl;
            cin >> selection;
        }

        if (selection == 1)
            transaction = apple.buy();
        else if (selection == 2)
            transaction = banana.buy();
        else if (selection == 3)
            transaction = orange.buy();
        else if (selection == 4)
            cout << "Your current total is $" << setfill('0') << setw(4) << total << endl;
        else
            cout << "\nThank you for shopping at the Fruit Stand!" << endl;

        if (transaction == true) {
            cout << "\nThank you for your purchase!\n" << endl;
            total = apple.amountSold() + banana.amountSold() + orange.amountSold();
        }
        else
            cout << "\nSorry, out of stock.\n" << endl;

    } while (selection != 0);

    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

这不符合您的期望:

void Fruit::setNumberOfFruit(int num)
{
    if (num >= 0)
        numberOfFruit = num;
    else
        while (num < 0)
            cout << "\nThe number of fruit cannot be negative. Please try again: " << endl;
            cin >> num;
                if (num >= 0)
                    numberOfFruit = num;
}

请使用大括号 - 这是C ++,而不是python。就像现在一样,代码相当于:

void Fruit::setNumberOfFruit(int num)
{
    if (num >= 0)
        numberOfFruit = num;
    else
        while (num < 0)
            cout << "\nThe number of fruit cannot be negative. Please try again: " << endl;
    cin >> num;
    if (num >= 0)
        numberOfFruit = num;
}

这意味着当您在setNumberOfFruit()中致电main()时,总是尝试从stdin读取一个号码。将其更改为:

void Fruit::setNumberOfFruit(int num)
{
    if (num >= 0)
        numberOfFruit = num;
    else {
        while (num < 0) {
            cout << "\nThe number of fruit cannot be negative. Please try again: " << endl;
            cin >> num;
                if (num >= 0)
                    numberOfFruit = num;
        }
    }
}

事实上,也许更好的选择是做到这一点:

void Fruit::setNumberOfFruit(int num)
{
    while (num < 0) {
        cout << "\nThe number of fruit cannot be negative. Please try again: " << endl;
        cin >> num;
    }
    numberOfFruit = num;
}

或者,更好的是,如果num不在您期望的范围内,则抛出异常,并在main()或调用setter的任何地方处理该异常 - 在我看来,它不是很好的设计可以做你在setter方法中做的那些事情。