gpt4 book ai didi

C++ 计算问题,总是返回 $0

转载 作者:行者123 更新时间:2023-11-28 08:21:55 26 4
gpt4 key购买 nike

我必须显示并循环显示一个菜单,允许客户多次订购花生、电影或书籍。菜单显示正常,但数量不会下降到我的代码的计算部分。每次我输入任何东西的数量并结账时,它都会返回 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

最佳答案

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

关于C++ 计算问题,总是返回 $0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5507938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com