Thread: not sure why my loop isnt working....

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Hi everyone, almost done with my code here but for some reason when the user enters 'Y' it does not loop up back to the top of the code. This is very important for me because I need it to display the count of sales. Any idea why? The do-while loop is being used. Here is my code:

    Code:
    #include <stdio.h>
    
    
    
    #define COMPANY "company"
    #define MAXA 1000000
    #define MINA 10000
    #define MINCP 0
    #define MAXCP 6
    
    
    void report(int sale, double cp, double tc);
    int getAmount(int min, int max, char item[]);
    double calcAmount(int sale, double cp);
    
    main()
    {
    
    int i=0;
    int sale, ch;
    double cp, tc;
    char c;
    
    do
    {
    system("cls");
    printf("\n\n\t%s\n\n", COMPANY);
    sale = getAmount(MINA, MAXA, "home sale amount");
    cp = getAmount(MINCP, MAXCP, "commission percentage");
    tc = calcAmount(sale, cp);
    report(sale, cp, tc);
    i++;
    
    printf("\tContinue (Y/N) : ");
    scanf("%c%*c", &c);
    
    } while(c == 'y' || c == 'Y');
    printf("\n\tTotal count:", i);
    
    
    return 0;
    }
    
    
    void report(int sale, double cp, double tc)
    {
    
    
    
    printf("\n\tThe home price is %6.2d",sale);
    printf("\n\tThe commission percentage is %2.1f", cp);
    printf("\n\tThe commission is %4.2f", tc);
    
    }
    
    
    
    int getAmount(int min, int max, char item[])
    {
    int n;
    
    printf("\n\tEnter the %s from %d to %d: ", item, min, max);
    scanf("%d*c*",&n);
    while (n < min || n > max)
    {
    printf("\n\tError, please enter within range");
    printf("\n\tEnter the %s from %d to %d: ", item, min, max);
    scanf("%d*c*",&n);
    }
    return (n);
    }
    
    double calcAmount(int sale, double cp)
    {
    return (sale * (cp / 100));
    }
    I think it has something to do with the subfunctions I believe...I am really stumped on this one..


    help would be greatly appreciated..thanks

    Also sorry for not including comments, i usually do this at the last moment :\
    Last edited by Salem; 03-11-2011 at 12:42 PM. Reason: R

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fflush vs gets

    Better use fgets() + sscanf()...
    Avoid scanf() as much as possible..... unless for properly formatted input file..

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    sorry im not following....

    I read that email but are you talking specifically about when I ask for a character input to validate the loop? 'Y' 'N' ??

    Sorry I am a noob :s
    Last edited by cprogrammer1980; 03-11-2011 at 10:55 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I must say that I'm getting ANNOYED by the noobs editing their posts down to crap just because they've got the answer (in some cases, just one reply).

    How is anyone else supposed to contribute or learn when you do this!?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "try again" loop not working
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 04-01-2007, 09:56 PM
  2. my do while loop not working
    By rodrigorules in forum C Programming
    Replies: 12
    Last Post: 09-07-2005, 06:52 PM
  3. Replies: 6
    Last Post: 07-19-2005, 01:03 PM
  4. EOF not working in a loop
    By Malabux in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2003, 06:28 PM