2017-02-17 41 views
0

我很难解决这个问题。我需要创建一个循环重复5次。该循环需要取整数x并乘以0.2并显示结果。然后它需要取得这个结果并且乘以0.2并显示结果。它需要做5次。循环显示一个数字增加0.2%5次

float credithours; 
float tuition = 274.41; 


cout << "How many credit hours are you taking this semester? " << endl; 
cin >> credithours; 
cout << "Your current semester tuition is " << credithours * tuition << endl; 

float tuitionfive = credithours * tuition * 2; 

cout << "Your tuition for the year is " << tuitionfive << endl; 

float tuitiontotal = (tuitionfive * .2) + (tuitionfive); 


for (float x = 1; x <= 5; x++) { 
    x = tuitiontotal * .2 + tuitionfive; 
    cout << x << endl; 
} 

编辑*****

我在这里固定的帮助后的代码,这是最终的结果:

float credithours; 
float tuition = 274.41; 



cout << "How many credit hours are you taking this semester? " << endl; 
cin >> credithours; 

float semestertuition = credithours * tuition; 

cout << "Your current semester tuition is " << semestertuition << endl; 

float tuitionfive = semestertuition * 2; 

cout << "Your tuition for the year is " << tuitionfive << endl; 

cout << "Your tuition for the next 5 years is: " << endl; 

for (int i = 0; i < 5; ++i) 
{ 
    tuitionfive = tuitionfive * .2 + tuitionfive; 
    cout << tuitionfive << endl; 
} 

我没有使用指数然而,正如我需要做更多的研究来更多地理解它。再次感谢您的帮助。

+0

那么,是你的码?除了问这里之外,你实际上做了什么来解决你的问题? – Kupto

+0

我刚添加了代码。编辑我的原始帖子。我得到了循环重复5次,我只是不知道如何输入数学方程,所以方程重复5次。 – AMD

回答

1

这似乎是问题所在,您将计数器变量指定为不恰当的位置。良好的做法是使用整数计数器(不浮动),并将其命名为i,让大家都知道它是一个反...

你可能会想这样的事情:

float tuitiontotal = tuitionfive * .2 + tuitionfive; 

for (int i = 0; i < 5; ++i) 
{ 
    tuitiontotal = tuitiontotal * .2 + tuitionfive; 
    cout << tuitiontotal << endl; 
} 

。注意,变量是tuitiontotal是什么增加五倍。但你必须弄清楚你自己想要什么的实际数学......我似乎不明白.2做什么:]

+0

这是一个非常简单的修复程序。非常感谢你! – AMD

+1

.2年增加20%的学费每年增加 – AMD

+0

UR欢迎,现在我明白了... – Kupto

1

除了Kupto的答案,你需要有介意C++中的float是一个近似值。

由于值0.2乘以新值,我建议您使用它的指数值。

是这样的:tuitiontotal *= pow(0.2, i)

使用POW将减少计算错误

你需要包括CMATH库,并使用战俘一样:std::pow(2,3)导致8

+0

我会研究更多。我不知道浮法是一个近似值。谢谢 – AMD