2016-05-05 53 views
0

主程序C++数字值未正确打印

#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.07374 e。+ 008

我还在学习如何编写代码,而且我非常沮丧于此。任何帮助将非常感激!

+0

从来没有见过'goto'很长一段时间!你应该避免使用它,因为它使代码难以阅读;特别是随着代码的增长。 –

回答

1

我想你在你的方法getPizza中影响你的成员字段pizzaCheck,这样你实际上不会写入成员字段,而是写入在该方法第一行声明的局部变量。
尝试删除该局部变量。 (虽然我没有测试它)