验证错误我无法理解

时间:2016-04-03 21:21:57

标签: c++ debugging

我在测试程序中验证“价格验证”时收到错误,我不确定为什么。我将在下面附上Header / CPP / Tester。任何帮助表示赞赏!谢谢。

HEADER:

$data = json_decode($data,true);

CPP:

#ifndef SICT_AMAPRODUCT_H__
#define SICT_AMAPRODUCT_H__
#include <iostream>
#include "ErrorMessage.h"
#include "Product.h"

namespace sict {

    class AmaProduct : public Product {
    private:
            //Holds a single character to tag the records as Perishable or non-Perishable product in a file.   
            char fileTag_;
            //Unit of Measurement
            char unit_[11];

    protected:
            ErrorMessage err_;

    public:
            AmaProduct(char = 'N');

            //Public member functions

            //returns a constant pointer to the unit_ member variable.
            //returning function from Header as did not work in CPP
            const char* unit()const { return unit_; };
        /*  Copies the incoming value string into the unit_ string.
            Make sure copying does not pass the size of the unit_ array.*/
            void unit(const char* value);

            std::fstream& store(std::fstream& file, bool addNewLine = true)const;
            std::fstream& load(std::fstream& file);
            std::ostream& write(std::ostream& os, bool linear)const;
            std::istream& read(std::istream& istr);
    };
}
#endif

TESTER:

#include "AmaProduct.h"
#include <cstring>
#include <iomanip>
#include <fstream>

using namespace std;

namespace sict {

    AmaProduct::AmaProduct(char tag) {
            fileTag_ = tag;

    }

    void AmaProduct::unit(const char* value) {
            strncpy(unit_, value, 11);
    }

    std::fstream& AmaProduct::store(std::fstream& file, bool addNewLine)const {
            file << fileTag_ << "," << sku() << "," << name() << ","
                    << price() << "," << taxed() << "," << quantity() << ","
                    << unit_ << qtyNeeded();
            if (addNewLine) {
                    file << endl;
            }
            return file;
    }

    std::fstream& AmaProduct::load(std::fstream& file) {
            char s[MAX_SKU_LEN];
            char n[20];
            double p;
            int t = 0;
            int q;
            int qN;

            file.getline(s, MAX_SKU_LEN, ',');
            sku(s);
            file.getline(n, 20, ',');
            name(n);
            file >> p;
            price(p);
            file.ignore();
            file >> t;
            if (t) {
                    taxed(true);
            }
            else {
                    taxed(false);
            }
            file.ignore();
            file >> q;
            quantity(q);
            file.ignore();
            file >> qN;
            qtyNeeded(qN);
            file.ignore();

            return file;

    }

    std::ostream& AmaProduct::write(std::ostream& os, bool linear)const {
            if (!err_.isClear()) {
                    os << err_.message();
                    return os;
            }

            else if (linear) {
                    //setfill?
                    os << setfill(' ') << left << setw(MAX_SKU_LEN) << sku() << '|'
                            << setw(20) << name() << '|' << right
                            << setw(7) << fixed << setprecision(2) << cost() << '|'
                            << setw(4) << quantity() << '|' << left << setw(10)
                            << unit() << right << setw(4) << qtyNeeded();
            }
            else {
                    os << "Sku:" << sku() << endl
                            << "Name:" << name() << endl
                            << "Price:" << price() << endl;
                    if (taxed() == true) {
                            os << "Price after tax:" << (TAX + 1) * price() << endl;
                    }
                    else {
                            os << "Price after tax: N/A" << endl;
                    }

                    os << "Quantity on hand:" << quantity() << " " << unit() << endl
                            << "Quantity needed:" << qtyNeeded();

            }
            return os;

    }

    std::istream& AmaProduct::read(std::istream& istr) {
            char s[MAX_SKU_LEN];
            char n[20];
            char u[11];
            char t;
            double p;
            int q;
            int qN;

            if (istr.fail() == false) {
                    cout << "Sku: ";
                    istr.getline(s, MAX_SKU_LEN, '\n');
                    //false or ! ******
                    if (istr.fail() == false) {
                            sku(s);
                    }

                    cout << "Name: ";
                    istr.getline(n, 20, '\n');
                    if (istr.fail() == false) {
                            name(n);
                    }

                    cout << "Unit: ";
                    istr.getline(u, 11, '\n');
                    if (istr.fail() == false) {
                            unit(u);
                    }

                    cout << "Taxed? (y/n): ";
                    istr >> t;
                    if (t == 'Y' || t == 'y') {
                            taxed(true);
                    }
                    else if (t == 'N' || t == 'n') {
                            taxed(false);
                    }
                    else {
                            err_.message("Only (Y)es or (N)o are acceptable");
                            istr.setstate(ios::failbit);
                    }

                    if (err_.isClear()) {
                            cout << "Price: ";
                            istr >> p;
                            if (istr.fail()) {
                                    err_.message("Invalid Price Entry");
                            }
                            else {
                                    price(p);
                            }

                    }
                    if (err_.isClear()){
                            cout << "Quantity On hand: ";
                            istr >> q;
                            if (istr.fail()) {
                                    err_.message("Invalid Quantity Entry");
                            }
                            else {
                                    quantity(q);
                            }

                    }
                    if (err_.isClear()) {
                            cout << "Quantity Needed: ";
                            istr >> qN;
                            if (istr.fail()) {
                                    err_.message("Invalid Quantity Needed Entry");
                            }
                            else {
                                    qtyNeeded(qN);
                            }
                    }

            }
            return istr;

    }

  }

失败的地方:

#include "AmaProduct.h"
using namespace sict;
using namespace std;
void piv(const char* sku, const char* name, const char* unit = "", char Tx = '\0',
    const char* price = "", const char* qty = "", const char* qtyNd = "",
    const char* date = "") {
    cout
            << "Enter the following: " << endl
            << "Sku: " << sku << endl
            << "Name: " << name << endl
            << "Unit: " << unit << endl;
    if (Tx) cout << "Taxed: " << Tx << endl;
    if (price[0]) cout << "Price: " << price << endl;
    if (qty[0]) cout << "Quantity on hand: " << qty << endl;
    if (qtyNd[0]) cout << "Quantity needed: " << qtyNd << endl;
    if (date[0]) cout << "Expiry date: " << date << endl;
    cout << endl;
}
void dumpFile(const char* fname) {
    ifstream f(fname);
    char ch;
    while (!f.get(ch).fail()) {
            cout.put(ch);
    }
    f.clear();
    f.close();
}
void _pause() {
    cout << "****press enter to continue...";
    cin.ignore(1000, '\n');
}

int main() {
    fstream prdfile("amaPrd.txt", ios::out);
    AmaProduct amaPrd;
    bool ok = true;
    int i;
    cout << "--AmaProduct test:" << endl;
    cout << "----Taxed validation test:" << endl;
    piv("abc", "abc", "abc", 'a');
    cin >> amaPrd;
    if (cin.fail()) {
            cin.clear();
            cin.ignore(2000, '\n');
            cout << "Passed!" << endl
                    << "Message shoule be:  Only (Y)es or (N)o are acceptable" << endl
                    << "Your Error message: " << amaPrd << endl;
    }
    else {
            ok = false;
            cout << "Taxed validation failed" << endl;
    }
    _pause();
    if (ok) {
            cout << "----Price validation test:" << endl;
            piv("abc", "abc", "abc", 'y', "abc");
            cin >> amaPrd;
            if (cin.fail()) {
                    cin.clear();
                    cin.ignore(2000, '\n');
                    cout << "Passed!" << endl
                            << "Message shoule be:  Invalid Price Entry" << endl
                            << "Your Error message: " << amaPrd << endl;
            }
            else {
                    ok = false;
                    cout << "Price validation failed" << endl;
            }
            _pause();
    }
    if (ok) {
            cout << "----Quantity validation test:" << endl;
            piv("abc", "abc", "abc", 'y', "10", "abc");
            cin >> amaPrd;
            if (cin.fail()) {
                    cin.clear();
                    cin.ignore(2000, '\n');
                    cout << "Passed!" << endl
                            << "Message shoule be:  Invalid Quantity Entry" << endl
                            << "Your Error message: " << amaPrd << endl;
            }
            else {
                    ok = false;
                    cout << "Quantity validaton failed" << endl;
            }
    }
    _pause();
    if (ok) {
            cout << "----Quantity Needed validation test:" << endl;
            piv("abc", "abc", "abc", 'y', "10", "10", "abc");
            cin >> amaPrd;
            if (cin.fail()) {
                    cin.clear();
                    cin.ignore(2000, '\n');
                    cout << "Passed!" << endl
                            << "Message shoule be:  Invalid Quantity Needed Entry" << endl
                            << "Your Error message: " << amaPrd << endl;
            }
            else {
                    ok = false;
                    cout << "Quantity Needed validaton failed" << endl;
            }
    }
    _pause();
    if (ok) {
            cout << "----Display test, the output of the Program and yours must match:" << endl;
            piv("1234", "box", "kg", 'y', "123.45", "1", "5");
            cin >> amaPrd;
            cin.ignore(1000, '\n');
            cout << "--Linear------------" << endl;
            cout << "Program: 1234   |Box                 | 139.50|   1|kg        |   5|" << endl;
            cout << "  Yours: " << amaPrd << endl;
            cout << "--Form Display------" << endl;
            cout << "--Program: " << endl;
            cout << "Sku: 1234" << endl;
            cout << "Name : box" << endl;
            cout << "Price : 123.45" << endl;
            cout << "Price after tax : 139.50" << endl;
            cout << "Quantity On Hand : 1 kgs" << endl;
            cout << "Quantity Needed : 5" << endl;
            cout << "--Yours: " << endl;
            amaPrd.write(cout, false) << endl;
    }
    _pause();
    if (ok) {
            cout << "----Storage and loading test, the output of the Program and yours must match:" << endl;
            AmaProduct tprd;
            amaPrd.store(prdfile);
            amaPrd.store(prdfile);
            prdfile.close();
            cout << "--Store AmaProduct, program: " << endl
                    << "N,1234,box,123.45,1,1,kg,5" << endl
                    << "N,1234,box,123.45,1,1,kg,5" << endl;
            cout << "--Store AmaProduct, yours: " << endl;
            dumpFile("amaPrd.txt");
            cout << "--Load AmaProduct: " << endl;
            prdfile.open("amaPrd.txt", ios::in);
            prdfile.ignore(2);
            tprd.load(prdfile);
            cout << "Program: 1234   |box                 | 139.50|   1|kg        |   5|" << endl;
            cout << "  Yours: " << tprd << endl;
            prdfile.clear();
            prdfile.close();
    }return 0;
}

我不明白为什么我的价格验证失败了。 (“价格验证失败”)。

0 个答案:

没有答案