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.
#include <iostream>
usingnamespace 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;
}
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.
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 ++.
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.
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
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.