c++ - 为什么我的 C++ 程序在这一点之后不工作

标签 c++

<分区>

#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;
}

能否请您告诉我为什么以及如何解决它?

最佳答案

是的,这是因为您的 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 false,这将完全跳过循环。

关于c++ - 为什么我的 C++ 程序在这一点之后不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24702139/

相关文章:

c++ - 调用 dll 函数 C++ rad studio xe3

c++ - 为什么存储在数组中的 double 值会消失?

c++ - 试图理解 GCC-XML 输出变量的含义

c++ - 使用和不使用默认构造函数声明泛型类有什么区别?

c++ - OpenGL 问题 : cannot render geometry on screen

c++ - libtorrent-rasterbar 无法使用磁力链接下载元数据

c++ - 如何使用 boolean 数组或 LogicalVector Rcpp::List getPIcvalue() 和 RcppExport SEXP mypackage5_getPIcvalue()

c++ - 如何使用 tbb 实现并发哈希表,将键映射到不同类型的值?

c++ - STL max_element 的复杂度

c++ - 移动指针的右值引用