Thread: Help with C, loops

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Help with C, loops

    Newbie here, this is only my third program. I have to write a program that calculates simple interest. Then loops and does it again unless you input "-1". Seems like I've got it 98% done, just one little thing I can't get to work right. The program should stop as soon as you input "-1", but it keeps asking the other three questions before it stops.


    Code:
    #include <stdio.h>
    
    
    /* function main begins program execution */
    int main( void )
    {
    float principal; /* define principal variable */
    float itr; /* define interest variable */
    int tm; /* define term in days variable */
    float rtch; /* define interest charge variable */
    
    while ( principal != -1 ){
    /* input info */
    printf( "Enter loan principal (-1 to end): " );
    scanf_s( "%f", &principal );
    printf( "Enter interest rate:" );
    scanf_s( "%f", &itr );
    printf( "Enter term of the loan in days: " );
    scanf_s( "%d", &tm );
    
    
    /* calculate */
    rtch = (principal * itr * tm) /365;
    printf( "The interest charge is $%.2f\n\n\n", rtch );}
    
    
    return 0;
    
    } /* end */
    BANG HEAD ON DESK for about an hour now.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't checking after each line of input. If you want to potentially break right after that line of input, then you need to check right when you make that line of input.
    Code:
    while condition
        spit out some text
        get a number
        check that number, if condition break
        spit out more text
        get another number
        check that number, if condition break
        ...and so on...
    You are only checking at the top of the loop. If you want to check after each input, then you need to actually do that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Test for -1 after user input instead of at the top of the loop. The code cycles back to the while only after stepping o'er all the statements that are after it.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    Thanks guys. This is so logical, AFTERWARDS! Perfectly clear, once your done, you can look back and say "Oh sure that makes sense", but how do you think like that before you've spent a couple hours banging you head on the keyboard?

    OH yea, just follow the logic! From now on I'll just start by banging my head on the keyboard, then write the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. For/Next Loops...adding 10 numbers...
    By IanelarAzure in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2002, 12:02 PM
  4. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM