### Need help with loop in homework

Hi I have some homework that I need help in and I am completely lost. I have a homework assignment that needs to look like this: http://tinypic.com/view.php?pic=366h2&s=6 I have got about half of it down so I just need help with the whole loop thing. I'm sure I can figure out how to align it later but if someone can help me with that too that be swell. I don't need the answer just a couple hints or nudges in the right direction.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include #include #include using namespace std; int main () { int ACCU; // Patient account number int CONSM; // Consultation date month int CONSD; // Consultation date day int CONSY; // Consultation date year char SLASH; // Unsed / in date float FEE; // Treatment fee float INIT; // Initial payment float TOTAL; // Total balance due int C; // Counter int NPAY; // Numbered payments int DPAY; // Payment due dates float TPAY; // Total amount payed float BAL; // Outstanding balance cout << "This program will ask you for your patient account number, consultation date,\n"; cout << "treament fee, and initial payment. The program will then calculate the number of\n"; cout << "payments needed, the payment due dates, the total amount payed on those dates,\n"; cout << "and finally the outstanding balance. It charges an initial payment followed\n"; cout << "by monthly payments until the contract amount has been fully payed for. These monthly\n"; cout << "payments will be due on the first of every month starting the month following the\n"; cout << "consultation date.\n"; system ("cls"); // Clear the screen /* Data Input Section */ cout << "PATIENT ACCOUNT NUMBER: "; cin >> ACCU; if (ACCU <= 1000 || ACCU >= 9999) { cout << "Please enter a correct account value."; } else { cout << "CONSULTATION DATE: "; } cin >> CONSM; cin >> SLASH; cin >> CONSD; cin >> SLASH; cin >> CONSY; if (CONSM < 1 || CONSM > 12) cout << "Please enter a valid date."; else if (CONSD < 1 || CONSD > 31) cout << "Please enter a valid date."; cout << "\nTREATMENT FEE: \$ "; cin >> FEE; cout << "INITIAL PAYMENT: \$ "; cin >> INIT; BAL = FEE - INIT; cout << "BALANCE DUE: \$ " << BAL; /* Calculation Section */ cout << "\n\nPAYMENT NUMBER"; C = 1; do { C = C + 1; } while (C < 15); system ("PAUSE"); return 0; }``````
Last edited on
closed account (Dy7SLyTq)
i dont know how to do it in c++ with the cout object, but you can format c style with printf. so what exactly do you need help with in the loop?
The class I am taking didn't very well explain HOW to do a loop. I think I am on the right path here but I still can't get it too hold the new value and subtract that from the old one. I also can't get it to repeat it over.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101`` ``````#include #include #include #include #define NAME Nick Maner using namespace std; int main () { int ACCU; // Patient account number int CONSM; // Consultation date month int CONSD; // Consultation date day int CONSY; // Consultation date year char SLASH; // Unsed / in date float FEE; // Treatment fee float INIT; // Initial payment float TOTAL; // Total balance due int C; // Counter int NPAY; // Numbered payments int DPAY; // Payment due dates float TPAY; // Total amount payed float BAL; // Outstanding balance /* Intro. & Instructions */ cout << "Written by Nick Maner - 21 Febuary 2013\n\n"; cout << "This program will ask you for your patient account number, consultation date,\n"; cout << "treament fee, and initial payment. The program will then calculate the number of\n"; cout << "payments needed, the payment due dates, the total amount payed on those dates,\n"; cout << "and finally the outstanding balance. cout << "by monthly payments until the contract amount has been fully payed for. These monthly\n"; cout << "payments will be due on the first of every month starting the month following the\n"; cout << "consultation date.\n"; system ("cls"); // Clear the screen /* Data Input Section */ cout << "PATIENT ACCOUNT NUMBER: "; cin >> ACCU; if (ACCU <= 1000 || ACCU >= 9999) { cout << "Please enter a correct account value."; } else { cout << "CONSULTATION DATE: "; } cin >> CONSM; cin >> SLASH; cin >> CONSD; cin >> SLASH; cin >> CONSY; if (CONSM < 1 || CONSM > 12) cout << "Please enter a valid date."; else if (CONSD < 1 || CONSD > 31) cout << "Please enter a valid date."; cout << "\nTREATMENT FEE: \$ "; cin >> FEE; cout << "INITIAL PAYMENT: \$ "; cin >> INIT; BAL = FEE - INIT; cout << "BALANCE DUE: \$ " << BAL; /* Calculation Section */ cout << "\n\nPAYMENT NUMBER" ; C = 1; do { C = C + 1; } while (C < 15); cout << "Payment amount :" ; cin >> C ; for (BAL == 0; BAL - C;) { cout << BAL ; cout << "Payment amount :" ; cin >> C ; cout << BAL - C ; cin >> BAL ; } system ("PAUSE"); return 0; } ``````
I put that at the end of the code.
Last edited on
closed account (Dy7SLyTq)
oh ok so its the for loop at the end not the do while loop?

a for loop is declared like this:
 ``1234`` ``````for(initializtion; expression; increment) { //code... }``````

so how this is read:
the initialization part is equivalent to:
int i = 0;
while(i < 10) ...

so in a for loop you would see this as:
for(int i = 10; ...

the expression part is the same as a while loop, so in this case, i < 10.
so now we have:

for(int i = 0; i < 10...

the increment part is equivalent to:
while(i < 10)
i = i + 1;

so now we have:
for(int i = 0; i < 10; i = i + 1)
{
//code...
}

*note: i declared the int i in the for loop, and while this is legal, doesnt need to be done.

ie: int counter;
for(counter = 0...

**note: how it is executed:
it first does the initialization, then checks to make sure the expression is true, then does the code, then does the increment, then (until the expression isn't true), checks the expression, then executes the code, then does the increment
oh okay thank you. I entered that and realized my problem was that I thought it would run till the code was TRUE not false. anyways I fixed it now it does subtract from that to give you the balance. All I have to do now is add the other stuff (should be easy now that I understand for) and format it. here's my code for anyone else.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````#include #include #include #include using namespace std; int main () { int ACCU; // Patient account number int CONSM; // Consultation date month int CONSD; // Consultation date day int CONSY; // Consultation date year char SLASH; // Unsed / in date float FEE; // Treatment fee float INIT; // Initial payment float TOTAL; // Total balance due int C; // Counter int NPAY; // Numbered payments int DPAY; // Payment due dates float TPAY; // Total amount payed float BAL; // Outstanding balance /* Intro. & Instructions */ cout << "This program will ask you for your patient account number, consultation date,\n"; cout << "treament fee, and initial payment. The program will then calculate the number of\n"; cout << "payments needed, the payment due dates, the total amount payed on those dates,\n"; cout << "and finally the outstanding balance. cout << "by monthly payments until the contract amount has been fully payed for. These monthly\n"; cout << "payments will be due on the first of every month starting the month following the\n"; cout << "consultation date.\n"; system ("cls"); // Clear the screen /* Data Input Section */ cout << "PATIENT ACCOUNT NUMBER: "; cin >> ACCU; if (ACCU <= 1000 || ACCU >= 9999) { cout << "Please enter a correct account value."; } else { cout << "CONSULTATION DATE: "; } cin >> CONSM; cin >> SLASH; cin >> CONSD; cin >> SLASH; cin >> CONSY; if (CONSM < 1 || CONSM > 12) cout << "Please enter a valid date."; else if (CONSD < 1 || CONSD > 31) cout << "Please enter a valid date."; cout << "\nTREATMENT FEE: \$ "; cin >> FEE; cout << "INITIAL PAYMENT: \$ "; cin >> INIT; BAL = FEE - INIT; cout << "BALANCE DUE: \$ " << BAL; /* Calculation Section */ cout <<" "<< endl; cout << "Payment amount :" ; cin >> C ; for (int i = BAL; i >= 0 ; i = i - C ) { cout << i ; cout << "Payment amount :" ; cin >> C ; } system ("PAUSE"); return 0; } ``````
Last edited on
closed account (Dy7SLyTq)
yeah and i dont know if you want to do this, but just so you know, you can leave initialization blank and you dont have to have an i. it can be anything
Topic archived. No new replies allowed.