C ++数字值无法正确打印

时间:2016-05-05 03:31:16

标签: c++11 double decimal

主程序

    #include <iostream>
#include <iomanip>
#include "pizza.h"
using namespace std;

int main() {

    menuItem item;
menu:
    item.getMenu();//Menu Prompt

    cout << "\nI would like number: " << flush;
    int menuChoice;
    cin >> menuChoice;
    while (menuChoice < 1 || menuChoice > 3) {
        cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
    }
    if (menuChoice == 1) {
        item.getPizza();
    }
    if (menuChoice == 2) {
        item.getSandwhich();
    }
    if (menuChoice == 3) {
        item.getSide();
    }
    int ready4checkout;
    cout << "\n\n~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~" << endl;
    cout << "\nAre you ready to checkout?\n" << endl;
    cout << "checkout = 1\nAdd to the order = 2" << endl;
    cout << "\nI would like to:" << flush;
    cin >> ready4checkout;
    if (ready4checkout == 1) {
        item.addcheckout();
    }
    if (ready4checkout == 2) {
        goto menu;
    }
    cin.ignore();
    cin.get();
}

标头文件

#include<iostream>
#include <iomanip>
using namespace std;

class menuItem {
protected:
    float smallPizza = 5.99;
    float mediumPizza = 6.99;
    float largePizza = 9.99;
    float halfSandwhich = 6.00;
    float wholeSandwhich = 8.00;
    float bonelessWings = .70;
    float breadStix = 4.99;

public:
    float checkout;
    float pizzaCheck;
    float sandCheck;
    float sideCheck;

    menuItem item(float smallPizza, float mediumPizza, float largePizza, float halfSandwhich, float wholeSandwhich, float bonelessWings, float breadStix,float checkout);


    string getMenu() {
        string menu;
        cout << "~*~*~*~*~*~*~*~ Welcome to Pizza Italiano! ~*~*~*~*~*~*~*~*~\n\n" << endl;
        cout << "~~~~~~~~~~~~~~~~~ Main Menu ~~~~~~~~~~~~~~~~~~" << endl;
        cout << "Enter the value of the food that you wish to devour:\n" << endl;
        cout << "\n1.Pizza\n2.Sandwhich\n3.Appetizer" << endl;
        return menu;
    }

    string getPizza() {
        float pizzaCheck;
        string pizza;
        float pizzaSize;
pizza:
        cout << " \n~~~~~~ How would you like your pizza made? ~~~~~~\n" << endl;
        cout << "   Size" << endl;
        cout << "-----------" << endl;
        cout << "1. Large                 $9.99 \n2. Medium                $6.99 \n3. Small                 $5.99" << endl;
        cout << "\nI would like the number: " << flush;
        cin >> pizzaSize;
        cin.ignore();
        while (pizzaSize < 1 || pizzaSize > 3) {
            cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
            goto pizza;
        }
        if(pizzaSize == 1) {
            cout << "\n****You have selected a Large pizza****" << endl;
            pizzaCheck = pizzaSize;
            pizzaCheck = largePizza + pizzaCheck;
        } else if(pizzaSize == 2) {
            cout << "\n****You have selected a Medium Pizza****" << endl;
            pizzaCheck = pizzaSize;
            pizzaCheck = mediumPizza + pizzaCheck;
        } else {
            cout << "\n****You have selected a Personal pizza****" << endl;
            pizzaCheck = pizzaSize;
            pizzaCheck = smallPizza + pizzaCheck;
        }
        cin.get();
        return pizza;
    }
    string getSandwhich() {
        string sandwhich;
        float sandSize;
Sandwhich:
        cout << "~~~~~~~~~~~~ What size would you like your sandwhich to be? ~~~~~~~~~~~~~~~\n\n" << endl;
        cout << "1. Six inch             $6.00\n2. Twelve inch           $8.00" << endl;
        cout << "I would like number:" << flush;
        cin >> sandSize;
        while (sandSize < 1 || sandSize > 2) {
            cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
            goto Sandwhich;
        }
        if (sandSize == 1) {
            cout << "**** You have selected a six inch sub ****" << endl;

            sandCheck = halfSandwhich + sandSize;

        } else {
            cout << "**** You have selected a 12 inch sub ****" << endl;

            sandCheck = wholeSandwhich + sandSize;
        }
        return sandwhich;
    }
    string getSide() {
        string Appetizer;
        float side;
sides:
        cout << "~~~~~~~~~~~~~~~ Which appetizer would you like? ~~~~~~~~~~~~~~~~~\n\n" << endl;
        cout << "1. Boneless Wings            .70\n2. BreadStix               $4.99\n\n" << endl;
        cout << "I would like the number:" << flush;
        cin >> side;
        while (side < 1 || side > 2) {
            cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
            goto sides;
        }
        if (side == 1) {
            cout << "\n**** You have selected Boneless wings ****\n" << endl;
            cout << "How many wings would you like?\n" << endl;
            cout << "I would like this many wings:" << flush;
            int wings;
            cin >> wings;
            cout << "\n**** You will recieve " << wings << " wings. ****" << endl;
            side = sideCheck;
            sideCheck = bonelessWings += sideCheck;
        } else {
            cout << "\n**** You have selected an order of breadstix ****" << endl;
            side = sideCheck;
            sideCheck = breadStix += sideCheck;
        }
        return Appetizer;
    }
    float addcheckout() {
        checkout = pizzaCheck;
        cout << "\nYour total is " << checkout << endl;
        cout << "\nThank You for your purchase!";
        return checkout;
    }




};

在程序结束时,当它给出结账总额时,它给出的值类似于-1.07374e。+ 008

我还在学习如何编码,而且我对此感到困惑。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我认为您在方法pizzaCheck中隐藏了您的成员字段getPizza,因此您实际上不会写入成员字段,而是写入在该方法第一行声明的本地var。
尝试删除该局部变量。 (虽然我没有测试它)