using mod(%)

This is a discussion on using mod(%) within the C++ Programming forums, part of the General Programming Boards category; i am having problems using mod. i am making a program that will automaticlly make a deposit every 30 days. ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    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?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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
    Code:
    principle+=deposit
    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.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    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;

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21