Thread: Coin counting (logic problem)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    18

    Coin counting (logic problem)

    Problem here. I assume that I put too many nested if and else statements and screwed up somewhere :x. Anyway, the project is:
    Write a program to print the coins necessary to return change to a customer after a purchase. The change can be made up of quarters, dimes, nickels and pennies. You may assume that the amount of change is less than $1. Your program should use the minimum number of coins needed. Input the amount of change in cents.
    Yeah, So I've been at this for a while and came up to the conclusion that I suck at programming. Help would be greatly appreciated .
    I get no value for Quarters (q) and everything else says its equal to 0 in the output.
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    main()
    {
        int q1, d1, n1, p1, ans, change; 
        int q=0;
        int d=0;
        int n=0;
        int p=0;
        
     do
     {
        cout << "What is the change amount? ";
        cin >> change;
        if (change>25)
           do
           {
           q1=change-25;
           q++;
           change=change-(q*25);
           }
           while(q1>=25);
        else    
                if (change>10)
                   do
                   {
                   d1=change-10;
                   d++;
                   change=change-(d*10);
                   }
                   while (d1>=10);
                else
                    if (change>5)
                       do
                       {
                       n1=change-5;
                       n++;
                       change=change-(n*5);
                       }
                       while (n1>=5);
                    else
                          if (change>1)
                             do
                             {
                             p1=change-1;
                             p++;
                             change=change-(p*1);
                             }
                             while (p1>=1);
                          else
                                  cout << "Amount of quarters " << q << '\n';
                                  cout << "Amount of dimes " << d << '\n';
                                  cout << "Amount of nickels " << n << '\n';
                                  cout << "Amount of pennies " << p << '\n';
                                  cout << "\nTry again? (1 = yes, 2 = no): ";
                                  cin >> ans;
      }
      while (ans=1);
      return 0;
    }
    Last edited by xlokix; 01-17-2005 at 02:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Josephus problem variant logic troubles
    By misterMatt in forum C++ Programming
    Replies: 0
    Last Post: 04-29-2009, 02:38 PM
  2. Replies: 5
    Last Post: 01-31-2006, 01:54 AM
  3. Logic problem?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-10-2002, 04:10 PM
  4. Problem with letter and word counting
    By wordup in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 04:02 PM