-
using mod(%)
i am having problems using mod. i am making a program that will automaticlly make a deposit every 30 days. i was told to use
if(day%30==0) principle=principle+deposit.
now on my output in the loop the deposit is never incremented every 30 days. am i using the right stuff?
-
Just to make sure you understand what it is, the modulus operator will return the remainder of a division problem. For example, if you use the 29%7, you will recieve an answer of 1, because 7 will go evenly into 28, and there is one left over. So the above strategy should work if employed correctly. Post the rest of your code and we might find the problem there.
On a side note, there's an operator in C++ you might find useful if you're working with numbers a lot in your programs.
Code:
principle=principle+deposit
can be written as
It simply adds the right number to the left number, where the answer is stored. You can do the same thing with subtraction, multiplication, and division.
-
Code:
#include <iostream>
using namespace std;
int main()
{
int day;
day=1;
double age, principle, begbal, rate, deposit, intDay=0, intTotal=0, endbal=0;
cout<<"age: ";
cin>>age;
cout<<endl<<"intitial deposit: ";
cin>>principle;
cout<<endl<<"deposit: ";
cin>>deposit;
cout<<endl<<"interest: ";
cin>>rate;
rate=rate/100;
do
{
begbal=principle+endbal;
intDay=begbal*rate/365;
intTotal=intDay+intTotal*365;
if(day%30==0) begbal+=deposit;
endbal=principle+intDay+deposit;
day++;
cin.get();
cout<<age<<" "<<begbal<<" "<<intTotal<<" "<<deposit<<" "<<endbal<<endl;
intTotal=0;
age++;
}
while (age<=65);
return 0;
-
Ah. I see the problem. You have set up a do/while loop, that will run until the person is 65 years old. Every cycle, the person in question grows a year. You need to have a second loop inside of this containing all of the code that is to execute every month (like adding interest). This loop should execute twelve times every time the annual loop executes once.