Hello all, I am attempting to learn C++ on my own. I am going through the book C++ Primer by Steven Prada, in case anyone has the book. At the end of every chapter, they give programming problems and I am having trouble with one of the problems. Essentially the problem ask to create a program that calculates the value of two women's investments. One that receives standard interest and one that receives compound interest, and to determine when the value of the investment receiving 5% compound interest exceeds the value of the investment receiving 10% standard interest. Here is my code.

#include <iostream>
int main()
	using namespace std;
	int Cleo = 100;
	int Daphne = 100;
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = Cleo, j = Daphne; j > i; i++, j++)
		j += (.1 * 100);
		i += (.05 * i);
	cout << "In " <<count<< " years, Cleo's"
	     << "investment exceeds Daphne's investment.\n";	
	cout << "Cleo's investment is worth "<< Cleo << " , while"
	     << " Daphne's investment is worth " << Daphne << endl;
	return 0;
8 Years
Discussion Span
Last Post by arinkverma

the loop on line 10 will never execute because the condition j > i is always false -- they are initialized to the same value (100).


This is a counting problem, which usually leads one to use a for loop. Really, what will cause the loop to end is a condition unrelated to the counting, per se. You need to be comparing each woman's total balance, after the interest has been added.

Also, the problem statement seems to be missing some information. In this context, I take "standard interest" to mean 10% of balance gets added annually, where "compound interest" gets some portion added on a shorter time span (monthly, weekly, daily?), so that some interest is gaining interest as the year progresses. Hmm, I smell a loop within a loop!

Think about the condition used in for loop.
j>i will be 0 always.

And your logic is not as required by the situation you mention.
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.