C ++计算问题,始终返回$ 0

时间:2011-04-01 00:22:51

标签: c++ return

我必须显示并循环菜单,允许客户为花生,电影或书籍多次下单。菜单显示正常,但数量不会下降到我的代码的计算部分。每次我为任何东西输入数量并结账时,它返回$ 0。我不知道为什么会这样,我没有看到我的代码有什么问题,但显然有。你们有什么建议可以根据我的看法做什么吗?

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

//function declarations
void displayMenu();

//constant statements
const double BOOK_PRICE = 9.00;                 //price per book
const double BOOK_SHIPPING = 1.06;              //shipping per book
const double MOVIE_PRICE = 13.99;               //price per movie
const double MOVIE_SHIPPING = .05;              //shipping per movie subtotal
const double PEANUT_PRICE = 1.80;               //price of peanuts per pound
const double SHIPPING_PRICE = .50;              //shipping of peanuts per lb

int main()
{
//declaration statements
    int numBooks = 0;                               //# of books purchased
    int numMovies = 0;                              //# of movies purchased
    double numPeanuts = 0.0;                        //# of peanuts per pound
    double bookSubtotal = 0.0;                      //subtotal of books
    double movieSubtotal = 0.0;                     //subtotal of movies
    double peanutSubtotal = 0.0;                    //subtotal of peanuts
    int totalBooks = 0;                             //running total of books
    int totalMovies = 0;                            //running total of movies
    double totalPeanuts = 0.0;                      //running total of peanuts
    int userChoice = 0;                             //user input    
    double totalPrice = 0.0;                        //final price

    while (userChoice != 4)
    { 
        displayMenu();
        cout << "Enter a menu choice: ";
        cin >> userChoice;

        if (userChoice == 1)
        {
            cout << "Please enter the number of books: ";
            cin >> numBooks;
            totalBooks = totalBooks + numBooks;
        }
        else if (userChoice == 2)
        {
            cout << "Please enter the number of movies: ";
            cin >> numMovies;
            totalMovies = totalMovies + numMovies;
        }
        else if (userChoice == 3)
        {
            cout << "Please enter the pounds of peanuts as a decimal: ";
            cin >> numPeanuts;
            totalPeanuts = totalPeanuts + numPeanuts;
        }
        else if (userChoice == 4)
        {
            break;          
        }
        else 
        {
            cout << "Invalid Input" << endl;
        }

    }

//computations
    bookSubtotal = (totalBooks * BOOK_PRICE) + (totalBooks * BOOK_SHIPPING);
    movieSubtotal = (totalMovies * MOVIE_PRICE * .05) + (totalMovies * MOVIE_PRICE);
    peanutSubtotal = (PEANUT_PRICE * totalPeanuts) + (totalPeanuts * .5);
    totalPrice = bookSubtotal + movieSubtotal + peanutSubtotal;

    cout << "The total price is $" << totalPrice << endl;

    system("PAUSE");
    return 0;
}//end of main


void displayMenu()
{
    cout << "1 Books" << endl;
    cout << "2 Movies" << endl;
    cout << "3 Peanuts" << endl;
    cout << "4 Checkout" << endl;
}//end of displayMenu

1 个答案:

答案 0 :(得分:1)

问题出在cin >> - 当你说图书数为零时,你自己找到了答案。我建议您尝试在每个<< endl之后添加cout << ...。其他解决方案是在每个cout之后使用_flushall();