Cout在几行之后停止打印(c++)



我正在为我的类编写这个程序,遇到了一个问题。没有错误,但当我尝试编译(用g++编译(时,程序在"行之后停止向控制台输出;斯图尔特正在下订单。\n〃;。我知道\n不会刷新输出,所以我尝试使用flush,但没有成功(也许我做错了?(。我需要做什么才能让程序在控制台中显示完整的输出?我真的不确定哪里出了问题,因为我已经想办法解决这个问题一段时间了。我还比较新,所以如果这是一个简单的解决方案,我很抱歉。提前谢谢。

输出:

Kevin placing order.
Invalid input [Iron Goddess].
Not serving requested drinks. Drink order ignored.
Order Detail:
Kevin
Date: 3/2/2021
Phone: 123-456-0000
Total Balance: $21.75
Ordered Drinks: 4
Balance: $39.279
Discounted Balance: $37.315
Stuart placing order.

main

#include <string>
#include "Order.h"
#include <iostream>
using namespace std;
int main()
{
const Account Stuart("Stuart", "Owner");

Account Kevin("Kevin", "VIP");
Account Bob("Bob", "");
cout << "Kevin placing order.n";
BobaOrder K("Kevin", 3, 2, 2021, "123-456-0000", 10.4, "Bar Pa Tea", 0.0);
try
{
K.addDrink("Matcha Lemonade", 1, true);
K.addDrink("Lemon Green Tea", 1, false);
K.addDrink("Brown Sugar Oolong Tea", 2, false);
K.addDrink("Iron Goddess", 1, false);
}
catch(InvalidInput& WrongDrink)
{
WrongDrink.cause();
cout << "Not serving requested drinks. Drink order ignored.n";
}
cout.precision(5);
K.printReceipt();
cout << "Balance: $" << K.calcBalance() << endl;
cout << "Discounted Balance: $"  << applyDiscount(&K, &Kevin) << endl << endl;

cout << "Stuart placing order.n";
FoodOrder S("Stuart", 3, 2, 2021, "123-456-1111", 25.5, "Trauts Steak House", 0.0);
try
{
S.addFood("Bone-in Ribeye", 2, true);
S.addFood("Grilled Salmon", 1, false);
S.addFood("Beyond Meat Burger", 3, true);
}
catch(InvalidInput& WrongFood)
{
WrongFood.cause();
cout << "Not serving requested food. Food order ignored.n";
}
S.printReceipt();
cout << "Balance: $" << S.calcBalance() << endl;
cout << "Discounted Balance: $"  << applyDiscount(&S, &Stuart) << endl << endl;
return 0;
}

cpp文件

#include "Order.h"
#include <iostream>
using namespace std;
int DeliveryOrder::orderCount;
const float DeliveryOrder::taxRate = 0.0887;
const float DeliveryOrder::deliveryRate = 1.5;
int BobaOrder::drinksCount;
int FoodOrder::foodCount;
DeliveryOrder::DeliveryOrder(string name, int month, int day, int year, string phone, float miles, float balance = 0.0)
{
DeliveryOrder::name = name;
DeliveryOrder::month = month;
DeliveryOrder::day = day;
DeliveryOrder::year = year;
DeliveryOrder::phone = phone;
DeliveryOrder::miles = miles;
DeliveryOrder::balance = balance;
orderCount++;
}
DeliveryOrder::~DeliveryOrder()
{
cout << "DeliveryOrder destroyed.n";
}
void DeliveryOrder::printReceipt() const
{
cout << "Order Detail:" << "n";
cout << "t" << name << "n";
cout << "tDate: " << month << "/" << day << "/" << year << "n";
cout << "tPhone: " << phone << "n";
cout << "tTotal Balance: $" << balance << "n";
}
float DeliveryOrder::calcBalance() 
{
balance = balance * (1 + taxRate) + miles * deliveryRate;
return balance;
}
float DeliveryOrder::getBalance() const
{
return balance;
}
int DeliveryOrder::getOrderCount()
{
return orderCount;
}
BobaOrder::BobaOrder(string name, int month, int day, int year, string phone, float miles, string shopName, float balance)
:DeliveryOrder(name, month, day, year, phone, miles, balance)
{
BobaOrder::shopName = shopName;
}
BobaOrder::~BobaOrder()
{
cout << "BobaOrder destroyed.n";
}
void BobaOrder::printReceipt() const
{
DeliveryOrder::printReceipt();
cout << "tOrdered Drinks: " << drinksCount << endl;
}
float BobaOrder::VIPdiscount() const
{
float discount;
if (drinksCount > 10){discount = 0.85;}
else
{
if (drinksCount > 5){discount = 0.9;}
else
{
if (drinksCount > 2){discount = 0.95;}
else
{
if(drinksCount <= 2){discount = 1;}
}
}
}
return discount;
}
void BobaOrder::addDrink(string drinkName, int sameDrink = 1, bool boba=true) 
{


if (drinkName == "Matcha Lemonade"){balance = balance + 5.5 * sameDrink;}
else 
{   
if (drinkName == "Brown Sugar Oolong Tea"){balance = balance + 5 * sameDrink;}
else 
{
if (drinkName == "Lemon Green Tea"){balance = balance + 5.25 * sameDrink;}
else
{
throw InvalidInput(drinkName);
}
}       
}
if (boba==true){balance = balance + 1 * sameDrink;}
drinksCount = drinksCount + sameDrink;
}
FoodOrder::FoodOrder(string name, int month, int day, int year, string phone, float miles, string restaurantName, float balance)
:DeliveryOrder(name, month, day, year, phone, miles, balance)
{
FoodOrder::restaurantName = restaurantName;
}
FoodOrder::~FoodOrder()
{
cout << "FoodOrder destroyed.n";
}
void FoodOrder::printReceipt() const
{
FoodOrder::printReceipt();
cout << "tOrdered Foods: " << foodCount << endl;
}
float FoodOrder::VIPdiscount() const
{
float discount;
if (balance > 50){discount = 0.85;}
else
{
if (balance > 30){discount = 0.9;}
else
{
if (balance > 20){discount = 0.95;}
else
{
if(balance <= 20){discount = 1;}
}
}
}
return discount;
}
void FoodOrder::addFood(string foodName, int sides = 0, bool soup=false)
{  
if (foodName == "Bone-in Ribeye"){balance = 32 + balance;}
else
{
if (foodName == "Rack of Lamb"){balance = 28 + balance;}
else
{
if (foodName == "Grilled Salmon"){balance = 24 + balance;}
else 
{
if (foodName == "Beyond Meat Burger"){balance = 22 + balance;}
else
{
throw InvalidInput(foodName);
}
}
}
}

if (soup==true){balance = balance + 0.5;}
balance = balance + sides * 1;
foodCount++;
}
Account::Account(string username, string status)
{
Account::username = username;
Account::status = status;
}
Account::~Account()
{
cout << "Account Removed.n";
}
string Account::getStatus() const
{
return status;
}
float applyDiscount(DeliveryOrder *o, Account const *a)
{
float b = o->getBalance();
float v = o->VIPdiscount();
if (a->getStatus() == "Owner"){b = b * 0.1;}
else
{
if(a->getStatus() == "VIP"){b = b * v;}
}
return b;
}

h文件

#ifndef ORDER_H
#define ORDER_H
#include <string>
#include <iostream>
using namespace std;
class DeliveryOrder
{
private:
string name;
int month;
int day;
int year;
string phone;
float miles;
static int orderCount;
protected:
float balance;
public:
const static float taxRate;
const static float deliveryRate;
DeliveryOrder(string, int, int, int, string, float, float);
~DeliveryOrder();
void printReceipt() const;
float calcBalance();
float getBalance() const;
static int getOrderCount();
virtual float VIPdiscount() const = 0;
};
class BobaOrder : public DeliveryOrder 
{
private:
string shopName;
static int drinksCount;
public:
BobaOrder(string, int, int, int, string, float, string, float);
~BobaOrder();
void printReceipt() const;
virtual float VIPdiscount() const;
void addDrink(string, int, bool);
};
class FoodOrder : public DeliveryOrder
{
private:
string restaurantName;
static int foodCount;
public:

FoodOrder(string, int, int, int, string, float, string, float);
~FoodOrder();
void printReceipt() const;
virtual float VIPdiscount() const;
void addFood(string, int, bool);
};
class Account 
{
private: 
string username;
string status;
public:
Account(string username, string status);
~Account();
string getStatus() const;
};
class InvalidInput
{
private:
string message;

public:
InvalidInput(string s) : message("Invalid input [" + s + "].n")
{};

void cause() {cout << message;}
};
float applyDiscount(DeliveryOrder*, Account const*);
#endif 

使用调试器对您来说可能是一件新鲜事,但它会为您节省大量时间。

在调试器中运行程序会显示您的问题已经存在。

void FoodOrder::printReceipt() const
{
FoodOrder::printReceipt();
cout << "tOrdered Foods: " << foodCount << endl;
}

调用FoodOrder::printReceipt()将调用FoodOrder::printReceipt()。非常

最新更新