CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2016
    Posts
    3

    Unhappy applying a rule in c++ (for loop)

    The payments will start in the amount of 1 dollar. Obviously that is not too much of a price at the beginning. But here are two rules we can apply every month to increase the installments.

    Rule A - Double the installment amount.
    Rule B - Triple the installment amount.
    You are free to apply Rule A every month, but Rule B can be applied only every other month.

    To maximize payments.

    1st month's payment 1 dollar.
    2nd month's payment 2 dollars. (doubled the amount)
    3rd month's payment 6 dollars. (triple it every other months)
    4th month's payment 12 dollars. (double the amount)
    5th month's payment 36 dollars. (triple it every other month)
    6th month's payment 72 dollars. (double the amount)
    7th month's payment 216 dollars. (triple it every other month)
    and so on ...
    So a program that will compute the monthly payment amounts you will get over a given number of months.
    The program should take the number of months from user and create a loop to compute the payments for each month and output on screen.


    SO FAR I have but I do not think this is write because I need the month to have the calculated dollar amount next to it.

    Code:
    	cout << "Here are your monthly installment amounts: " << endl;
    
    	for (int i = 0; i < 12; i++)
    	{
    			int m;
    			cout << "Month " << (i + 1) << ":
    Last edited by 2kaud; September 23rd, 2016 at 12:49 PM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: applying a rule in c++ (for loop)

    When posting code, please use code tags. go Advanced, select the formatted code and click '#'.

    [I have added code tags to the post]

    The program should take the number of months from user
    Then first you need to ask the user to enter the required number of months (cout) and the program will need to obtain this value into a variable (cin). The variable will be the upper terminating value for the for loop.

    months 2nd, 4th, 6th, etc double it - ie for every even month number double the amount
    months 3rd, 5th, 7th, etc triple it - ie for every odd month number triple the amount.
    Last edited by 2kaud; September 23rd, 2016 at 12:54 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Sep 2016
    Posts
    3

    Re: applying a rule in c++ (for loop)

    This is what I have, But it just makes the payment 1 I'm not sure how to get the numbers to appear.

    Code:
    	
            int month;
    	cout << "For how many months did they say you will recieve payments? "; 
    	cin >> month;
    	
    	cout << endl;
    	cout << "Here are your monthly installment amounts: " << endl;
    
    	for (int i = 0; i < 13; i++)
    	{
    			int Month;
    			int payment = 1; 
    			cout << "Month " << i << ":       " << payment << endl;
    			
    			if (i % 2 == 0) 
    			{ payment = payment * 2; }
    				
    			else { payment = payment * 3; }
    	}

  4. #4
    Join Date
    Sep 2016
    Posts
    3

    Re: applying a rule in c++ (for loop)

    I know my payment variable is not right since the outputs just spit 1 i am not sure how to make them double or triple since the first month must be 1

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: applying a rule in c++ (for loop)

    You ask for how many months to be entered - yet this value is not used in the for loop terminating condition?

    Why have a new variable Month within the for loop which isn't used?

    As payment is defined within the for loop, it is initialised to 1 for every loop iteration! It needs to be defined/initialised before the loop.

    For info,
    Code:
    payment = payment * 2;
    is usually written as
    Code:
    payment *= 2;
    and the same for +, - /, % etc

    As a matter of style, it isn't usual for the opening/terminating braces to be on the same line as code statements as with the if statements. It is more usual as for the for loop.

    Also note, that if there is only one statement controlled by the if then/else then the braces can be omitted. Consider
    Code:
    if (i % 2 == 0)
        payment *= 2;
    else
        payment *= 3;
    Last edited by 2kaud; September 24th, 2016 at 03:52 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: applying a rule in c++ (for loop)

    Quote Originally Posted by 2kaud View Post
    Code:
    if (i % 2 == 0)
        payment *= 2;
    else
        payment *= 3;
    Even simpler

    payment *= (i % 2) + 2;

    No if statement required.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured