gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:42:35 25 4
gpt4 key购买 nike

我正在为我的编程课做作业。我对发生的事情感到困惑。我的代码编译没有问题,但是当我运行它时,它什么也没做。我决定敲几次回车,还是没有。然后我输入“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;
}
}

主要内容:

#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() 时,它将总是尝试从 中读取一个数字标准输入。将其更改为:

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 方法中做你正在做的事情并不是很好的设计。

关于c++ - 我的程序在运行时不会打印任何东西,在输入一些随机数后它会工作(有很多错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25696341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com