c++ - 我需要为学校项目找出简单的循环
问题描述
我们应该做一个零售收银员,但我根本无法弄清楚循环。到目前为止,我们只学习了简单的选择和重复语句,我知道这就是我所需要的,但我似乎无法弄清楚。
入门代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subtotal = 0.0;
double salesTax = 0.0;
double totalSale = 0.0;
int anotherSale = 1;
// Loop for repeat sales
// Enter the first Product ID for the first sale (-1 to exit)
// Main loop for each sale
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
// Get next Product ID
// Print properly formatted output for each sale
// Another sale?
// Display how much is in the cash drawer at the end
}
到目前为止我所拥有的:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subTotal = 0.0; // for receipt purposes
double salesTax = 0.0; // for receipt purposes
double totalSale = 0.0; // for receipt purposes
int anotherSale = 1;
double taxRate = 0.075; // default tax rate
// Loop for repeat sales
while ()
{
// Enter the first Product ID for the first sale (-1 to exit)
cout << "Enter the first Product ID: ";
cin >> productID;
// Main loop for each sale
while (productID > 0)
{
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
switch (productID)
{
case 101:
price = 65.00;
taxRate = 0.075;
break;
case 102:
price = 12.50;
taxRate = 0;
break;
case 103:
price = 24.50;
taxRate = 0.00;
break;
case 104:
price = 38.75;
taxRate = 0.075;
break;
case 105:
price = 17.80;
taxRate = 0.075;
break;
case 106:
price = 16.50;
taxRate = 0;
break;
case 107:
price = 42.85;
taxRate = 0.075;
break;
case 108:
price = 32.99;
taxRate = 0.075;
break;
case 109:
price = 28.75;
taxRate = 0.075;
break;
case 110:
price = 51.55;
taxRate = 0;
break;
default:
cout << "INVALID PRODUCT ID: Product ID not found." << endl;
}
cout << "Enter the quantity: ";
cin >> quantity;
subTotal += price * quantity;
salesTax += price * quantity * taxRate;
totalSale = subTotal + salesTax;
// Get next Product ID
cout << "Enter the next Product ID: ";
cin >> productID;
}
// Print properly formatted output for each sale
// Another sale?
}
// Display how much is in the cash drawer at the end
}
任何帮助表示赞赏,提前谢谢你们。
解决方案
循环语句的基本方面是重复相同的指令集,直到满足指定的条件。您已经成功地找出了您正在检查的代码的内部循环if productID>0
。您需要对外部循环执行相同的操作,并对anotherSale
ie施加类似的条件while(anotherSale!=0)
。每次内循环完成时,只需询问用户的值anotherSale
; 如果用户输入 0,则循环应该中断。
推荐阅读
- python - HTML 表单忽略文件部分
- c# - 如何更改大小 GridEditMode.Popup 并添加不可见记录
- maven - 如何在 Maven 项目中自动接受 Gradle Build Scan ToS?
- oracle-apex - 如何获取依赖对象进行部署 - APEX 21.1
- python - 使用 selenium 和 python 或 java 识别 Jira 问题类型(如 Epic/Story/Bug/Task)的 XPATH
- javascript - 我想在 googlechat 空间中获取待办事项列表的 ID
- html - 如何减少条件状态/进度条交付的代码
- unit-testing - toBeInTheDocument() 和 toBeVisible() 有什么区别?
- reactjs - 将来自api的数据设置为draftjs的editorState的初始状态
- google-maps - 多个地图标记