No errors compiling but no output?

This is a discussion on No errors compiling but no output? within the C Programming forums, part of the General Programming Boards category; Really new to C so bear with me I wrote this to convert change Faulty logic on the actual conversion ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    No errors compiling but no output?

    Really new to C so bear with me
    I wrote this to convert change
    Faulty logic on the actual conversion notwithstanding, when i run it, it outputs nothing and i have to ^C to exit (in linux terminal).
    My only guess is my logic is messed up and it stays in the do-while loop but i can't figure out the problem...

    Code:
    #include <stdio.h>
    
    typedef struct {
      int tens;
      int fives;
      int ones;
      int quarters;
      int dimes;
      int nickels;
      int pennies;
    } change;
    
    float user_input();
    void calculate_change(float price);
    void display_change();
    
    int main()
    {
      float price;
    
      price =  user_input();
      calculate_change(price);
      display_change();
    
      return 0;
    }
    
    float user_input(){
    
      float amount;
      printf ("Enter a dollar amount less than $20: ");
      scanf ("%f",&amount);
      printf ("\n");
      return (amount);
    }
    
    void calculate_change(float price){
    
      change bills;
      int total;
    
      total = price * 100;
    
    
       do {
          if(total%1000==1)
            {
              bills.tens++;
              total=total-1000;
            }
          else if(total%500==1)
            {
              bills.fives++;
              total=total-500;
            }
          else if(total%100==1)
            {
              bills.ones++;
              total=total-100;
            }
          else if(total%25==1)
            {
              bills.quarters++;
              total=total-25;
            }
          else if(total%10==1)
            {
              bills.dimes++;
              total=total-10;
            }
          else if(total%5==1)
            {
              bills.nickels++;
              total=total-5;
            }
          else if(total%1==1)
            {
              bills.pennies++;
              total=total-1;
            }
       } while(total>0);
    
    }
    
    void display_change(){
    
      change bills;
    
      printf ("$10:  %d \n", bills.tens);
      printf ("$5:  %d \n", bills.fives);
      printf ("$1:  %d \n", bills.ones);
      printf ("$.25:  %d \n", bills.quarters);
      printf ("$.10:  %d \n", bills.dimes);
      printf ("$.05:  %d \n", bills.nickels);
      printf ("$.01:  %d \n", bills.pennies);
    
    }
    Last edited by opeth; 04-05-2012 at 05:27 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The "change bills" in calculate_change() is unrelated to display_change().

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,579
    You don't want to keep checking for total%amount. Think about it. If you have one penny left, total is 1. 1 % 1 == 0, so you never increment bills.pennies and never subtract 1 from total, so you never converge to zero. This is not just an issue with pennies however, all the checks in your convert_change function are wrong. Just check > amount, like if (total > 1000).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors after compiling
    By CroBoss in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2010, 10:21 AM
  2. Compiling errors
    By courtesan in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 10:52 AM
  3. c++ compiling errors... very odd!
    By renderstream in forum C++ Programming
    Replies: 9
    Last Post: 03-07-2004, 04:39 PM
  4. errors when compiling, but I don't know what they mean
    By indigo0086 in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2002, 01:50 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21