【问题标题】:Loop accepts first set of input and then reuses that value循环接受第一组输入,然后重用该值
【发布时间】: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


【解决方案1】:

正如评论中所说,您不能在布尔值上使用 cin。来自文档:

标准输入流是由环境决定的字符源。

这是您的固定代码(将接受Y,仅此而已):

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //Declares variables
    float itemCount = 0;
    float taxPercent = 0;
    float itemCost = 0;
    char taxable = '';
    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 Y for Yes): ";
        cin >> taxable;

        //Adds the item cost to either the taxable or nontaxable variables
        if (taxable == 'Y'){
            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";
}

【讨论】:

  • 谢谢,现在可以完美运行了。但是,我必须在声明应税变量时添加一个字符。不赋值就无法编译。
猜你喜欢
  • 2019-08-11
  • 1970-01-01
  • 2020-11-09
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 2017-01-30
  • 2013-01-06
相关资源
最近更新 更多