【发布时间】:2014-11-28 13:51:35
【问题描述】:
我目前在学校学习 C++,我们的一个项目是创建一个程序来计算预算。当我运行我的程序时,接受项目成本输入的循环将获取一次输入,然后在每次循环返回时重用该值。我已经在网上搜索了解决方案,我的老师和我一样对此感到困惑。可能是代码块有问题,但我已经用不同的编辑器试过了。如果有人知道我可以如何解决它,那就太好了。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Declares variables
float itemCount = 0;
float taxPercent = 0;
float itemCost = 0;
bool taxable = true;
float totalTaxable = 0;
float totalNontaxable = 0;
float total = 0;
//Receive user input
cout << "How many items to you have to buy: ";
cin >> itemCount;
cout << "\nWhat is the tax percentage (do not include the % sign): ";
cin >> taxPercent;
//This code runs once for every item
while (itemCount > 0){
//Receive the remaining user input
cout << "\nWhat is the cost of the item: ";
cin >> itemCost;
cout << "\nIs the item taxable (Please use either true or false): ";
cin >> taxable;
//Adds the item cost to either the taxable or nontaxable variables
if (taxable == true){
totalTaxable += itemCost;
cout << "true";
} else{
totalNontaxable += itemCost;
cout <<"false";
}
itemCount -= 1;
}
total = (totalTaxable * (1 + (taxPercent / 100))) + totalNontaxable;
cout << "\n--------------------------------------------------\n";
cout << "You must earn $";
cout << total;
cout << " to meet this budget\n\n";
}
【问题讨论】:
-
你不能在布尔值上使用
cin,使用带有 Y 和 N 的字符并进行一些字符比较
标签: c++ loops io codeblocks