Looping problem

Hello in new to C++ so this will probably be an easy one so forgive me for that. Anyways I am having trouble with a loop the question goes like this :
Write a program to gauge the expected cost of an item in a specific number of years. The program asks for the cost of the item, the number of years from now that the item will be purchased, and the rate of inflation. The program then outputs the estimated cost of the item after the specified period. Have the user enter the inflation rate as a percentage, such as 5.6(percent). Your program should then convert the percentage to a fraction, such as 0.056, and should use a loop to estimate the price adjusted to inflation. (hint: use a loop)
I know how loops work but how does a loop work in this problem? Im not asking for anyone to solve it but any help would greatly appreciated. I've done research online, read my book, asked my proffesor. If it helps this is my code for the moment
#include <iostream>
using namespace std;

int main()
{
int years;
double cost, inflaRate;

cout << "Enter the cost, number of years and inflation rate: ";
cin >> cost >> years >> inflaRate;

while

system("pause");
return 0;
}

Also if this helps this is what the final output looks like:
Enter the cost, number of years and inflation rate: 10.0 2 20.0

This item of $10.00 will cost $14.40 after 2 years.

Like I said any help would be greatly appreciated an sorry if this is a long posting.
Hiya, welcome to cplusplus forums ^^
When posting code, please use [ code ] and [ /code ] brackets (without the spaces) to improve the readability of your code.

As for your problem, you should read up on "The For Loop" here: http://cplusplus.com/doc/tutorial/control/


After reading the content of that link, you can reach your goal by the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
	int years;
	double cost, inflaRate;

	cout << "Enter the cost, number of years and inflation rate: ";
	cin >> cost >> years >> inflaRate;

	inflaRate /= 100; //turn inflaRate from a percentage into a decimal

        cout << "This item of " << cost;

	//use a for loop here to loop the the amount of times that is stored in 'year'.
	//each loop increment cost the amount of inflation (cost += (cost*inflaRate)

	cout << " will cost " << cost << " after " << years << "." << endl;

	system("pause");
	return 0;
}
Last edited on
Okay sorry about that I will remember that for next time and thanks for pointing out the article but a couple of questions. What do you mean by loop the the amount of times that is stored in 'year'?
and does (cost += (cost*inflaRate) go in the increment part of the for loop? Sorry like i said im really new to this.
What do you mean by loop the the amount of times that is stored in 'year'?
I meant.. If years equals X, loop X times, which can be done easily with for loops.

and does (cost += (cost*inflaRate) go in the increment part of the for loop
That would go inside the loop, if that's what you mean.
Last edited on
Okay so will the for loop look something like this:

for (years = 2; year > 0; cost += (cost*inflaRate))

Am I somewhere in the correct path, or at least getting closer?
Well, the fact that you have the word for is a good start. ;)

1
2
3
4
for(expr1;expr2;expr3)
{
    /*code to do per run*/;
}

expr1 here is the starting state of your loop. Usually, here you initialize a temporary variable.
expr2 here is the condition for the loop to run and keep running. Usually, this is based on your temporary variable's value. In this case, you want it to be less than some value. Think, when would you want your loop to stop running?
expr3 is what happens at the start of the loop's next cycle. Generally one does not do anything here unless it's to the temporary variable. I recommend ++.

Do you think you understand now?

-Albatross


Last edited on
I believe i got it this is what i entered
[code]for (i = 0; i < years; i++)
{
cost += (cost*inflaRate);
}[code]

i ran it in the code and it gave me the output what i wanted if that is the correct loop well thank you for all of your help all of you, you have no idea how much i appreciate it becaus im sure its frustrating trying to deal and help with someone as new as me.
Yep! You got it! :D
Good job ^_^

and on your last code tag, you need a '/' like [ /code ] without the spaces.


Helping people as new as you is what this forums for. (:

EDIT: You should probably use pre-increment (++i), instead of post-increment (i++). The difference being that with pre-increment, the variable is incremented before it's used.
An example being

1
2
3
4
int x = 5;
cout << ++x << endl;
cout << x++ << endl;
cout << x << endl;


output:

6
6
7


You'd think it'd appear 6, 7, 7, but because the second cout statement post-increments x, it outputs 6, then increments x to 7. This could be problematic :b
Last edited on
Okay ill definitely remember that and thanks you guys will probably here from me quite a bit these months seems like its going to be a long quarter in school but with experts like you im sure i will do just fine and again thanks a lot.
Topic archived. No new replies allowed.