Coin counting (logic problem)

This is a discussion on Coin counting (logic problem) within the C++ Programming forums, part of the General Programming Boards category; Whoa whoa! You forgot to ask the user how much change he/she has! Code: cout << "What is the change ...

  1. #16
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Whoa whoa!
    You forgot to ask the user how much change he/she has!
    Code:
    cout << "What is the change amount? ";
    cin >> change;

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Ok, look at this
    Code:
    #include <iostream>
    //#include <conio.h> Not standard C++ :(
    
    // We could do: using namespace std;
    // But we'd be including more than we need/want!
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main() {
     int change;
     int quarters=0, dimes=0, nickels=0, pennies=0;
    
    
     cout << "What is the change amount? ";
     cin >> change;
    
    while(change > 0)
        {
         if (change >= 25)
         {
         quarters++;
         change = change - 25;
         continue;
         }
         if(change >= 10)
         {
         dimes++;
         change = change - 10;
         continue;
         }
         if(change >= 5)
         {
         nickels++;
         change = change - 5;
         continue;
         }
         if(change >=1)
         {
         pennies++;
         change = change - 1;
         continue;
         }
    } // <-- You forgot to stop your while loop
    
     cout << "Quarters: " << quarters << endl;
     cout << "Dimes:    " << dimes  << endl;
     cout << "Nickels:  " << nickels  << endl;
     cout << "Pennies:  " << pennies  << endl;
    
     return 0;
    }
    This crashes BUT I can see just as it crashes, that the logic is correct. (I entered 35, and got 1 quarter, and 1 dime) then it crashed.

  3. #18
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Probably not crashing. Probably just closing. Check the FAQ for information on how to stop this from happening.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #19
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    hey pal listen to scribbler. you are going to complex for an easy task. Look at the program that I posted on the first page. On 1 pass it calculates the change.

    Code:
    void change(float fMoney, int* iQuart, int* iDime, 
                int* iNick, int* iPenn) {
    
    //Determine how many quarters, dimes, nickels and pennies are in the money provided
    	*iQuart = (int)(fMoney / .25);
    	fMoney = fMoney - (.25 * *iQuart);
    	*iDime = (int)(fMoney / .10);
    	fMoney = fMoney - (.10 * *iDime);
    	*iNick = (int)(fMoney / .05);
    	fMoney = fMoney - (.05 * *iNick);
    	*iPenn = ceilf(fMoney*100); //ceilf used to counter rounding error experienced with floating point numbers
    }
    fMoney would be the change. If you have questions on this implementation just ask.
    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

  5. #20
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by xlokix
    This crashes BUT I can see just as it crashes, that the logic is correct. (I entered 35, and got 1 quarter, and 1 dime) then it crashed.
    Code:
    #include <stdio.h>
    ------------------[BLAHBLAHBLAH]
    cout << "Quarters: " << quarters << endl;
    cout << "Dimes:    " << dimes  << endl;
    cout << "Nickels:  " << nickels  << endl;
    cout << "Pennies:  " << pennies  << endl;
    
    getchar();getchar();
    
    return 0;
    }
    Try adding the snippets that to the source so that the window doesn't autoclose!

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Well I'm trying to keep it simple, My teacher probably wouldn't believe I wrote the code if I included stdio.h, since we've never learned that before. Or getchar (whatever the hell that is ). But yes, that did fix the crash. Is there another way around it without stdio.h ?

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Here, I'll update on what I have so far.
    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    
    main()
    {
        int change;
        int q=0,d=0,n=0,p=0;
        cout << "What is the change?";
        cin >> change;
        
        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';
        getchar();getchar();
        return 0;
    }

  8. #23
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    This is what getchar() does: It basically just asks for user input.
    Code:
    #include <iostream> // Allow us to use cout and endl
    #include <stdio.h> // Allows us to use getchar();
    
    // We could also do:
    // using namespace std;
    using std::endl;
    using std::cout;
    
    int main() { // Main always returns an integer!
         // Gets a character from console input (like cout), except only ONE character!
         // Not a string
         int myCharacter = getchar();
         cout << myCharacter << endl;
         return 0;
    }
    If your teacher looks at the source and goes: "WTH", then you can explain it to him, as far as what everything does! And if he still doesn't believe you, then send him to this forum topic, and he'll probably be like:
    "Wow somebody asked for help! Holy ****! 50 points extra credit for you!" lol

    ---EDIT---
    Be sure to test this out, practice makes perfect!
    Last edited by Kleid-0; 01-17-2005 at 04:17 PM.

  9. #24
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I don't recommend telling a C++ student to use C functions, and then challenge the instructor. There's a curriculum to follow and all you'll do is throw a rock into the machine. Not to mention that is a complete garbage solution to what he's asking. There is a member function of cin which would suit perfectly, however I'd sooner let his curriculum teach the method first. Just giving the answers robs him of a valuable learning tool...studying.

    In my experience, instructors don't look at the compiled/executable file. They are only interested in the sourcecode which they look over themselves to verify the student has grasped the concepts the project places before them (as well as observe their logic skills). The window closing upon program exit is a non issue.

  10. #25
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Alright, heres a C++ approach still using the one pass way yet no floats or strange rounding functions:

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main() {
    
    	int fChange;
    	int Quart, Dime, Nick, Penn;
    
    	cout << "Enter the change:";
    	cin >> fChange;
    	//calculate change on 1 pass
    	Quart = (fChange / 25);
    	fChange = fChange - (25 * Quart);
    	Dime = (fChange / 10);
    	fChange = fChange - (10 * Dime);
    	Nick = (fChange /5);
    	fChange = fChange - (5 * Nick);
    	Penn = fChange; 
    
    	cout << "Quarters: "<< Quart << endl;
    	cout << "Dimes: " <<Dime << endl;
    	cout << "Nickels: "<< Nick << endl;
    	cout << "Pennies: " << Penn << endl;
    
    	return 0;
    }
    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

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Actually, I'm in C++ 1, and there will be no C++ 2 next year. So I will not learn more. And it's only a half a year class, so I'm finished with the class in about a week.

  12. #27
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by andyhunter
    Alright, heres a C++ approach still using the one pass way yet no floats or strange rounding functions:
    Mine was very similar. And since so many people have already posted code, I might as well follow suit.

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    main()
    {
    	int quarters, dimes, nickels, pennies;
    	int change;
    
    	cout << "What is the change amount? ";
    	cin >> change;
    	
    	quarters = change / 25;
    	change %= 25;
    	dimes = change / 10;
    	change %= 10;
    	nickels = change / 5;
    	change %= 5;
    	pennies = change;
    	
    	cout << "Amount of quarters " << quarters << endl;
    	cout << "Amount of dimes " << dimes << endl;
    	cout << "Amount of nickels " << nickels << endl;
    	cout << "Amount of pennies " << pennies << endl;
    
    	return 0;
    }
    Last edited by Scribbler; 01-17-2005 at 04:43 PM.

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Before the way I'm using now, I wrote something similar to scribblers way and got weird numbers so I changed my method.

    Ok. Next up, Project 2!
    Oh boy this is going to be a long one. The user thinks of a number, and the program guesses. The program makes a guess of 50, then the user tells whether it's higher or lower until the program eventually gets it.
    Booo! I'm gonna eat dinner before I start this. Leave some suggestions on how to start it if you want. I presume that there will be a hell of a lot of nested if statements.

  14. #29
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I won't post any code for that project, but it's a typical learning tool.

    Hint. Search your textbook for binary search method.

  15. #30
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540

    Quote:
    Originally Posted by andyhunter
    Alright, heres a C++ approach still using the one pass way yet no floats or strange rounding functions:

    Mine was very similar. And since so many people have already posted code, I might as well follow suit.
    Of course, there are only so many ways of solving a particular problem. And I do recall saying :

    hey pal listen to scribbler
    One solution was originally in C and one in C++. No reason to get all huffy.

    edit: Not to mention I wasn't attempting to give him a solution for his first post. Just show him a similar solution where the algorithm was important, not the implementation. That's why my first post said this:

    Here is a program that calculates the amount of change in a given sum. I believe this might point you in the right direction for what you are doing.
    Last edited by andyhunter; 01-17-2005 at 04:57 PM.
    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

Page 2 of 4 FirstFirst 1234 LastLast
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, 03: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, 05:10 PM
  4. Problem with letter and word counting
    By wordup in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 05:02 PM

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