我要做的是使用类的向量而不是并行向量来将所有数据存储在代码的“收据”部分中。
我首先使用并行向量来执行此操作(这是代码中编写的内容),但现在我需要使用类的向量,我不知道该怎么做。这样做的正确方法是什么?
#include <iostream>
#include <string>
#include <cmath>
#include <cctype>
#include <iomanip>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
vector <string> flavor;
vector <string> sizes;
vector <double> price;
void printWelcomeMessage() {
cout << "Welcome to My Frozen Yogurt!" << endl;
cout << endl;
cout<< endl;
}
char toLowerCase (char c) {
char c_lower = c;
if ( (c >= 'A') && (c <= 'Z')) c_lower = c + 32;
return c_lower;
}
void toLowerCase (string& str) {
for (int k=0; k< str.size(); k++) {
str[k] = toLowerCase (str[k]);
}
return;
}
double getYogurtSize(string& yogurtSize) {
double subtotal=0.;
const double TAX= 0.0875, SMALLSIZE= 2.19, MEDIUMSIZE = 3.49, LARGESIZE = 4.49;
cout << "What size would you like? Please enter small, medium, or large: ";
getline(cin,yogurtSize);
toLowerCase(yogurtSize);
//push_back to enter content into vector
sizes.push_back(yogurtSize);
//pop_back to delete any times the user mispells the size
if (yogurtSize != "small" && yogurtSize != "medium" && yogurtSize != "large")
sizes.pop_back();
while (yogurtSize != "small" && yogurtSize != "medium" && yogurtSize != "large") {
cout << "What size would you like? Please enter small, medium, or large: ";
getline (cin, yogurtSize);
sizes.push_back(yogurtSize);
}
if (yogurtSize== "small") {
subtotal += SMALLSIZE;
price.push_back(SMALLSIZE); }
else if (yogurtSize == "medium") {
subtotal += MEDIUMSIZE;
price.push_back(MEDIUMSIZE); }
else if (yogurtSize=="large") {
subtotal+= LARGESIZE;
price.push_back(LARGESIZE); }
return subtotal;
}
void getYogurtFlavors(string& flavor1, string& flavor2, string& flavor3) {
cout << "Enter flavor 1: ";
getline (cin, flavor1);
toLowerCase (flavor1);
//use push_back to into content into vector
flavor.push_back(flavor1);
flavor.push_back("-");
cout << "Enter flavor 2: ";
getline (cin, flavor2);
toLowerCase(flavor2);
flavor.push_back(flavor2);
flavor.push_back("-");
cout << "Enter flavor 3: ";
getline (cin, flavor3);
toLowerCase(flavor3);
flavor.push_back(flavor3);
flavor.push_back("-");
}
// function printOrder prints out the flavors of each input
void printOrder (string yogurtSize, string flavor1, string flavor2, string flavor3, int){
int orderNumber= 0;
orderNumber++;
cout<< endl;
cout << "************************" << endl;
cout << "Order: " << orderNumber << flavor1.substr(0,4) << "-" << flavor2.substr(0,4) << "-"<< flavor3.substr(0,4) << " " << yogurtSize << endl;
cout << "************************" << endl;
cout << endl;
return;
}
bool addAnotherOrderQ(){
string order;
string yes = "yes";
string no = "no";
bool moreorders= true;
cout << "Would you like another order? ";
getline (cin, order );
while (order != yes && order != no) {
cout << "Would you like another order? Please enter yes or no: ";
getline (cin, order);
cout << endl;
}
if (order == no){
moreorders= false;
cout << endl;
}
return moreorders;
}
void printTotalCosts (double& subtotal, int& orderNumber) {
const double TAX= 0.0875;
int setpre= 2;
int totalwidth=15;
int taxwidth= 9;
int subwidth =12;
int itemwidth= 6;
cout << "===================== Receipt ===========================" << endl;
cout << endl;
// for for loops to print the size, flavors, and price in the receipt screen, and in the file
for (int i=0; i< flavor.size(); i++) {
cout << flavor[i].substr(0,4)<< " ";
}
for (int i=0; i< sizes.size(); i++) {
cout << sizes[i]<< " " ;
}
for (int i=0; i < price.size(); i++) {
cout << price[i] << endl;
}
cout << "Number of items: " << setw(itemwidth) << orderNumber << endl;
cout << "Subtotal: " << "$" << fixed << setprecision (setpre) << setw (subwidth) << subtotal << endl;
cout << "Tax (8.75%): " << "$"<< setw(taxwidth) << TAX * subtotal << endl;
cout << "Total: " << "$" << setw(totalwidth) << (TAX *subtotal) + subtotal << endl;
cout << endl;
cout << "=========================================================" <<endl;
cout << endl;
}
class YogurtOrder {
public:
YogurtOrder (); //default constructor
YogurtOrder (string, string, string, string, double); //constructor with parameters: size, flavor1, flavor2, flavor3, and price
string getSize ();
string getMixedFlavor ();
double getPrice ();
private:
string size;
string flavor1, flavor2, flavor3;
double price;
};
YogurtOrder:: YogurtOrder() {}
YogurtOrder:: YogurtOrder (string s, string first, string second, string third, double p){
s= size;
first= flavor1;
second= flavor2;
third= flavor3;
p= price;
}
string YogurtOrder:: getSize () {
return size;
}
string YogurtOrder:: getMixedFlavor() {
return flavor1, flavor2, flavor3;
}
double YogurtOrder:: getPrice() {
return price;
}
int main () {
const double TAX= 0.0875;
int setpre= 2;
int totalwidth=15;
int taxwidth= 9;
int subwidth =12;
int itemwidth= 6;
//Print the welcome message
printWelcomeMessage();
// initialize the loop variables
bool more_order = true;
int orderNumber= 0;
// varible for cost
double subtotal= 0.;
//variable for suze and flavors default intitialized to ""
string yogurtSize, flavor1, flavor2, flavor3, order;
//continye to get order until the user is done
while (more_order) {
//increment order number
orderNumber++;
//update the size and subtotal
subtotal= subtotal +getYogurtSize(yogurtSize);
//update the flavors
getYogurtFlavors(flavor1, flavor2, flavor3);
// print the current order
printOrder (yogurtSize, flavor1, flavor2, flavor3, orderNumber);
//determine whther or not to order more
more_order= addAnotherOrderQ();
}
// Print out the subtotal, tax, and total
vector <YogurtOrder> flavors;
printTotalCosts(subtotal, orderNumber);
return 0;
}
答案 0 :(得分:1)
您需要制作一类必要的数据成员,如下所示:
class FrozenYogurt
{
private:
string flavor;
string size;
double price;
public:
//constructor
public FrozenYogurt(string f,string s,double p)
{
this.flavor=f;
this.size=s;
this.price=p;
}
//Getters
public string getFlavor(){return this.flavor;}
public string getSize(){return this.size;}
public double getPrice{return this.price;}
....//All other necessary functions
}
然后像这样制作这个类的向量:
vector<FrozenYogurt> listOfItems;
你可以制作FrozenYogurt的对象并像这样插入这个矢量列表
string flavor="somthing";
string size="somthing";
double price=123;
FrozenYogurt item(flavor,size,price);
listOfItems.push_back(item);
现在您可以访问以下内容:
listOfItems[index].getSize();
listOfItems[index].getFlavor();
listOfItems[index].getPrice();