for loop not working

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

//include directives
#include <iostream>

//using directives
using namespace std;

//declare variables
int count, months;
char ans;
double initial_balance, interest_rate, total_interest;

//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;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

total_interest = cc_interest(initial_balance, interest_rate, months) - initial_balance;

cout << "Interest accumulated over " << months << " months at \n";
cout << interest_rate * 100 << "% monthly interest rate\n";
cout << "from $" << initial_balance << " is \n";
cout << "$" << total_interest << endl;


//prompt the user to calculate a different value
cout << "Would you like to try another value?\n";
cout << "Enter y or n\n";
cin >> ans;

//end\restart the loop
} while (ans == 'y' || ans == 'Y');


return 0;
}

double cc_interest(double initial_balance, double interest_rate, int months)
{
double total, tot_interest;

total = initial_balance * (1+ interest_rate);

for (count = 2; count <= months; count++)

tot_interest = total * (1 + interest_rate);





return (tot_interest);
}
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'
is there a more "beginner" way to do that?
Ummmm... lemme see what I can come up with.

How about std::cin.ignore(256, '\n')?
Where am I supposed to put that?
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?
Ok. What does ignore do, and how can you tell there's leftover cin call junk in the stream?
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.

Ignore goes after your cin.
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?
closed account (E0p9LyTq)
is there a more "beginner" way to do that?


https://www.daniweb.com/software-development/cpp/threads/90228/flushing-the-input-stream
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 using namespace std; you won't have an ambiguity error and wouldn't need to use the scope operator.
Last edited on
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. =)
Topic archived. No new replies allowed.