#include <iostream>
int main()
{
std::cout << "Nerdrvox skzbnakan gumar = ";
double invmoney = 0;
std::cin >> invmoney;
std::cout << std::endl << "Nerdrvox skzbnakan gumar = " << invmoney;
std::cout << std::endl << "Qani tarov = ";
int years = 0;
std::cin >> years;
std::cout << std::endl << "Qani tarov = " << years;
/*
* After this point the cmd says press any key to continue, after pressinga key
* cmd closes. However it is supposed to do this.
*/
double summary = invmoney;
int months = 12 * years;
for (int i = months; i < 0; i--)
{
std::cout << "Month " << i - months << std::endl << "Invested money"
<< summary << std::endl;
double percent = summary * 0.1;
std::cout << "Add percent" << percent << std::endl;
summary += percent;
std::cout << "Sum for month " << i - months << "is " << summary;
}
return 0;
}
你能告诉我为什么以及如何解决它?
答案 0 :(得分:3)
是的,这是因为你的for循环没有执行。
for (int i = months; i<0; i--)
需要
for (int i = months; i>0; i--)
for
具有以下结构:
for (initialization; condition; increase) statement;
只要condition
为真,for循环就会继续执行。在原始示例中,i
被设置为months
,根据您的输入,i<0
将大于或等于零。这会使条件{{1}}为false,这将完全跳过循环。
答案 1 :(得分:1)
它做它应该做的事情
for (int i = months; i<0; i--)
如果月份的值为正,则循环将不会运行
将其更改为
for (int i = months; i>0; i--)
答案 2 :(得分:1)
您的循环条件:
for (int i = months; i<0; i--)
说只要i小于零就会循环,因此它甚至不会运行第一个循环,因为我大于或等于零(假设用户输入有效年份)。