难以在c ++中乘以变量。 (除非硬编码,否则c ++不想将它们相乘)

时间:2014-08-12 05:21:58

标签: c++

我最近选择了cpp并且做了几个非常小的项目来熟悉这门语言,而且我遇到了一个两难的问题,无论我在哪里看都无法弄清楚网页。

#include <iostream>
using namespace std;

int main(){
    double salesTax = 0.875, gross = 0, tax = (gross*salesTax), total = (salesTax + gross); //Variables 

    cout << "Welome to StoreMart!" << endl;

    cout << "How much is your total? "; //asks user for an amount of money spent (not including tax).
    cin >> gross; //saves total spent in the gross variable

    cout << "\n\nThat brings your total with tax to $" << total << "." << endl; //prints the users total with tax.

    cout << "Price: " << gross << "$ \n\nSales Tax: $" << tax << " @" <<
        salesTax << "% \n\nTotal: $" << total << endl; //prints out something similar to the receipts you get at Wal-Mart.

    return 0;
}

正如你所知,我正在构建一个小型的收银机类型程序,但问题是每当运行时我只得到一个看起来像这样的输出。

http://imgur.com/7ndthUX(无法上传图片直到10分,所以希望imgur很好:))

任何帮助都会受到赞赏(如果我的数学不合适,请不要介意我对让我的程序正常工作更感兴趣,而不是让数学更完美。)

5 个答案:

答案 0 :(得分:2)

您正在根据gross根据初始值0计算所有值。在输出之前,获取gross输入,然后计算税金和总金额。

其他一些修正:

  • 8.75%销售税为0.0875
  • 您只添加了salestax百分比,而不是计算值
  • 您希望将最终总数显示为2位小数

我已添加以下修复程序。

#include <iostream>
using namespace std;

int main(){
    double salesTax = 0.0875;
    double gross = 0;
    double tax = 0;
    double total = 0; //Variables with initial values

    cout << "Welome to StoreMart!" << endl;

    cout << "How much is your total? "; //asks user for an amount of money spent (not including tax).
    cin >> gross; //saves total spent in the gross variable
    tax = (gross*salesTax); //calculate tax
    total = (tax + gross); //calculate total

    cout.precision(2);
    cout << "\n\nThat brings your total with tax to $" << total << "." << endl; //prints the users total with tax.

    cout << "Price: " << gross << "$ \n\nSales Tax: $" << tax << " @" <<
        salesTax*100 << "% \n\nTotal: $" << fixed << total << endl; //prints out something similar to the receipts you get at Wal-Mart.

    return 0;

}

答案 1 :(得分:0)

当你编程时,你必须始终以与计算机“思考”相同的方式思考。代码从文件顶部到文件底部。也就是说,您在main的顶部编码的关系不是符号 - 您没有告诉计算机更新tax和total的值以反映从用户检索的新值gross。尝试从用户检索毛值后放置税和总分配语句。

答案 2 :(得分:0)

在打印新值到tax之后,您必须重新计算gross

int main(){
    double salesTax = 0.875;
    double gross = 0;
    double tax = 0;
    double total = 0;

    cout << "Welome to StoreMart!" << endl;

    cout << "How much is your total? ";
    cin >> gross;

    tax = (gross*salesTax)
    total = (salesTax + gross);

    cout << "\n\nThat brings your total with tax to $" << total << "." << endl;

    cout << "Price: " << gross << "$ \n\nSales Tax: $" << tax << " @" <<
        salesTax << "% \n\nTotal: $" << total << endl;

    return 0;
}

事实上,tax = (gross*salesTax)并未制定在grosssalesTax每次更改后都会更新的公式。它只是用当前指定的值计算tax

答案 3 :(得分:0)

我认为你遇到的根本问题是你认为通过在顶部声明变量,你可以定义整个程序中它们之间的关系。但它并没有这样做。您在第10行输入了用户的毛重,但在上面,您已经根据毛的初始值分配了总数。

您必须了解变量是在声明它们的位置计算的。如果你为每个变量使用一个单独的行,那就更清楚了。

const double salesTax = 0.875;
double gross = 0; // set to zero now but input later
double tax = 0; // no point setting the tax until we know the gross
and so on...

然后你输入总量

cin >> gross;

然后根据输入计算税额等:

tax = (gross*salesTax); // now we can actually compute the tax because we have the gross
.... and so on

答案 4 :(得分:0)

C和C ++逐行执行代码,税和总计初始化为粗nad salesTax值的旧值以纠正您的程序尝试下面的代码

#include <iostream>
using namespace std;

int main(){
    double salesTax = 0.875, gross = 0, tax = 0, total = 0; //Variables 

    cout << "Welome to StoreMart!" << endl;

    cout << "How much is your total? "; //asks user for an amount of money spent (not including tax).
    cin >> gross; //saves total spent in the gross variable

    tax = (gross*salesTax); //calculating new tax after accepting gross value from user
    total = (salesTax + gross);

    cout << "\n\nThat brings your total with tax to $" << total << "." << endl; //prints the users total with tax.

    cout << "Price: " << gross << "$ \n\nSales Tax: $" << tax << " @" <<
        salesTax << "% \n\nTotal: $" << total << endl; //prints out something similar to the receipts you get at Wal-Mart.

    return 0;
}