C++ 我的程序在运行时不会打印任何内容,只需输入几个随机数即可';我会工作(有很多bug)
我正在为编程课做作业。我对正在发生的事感到困惑。我的代码编译时没有问题,但当我运行它时,它什么也不做。我决定按回车键几次,还是一无所获。然后我输入“1”并点击回车键,四次之后,程序运行时应该显示的菜单最终显示出来。有人能帮我发现这个虫子吗 标题:C++ 我的程序在运行时不会打印任何内容,只需输入几个随机数即可';我会工作(有很多bug),c++,object,C++,Object,我正在为编程课做作业。我对正在发生的事感到困惑。我的代码编译时没有问题,但当我运行它时,它什么也不做。我决定按回车键几次,还是一无所获。然后我输入“1”并点击回车键,四次之后,程序运行时应该显示的菜单最终显示出来。有人能帮我发现这个虫子吗 标题: /* +-----------------------------------+ | Fruit | +-----------------------------------+ | -fruit
/*
+-----------------------------------+
| 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
/*
+-----------------------------------+
|果|
+-----------------------------------+
|-名称:字符串|
|-水果价格:双倍|
|-numberOfFruit:整数|
|-numberSold:Integer|
+-----------------------------------+
| |
|水果(名称:String)|
|价格:双倍|
|num:整数)|
| |
|水果(名称:串)|
| |
|水果()|
|+setFruitName(名称:字符串)|
|+setPriceOfFruit(价格:双倍)|
|+setNumberOfFruit(数值:整数)|
|+setNumberSold(num:Integer)|
|+getFruitName():字符串
|+getPriceOfFruit():双精度|
|+getNumberOfFruit():整数|
|+getNumberSold():整数|
|+已售出金额():双倍|
|+buy():布尔值|
+-----------------------------------+
*/
#包括
使用名称空间std;
#ifndef FROUT H
#定义水果
等级水果
{
私人:
字符串名称;
水果的双倍价格;
国际水果;
整数倍;
公众:
水果(字符串名称,双倍价格,整数);
水果(串名);
水果();
void setfurtname(字符串名称);
无效SetPriceOfRuit(双倍价格);
无效setNumberOfFruit(int num);
void setNumberSold(int num);
字符串getFruitName();
双getpriceofruit();
int getNumberOfFruit();
int getNumberSold();
双倍销售额();
bool buy();
};
#恩迪夫
实施:
#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;
}
}
#包括
#包括
#包括
#包括“水果h”
使用名称空间std;
水果::水果(字符串名称,双倍价格,整数)
{
果名=果名;
priceofruit=价格;
numberofruit=num;
numberSold=0;
}
水果::水果(字符串名称)
{
果名=果名;
priceofruit=0;
numberofruit=0;
numberSold=0;
}
水果
{
果名=”;
priceofruit=0;
numberofruit=0;
numberSold=0;
}
void Fruit::setFruitName(字符串名称)
{
果名=果名;
}
无效水果::setPriceOfFruit(双倍价格)
{
如果(价格>=0)
priceofruit=价格;
其他的
而(价格<0)
价格;
如果(价格>=0)
priceofruit=价格;
}
无效水果::setNumberOfFruit(int-num)
{
如果(num>=0)
numberofruit=num;
其他的
while(num<0)
库特数;
如果(num>=0)
numberofruit=num;
}
字符串Fruit::getFruitName()
{
返回水果名;
}
双果::getPriceOfFruit()
{
水果的退货价格;
}
int-Fruit::getnumberofruit()
{
退货数量;
}
int Fruit::getNumberSold()
{
返回次数;
}
双倍水果::销售量()
{
退货编号旧*水果价格;
}
布尔水果::买()
{
布尔交易;
整数购买,可用=0;
numberOfFruit=可用;
cout这不是你所期望的:
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;
}
void-Fruit::setnumberofruit(int-num)
{
while(num<0){
库特数;
}
numberofruit=num;
}
或者,如果num
不在您期望的范围内,则抛出一个异常,并在main()中处理该异常
或者无论在哪里调用setter-在我看来,在setter方法中执行这样的操作不是很好的设计。对我来说听起来很正常。谢谢!我承认,在此之前我一直在学习Python,我必须回到我在处理实现文件时不使用大括号的习惯。关于在setter方法中包含处理输入验证,我们对此有明确的指示。我们似乎在课堂上打破了许多编程惯例,或者被教授了一些不是真正惯例的惯例。我总是听到我应该做什么,而不是当我在这里发帖子寻求帮助时教授告诉我做什么。@Axel芬克尔:嗯,这项作业可能有其他的想法,所以你应该按照教授的指示去做。你最终会明白为什么这是个糟糕的设计(我希望如此)。
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)
{
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;
}