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);
		++count;
	}
	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;
}

Recommended Answers

All 4 Replies

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

ah, thanks. I appreciate it

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.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.