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

My program will not print anything when run, after entering a few random numbers it'll work (with many bugs)

本文关键字:随机数 几个 工作 错误 程序 任何内 打印 我的 输入 运行时      更新时间:2023-10-16

我正在为我的编程类进行赋值。我对正在发生的事情感到困惑。我的代码编译起来没有问题,但当我运行它时,它什么都不做。我决定打几次回车,但还是一无所获。然后我输入"1"并点击回车键,四次之后,程序运行时应该显示的菜单终于显示了。有人能帮我找出虫子吗?

标题:

/* 
+-----------------------------------+
|               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;
    }
}

Main:

#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;
}

这不是你所期望的:

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;
}

这意味着当您在main()中调用setNumberOfFruit()时,它将始终尝试从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方法中做这种事情不是很好的设计。