Thread: Coin counting (logic problem)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    I'll try and stay away from him..

    But it's still not working..

    I used some of what you told me,
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    main()
    {
        int change;
        int q=0,d=0,n=0,p=0;
        
        while(change > 0)
        {
         if (change >= 25)
         {
         q++;
         change = change - 25
         continue;
         }
         if(change >= 10)
         {
         d++;
         change = change - 10;
         continue;
         }
         if(change >= 5)
         {
         n++;
         change = change - 5;
         continue;
         }
         if(change >=1)
         {
         p++;
         change = change - 1;
         continue;
         }
      }    
            cout << "Amount of quarters " << q << '\n';
            cout << "Amount of dimes " << d << '\n';
            cout << "Amount of nickels " << n << '\n';
            cout << "Amount of pennies " << p << '\n';
        return 0;
    }
    I also tried compiling yours, and when I enter something in, it crashes. Maybe it's because im using the dev c++ compiler? D:

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    compile who's?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by xlokix
    I also have no clue why I was never taught "continue", as it's very useful
    It makes you go back to the top of the loop. break gets you out of the current loop. This is an infinite loop:
    Code:
    do {
        continue;
    } while(false); // Break out of the loop once you get here
    This is your crash fix:
    Code:
    while(change > 0)
        {
         if (change >= 25)
         {
         q++;
         change = change - 25
         continue;
         }
         if(change >= 10)
         {
         d++;
         change = change - 10;
         continue;
         }
         if(change >= 5)
         {
         n++;
         change = change - 5;
         continue;
         }
         if(change >=1)
         {
         p++;
         change = change - 1;
         continue;
         }
    } // <-- You forgot to stop your while loop
    Keep asking questions, anything that you don't understand quite.

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