我在编写控制台应用程序以创建一个简单的程序来解决养老金的数学方程时遇到问题

I'm having trouble writing a console application to create a simple program to solve a math equation for superannuation

本文关键字:解决 程序 养老金 简单 问题 遇到 方程时 一个 控制台 应用程序 创建      更新时间:2023-10-16

谁能帮我一下?我已经绞尽脑汁一个多小时了,还是不能使它工作。这是在C++,我已经学了一点,但我还是个新手…

int main()
{
 double rate, amount,time, S;
    cout << "Insert the time of the super: ";
    cin >> time;
    cout << "Insert the rate (as a decimal, eg 1% AKA 101% = 1.01): ";
    cin >> rate;
    cout << "Insert the amount $: ";
    cin >> amount;
    S =("amount * (rate ^ time - 1)", pow(rate,time));
    cin >> S;
    cout << "The total amount is: " << "S /(rate - 1)" << endl;
    system("PAUSE");
    return 0;
}

我没有得到一个编译错误,但我永远不能得到答案,从它

你"永远得不到结果"因为你用逗号操作符将S设置为pow的结果然后用

行再次赋值给它
cin >> S;

正在等待您输入另一个数字。

你有两个主要问题。以下是更新后的代码,并对更改的部分进行了注释:

int main()
{
    double rate, amount,time, S;
    cout << "Insert the time of the super: ";
    cin >> time;
    cout << "Insert the rate (as a decimal, eg 1% AKA 101% = 1.01): ";
    cin >> rate;
    cout << "Insert the amount $: ";
    cin >> amount;
    S = amount * pow(rate, time - 1); // take away the quotes and don't make pow seperate
    cout << "The total amount is: " << (S /(rate - 1)) << endl; // do the calculation and output it
    system("PAUSE");
    return 0;
}

请记住,引号"like this"内的内容是字符串字面量,因此"4 * 4"是字符串,但4 * 4(请参阅没有引号)进行乘法运算,产生数字16

我认为你不应该像现在这样给S赋值。S被声明为双精度,你一开始给它赋值一个字符串。当您输出结果时,还将计算结果括在引号中。你应该简单地计算<<S/(速率-1);//不带引号或cout将直接输出字符串