I am trying to do the following:

Company needs 200 pens a year. Write a program to gauge the expected cost of an item in a specific number of years. Asks for the cost of the item, numbers of years from now it will be purchased, and the rate of inflation. It outputs the estimated cost of the item after the specific period. Have the user enter the inflation rate as a percentage. Program needs to convert the percentage to a fraction, and should use a loop to estimate the price adjusted for inflation.

What I have right now is this, but I have to use a loop actually..

#include <iostream>
#include <cmath>

using namespace std;

int main() {
double CostOfItem;
double NumberYears;
double Inflation;
double Result;

cout << " What's the cost of the item ? : " ;
cin >> CostOfItem ;
cout << " Number of years from now that the item will be purchased: " ;
cin >> NumberYears ;
cout << " Enter the rate of Inflation: " ;
cin >> Inflation ;

// Result = CostOfItem*Inflation^X
Result = pow(CostOfItem * (1 + Inflation/100), NumberYears);

cout << Result << endl;

return 0;

How do I use a loop here? Can someone tell me in words how the loop is supposed to look like? I think I have to initialize to 0 first. Then somehow implement the equation... no idea.
Help is appreciated!

Recommended Answers

All 5 Replies

How do you do it on paper? The parts where you find yourself repeating the same task will fall into a loop in code.

Just solve the equation. But you just do that once with the input that was given to you. Nothing to repeat there.

What do you want to repaet here, the whole program perhaps? There are two forms of loops - While loop and the For loop but i think it would be much simpler if you just googled them, i mean its really a basic thing and its really not hard to find an example.

int number_of_repetitions = 10;

for(int i = 0; i < number_of_repetitions; i++)
  // insert your code here
int number_of_repetitions = 10;

for(int i = 0; i < number_of_repetitions; i++)
  // insert your code here

What do you mean by "number of repetitions"? I don't think the hwole code has to be repeated. I think the number of pens has to go there, too. The book just states what I posted in the OP. I am supposed to use a loop for this. But, I wasn't able to figure out how to do that with a loop, so I just did it this way. I suppose a loop could print out the price for each year. I guess that's what they mean - that the calculation goes inside a loop because you have multiple items.

Be a part of the DaniWeb community

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