Thread: help with while statement for college project

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    6

    help with while statement for college project

    So im trying to work on this project but i'm not clear on the while statement and I know that what I have isn't working because my calculations are all wrong. I've attached what I have so far and my objective is to take and to enter a fee amount, a monthly payment amount, and a deposit amount and have the C program continue to count payment numbers until the amount owed reaches 0. I'm just not sure how to fix this.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post your code to the forum using code tags, please.

    I'm not about to download a doc file (of unknown format) to look at it.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    
    	int acct_num, date1, date2, date3, pymt_num;
    	float total_paid, out_bal,fee, init_pay, mon_pay, bal_due;
    
    
    	printf("\n\n	Enter Account Number	");
    	scanf("%d" , &acct_num);
    	printf("\n\n	Enter Fee	");
    	scanf("%8.2f", &fee);
    	printf("\n\n	Enter Initial Payment	");
    	scanf("%8.2f", &init_pay);
    	printf("\n\n	Enter Monthly Payment		");
    	scanf("%8.2f", &mon_pay);
    	printf("\n\n	Enter Date	");
    	scanf("%d%d%d" , &date1, &date2, &date3);
    	fflush(stdin);
    	system("cls");
    
    
    	pymt_num = 0;
    
    
    
    
    
    	while (out_bal >= 0.0)
    		{
    			pymt_num = pymt_num + 1;
    			bal_due = fee - init_pay;
    			out_bal = bal_due - mon_pay;
    			total_paid = init_pay + mon_pay;
    
    		}
    
    
    
    	printf("\n\n Patient Account Number: %d", acct_num);
    	printf("\n Consultation Date:     %d/%d/%d", date1, date2, date3);
    	printf("\n\n\n\n Treatment Fee:     %8.2f", fee);
    	printf("\n Initial Payment:   %8.2f", init_pay);
    	printf("\n Balance Due:       %8.2f", out_bal);
    	printf("\n\n\n Payment    Due        Payment   Total  Outstanding");
    	printf("\n Number     Date       Amount    Paid    Balance ");
    	printf("\n\n   %d      %d/%d/%d   %8.2f %8.2f %8.2f",
    	pymt_num, date1, date2, date3, mon_pay, total_paid, out_bal);
    	
    
    
    
    	return(0);
    }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    im sorry, im new, i didn't realize

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by fsutati View Post
    im sorry, im new, i didn't realize
    Not a problem....

    Ok a few things...
    First your loop is repeatedly subtracting the intial payment which should be taken only once above the loop.

    Second your balance due inside the loop never changes, thus the outstanding balance will never decrease.

    You need to set out_bal once outside the loop and crunch it inside the loop.
    ( out_bal = fee - init_pay then out_bal -= mon_pay )

    The same rules will apply to calculating the total paid... initialize once outside the loop, crunch inside the loop.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The initial payment subtraction, should be done outside the while loop. Clearly, there will be only ONE initial payment, and it should not be subtracted from the balance, every month.

    Well dam Sam -- I've been ninja-posted!!

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Ok, I did what you said (I think) but something still isn't right. For example, when I compile and execute the program, my professor has given me the following information but when I execute it's taken the values I'm entering and turning it to a decimal. For example, if I enter 2500 for the fee, it turns it into 0.25.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    
    	/*	Dr. Zalfolks fees*/
    	/*	tatiana saavedra	*/
    	/*	computer programming tues 530pm	*/
    	/*	Project 3   10/18/10 */
    
    	int acct_num, date1, date2, date3, pymt_num;
    	float total_paid, out_bal, fee, init_pay, mon_pay, bal_due;
    
    
    	printf("\n\n	Enter Account Number	");
    	scanf("%d" , &acct_num);
    	printf("\n\n	Enter Fee	");
    	scanf("%8.2f", &fee);
    	printf("\n\n	Enter Initial Payment	");
    	scanf("%8.2f", &init_pay);
    	printf("\n\n	Enter Monthly Payment		");
    	scanf("%8.2f", &mon_pay);
    	printf("\n\n	Enter Date	");
    	scanf("%d%d%d" , &date1, &date2, &date3);
    	printf("\n\n	Enter Payment Number	");
    	scanf("%d" , &pymt_num);
    	fflush(stdin);
    	system("cls");
    
    
    	pymt_num = 1;
    	bal_due = fee - init_pay;
    	out_bal = bal_due - mon_pay;
    	total_paid = init_pay + mon_pay;
    
    
    
    
    
    	while (out_bal >= 0.0)
    		{
    			pymt_num = pymt_num + 1;
    			out_bal = bal_due - mon_pay;
    			total_paid = init_pay + mon_pay;
    
    		}
    
    
    
    	printf("\n\n Patient Account Number: %d", acct_num);
    	printf("\n Consultation Date:     %d/%d/%d", date1, date2, date3);
    	printf("\n\n\n\n Treatment Fee:     %8.2f", fee);
    	printf("\n Initial Payment:   %8.2f", init_pay);
    	printf("\n Balance Due:       %8.2f", out_bal);
    	printf("\n\n\n Payment    Due        Payment   Total  Outstanding");
    	printf("\n Number     Date       Amount    Paid    Balance ");
    	printf("\n\n   %d      %d/%d/%d   %8.2f %8.2f %8.2f",
    	pymt_num, date1, date2, date3, mon_pay, total_paid, out_bal);
    	printf("\n\n******Payment Schedule Printed By: Tatiana Saavedr..........****\n\n");
    
    
    
    	return(0);
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you try entering $2500.00 ?
    What does that give you?

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Code:
    Ok, I did what you said (I think) but something still isn't right. 
    For example, when I compile and execute the program, my professor 
    has given me the following information but when I execute it's taken
     the values I'm entering and turning it to a decimal. For example, if I 
    enter 2500 for the fee, it turns it into 0.25.
    I am writing an identical program. I am having the SAME exact problem with my program (thread: Money Issues)

    What I have done (it doesn't work for odd dollar amounts, but it gets the job done for what is required by my teacher) is I made them all ints and then output > printf("%d.00", amount)

    See the thread from yesterday and you will see what my code looks like to make the program work. The .00 isn't on that version...but it is easy to add.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  4. Remove ActiveX from VC++ project
    By cr_naik in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2003, 11:15 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM