Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
#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为true,for循环将继续执行。在您的原始示例中,将i设置为months,这取决于您的输入,将大于或等于零。这将使条件i<0为假,这将完全跳过循环。

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

10-16 05:05