I wrote a program that is supposed to give you the amount of credit accrued after x number of months at y percentage rate. I attempted to write a function with a for loop, but the for loop does not appear to work. Can anyone please tell me what I'm doing wrong. In the program, I am trying to test for
initial_balance == 1000
interest_rate == .015
months == 12
with the goal of the output to screen being
Interest accumulated = $195.62
Thanks in advance.
//computes the interest accumulated after some number of months on a credit card
//declare functions
double cc_interest(double initial_balance, double interest_rate, int months);
//returns the price per share of stock
int main()
{
//declare local variables
do //loop to repeat at user's discretion
{
/*Give instructions to users and request
from user (politely) the number of shares
and the price -broken into 3 pieces- per share */
cout << "This program will calculate the interest accrued \n";
cout << "on a credit card over a given number of months from\n";
cout << "an initial balance.\n";
cout << "Enter doubles: initial balance, monthly interest rate\n";
cout << "as a decimal fraction, e.g. for 1.5% per month write 0.015";
cin >> initial_balance;
cin >> interest_rate;
cin >> months;
You have leftover input from your previous cin calls. You can clear the input stream with std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'
The problem is that you have that left over input from the other cin calls and as far as I am away the only way to fix that is to clear the stream. Unfortunately I can't think of any other simple way to do that. If it helps, I can explain to you what ignore does?
Ignore basically "ignores" data in the stream up to a certain point. The default is 1 character. You can use different parameters to get different results.
For example: std::cin.ignore(256) ignores 256 characters std::cin.ignore(256, '\n' ignores 256 characters OR UNTIL it hits a new line character
The 1st parameter is the amount to be ignored, the 2nd is the delimiter.
If I put this in my second function, I still get the same results. Just the first loop results. Am I supposed to put it in the second function before the loop?
Wow... I'm sorry I didn't even see your function because you didn't use code tags and it was hard to read. I just assumed you were having problems with the do-while loop. My bad.
You're issue is that your count variables in the for-loop are ambiguous. It can't decide which count to use. Just prefix them with the scope operator. ::count
Edit:
And just so you know, if you remove the usingnamespace std; you won't have an ambiguity error and wouldn't need to use the scope operator.
That didn't seem to compile for me. I figured it out though. Stupid error on my part... in my algorithm for the loop, I changed tot_interest to total from the for loop and the return value and I now get the correct output. I really appreciate your time in helping me. I hope you win the lottery and get to pet a puppy. =)