我的程序在运行时不会打印任何内容,输入几个随机数后它会工作(有很多错误)
My program will not print anything when run, after entering a few random numbers it'll work (with many bugs)
我正在为我的编程类进行赋值。我对正在发生的事情感到困惑。我的代码编译起来没有问题,但当我运行它时,它什么都不做。我决定打几次回车,但还是一无所获。然后我输入"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方法中做这种事情不是很好的设计。
相关文章:
- 为我的 c++ 类介绍制作一个三角形分类器.我有几个问题
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 如何返回或护理项目清单基于几个类别
- 我们如何在文本文件中找到C++中的几个单词?
- 关于 std::bitset 构造函数的几个问题?
- 几个 Nt 函数在 Windows 7 x32 上返回STATUS_WAIT_0
- std::stoi 只转换前几个字符
- C++ 通过接口使用从其他几个类继承的类
- 在矢量中使用擦除时双重释放或损坏(快速顶部).如何擦除一个矢量的几个项目知道它们的索引?
- 如何遍历几个每小时一次的根(.root)文件,并将它们组合成更大的每日数据.root文件?
- 我有几个关于参考变量的问题
- 如何将几个变量复制到其他变量
- 如何在新设计中迁移需要几个额外参数的函数
- 多线程.如果我使用信号量,我可以在开始时创建很多线程还是应该只有几个线程?
- 如何在几个空行后打印文件中的输出
- recvfrom 只收到几个数据包,之后它进入等待状态
- 使用 std::vector<std::future<int>> 和 std::async 启动几个线程时中止
- C++ 两个线程,共享几个整数变量
- 几个随机数 C++
- 我的程序在运行时不会打印任何内容,输入几个随机数后它会工作(有很多错误)