Thread: Trouble with math operations-Beginner program

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    Trouble with math operations-Beginner program

    I have been building a very basic program to convert dollars and change to coins. This program is similar to an example in the book I am using. I try to make small changes to ensure that I understand the concepts of the program. I am having success at converting the input of dollars and cents into all cents, and the proper number of quarters displays. Regardless of input, quarters is correct, dimes shows 1, nickels and pennies produces 0. Looking at the book example (I tried to build before cheating with example) my code appears accurate but the results do not work. I have compiled the code in Visual Studio 2010 and CodeBlocks v 10.05.

    Code:
    #include <iostream>
    //This program makes change in quarters, dimes, nickles, and pennies
    using namespace std;
    int main()
    {
        int cents, dollars, total, quarters, dimes, nickels, pennies; //named variables
        quarters = 25, dimes = 10, nickels = 5, pennies = 1;          //establishing vaLues for variables
        cout << "This program will report change due/n"
             << "using the minimum number of coins." << endl;
        cout << "Enter number of dollars. " << endl;
        cin >> dollars;                                              //user input
    	cin.ignore();
        cout << "You entered " << dollars << "."                    //output of user input
             << " Now enter the amount of change." << endl;         //user input
        cin >> cents;
    	cin.ignore();
        cout << "You entered " << cents << "." << endl;            //output of user input
        total = dollars * 100 + cents;                             //converts dollars to cents and adds cents
        cout << "Your total is " << total << "cents" << endl;	//total of input converted to cents
        total = total / quarters;                               //divide by 25 to determine # of quarters
        cout << "You receive" << total << "quarters." << endl; //output of quarters
        total = total % quarters;                              //remainder of cents
        total = total / dimes;                                 //divide by 10 to determine # of dimes
        cout << "You receive" << total << "dimes." << endl;    //output of dimes
        total = total % dimes;                                 //remainder of cents
        total = total / nickels;                               //divide by 5 to determine # of nickels
        cout << "You receive" << total << "nickels." << endl;  //output of nickels
        total = total % nickels;                               //remainder of cents
        total = total / pennies;                               //divide by 1 to determine # of pennies
        cout << "You receive" << total << "pennies." << endl;  //output of pennies
    	cout << "Press the Enter key to close." << endl;       //keeps program open to view until Enter key is pressed
    	cin.get();
        return 0;
    }
    I would appreciate a clue as to where I am making my code error(s).
    Thank you

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Your arithmetic is incorrect. Let's say that your input is 186 cents which is $1.86. When you calculate the quarters you say total = 186/25 which is 7. You are confusing print statement values with remainder values that you need to continue computing the change in lower denominations.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Thank you claudiu, but i still do not understand my error. I changed int for quarters, dimes, nickels, and pennies to const int so they always retain their value.
    total = total % dimes (for example) should put the value of the remainder after division into total, correct? Do I misunderstand the use of % ?
    Last edited by brewgrass; 08-06-2011 at 02:38 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Suggestion: variables and constants named thusly:

    Code:
    const int CENTS_PER_QUARTER = 25;
    const int CENTS_PER_DIME = 10;
    const int CENTS_PER_NICKEL = 5;
    
    int num_of_quarters = 0;
    int num_of_dimes = 0;
    int num_of_nickels = 0;
    Maybe using these variable names will make it easier to figure out what you're doing.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Asking them to enter the number of dollars, and then assuming they are going to enter "1.86" is a little weird. I never talk about having 1.86 dollars. You might consider asking them to enter the dollars and cents as two different values. It just seems unnatural to refer to it the way you are.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    Asking them to enter the number of dollars, and then assuming they are going to enter "1.86" is a little weird. I never talk about having 1.86 dollars. You might consider asking them to enter the dollars and cents as two different values. It just seems unnatural to refer to it the way you are.
    Quzah.
    Quote Originally Posted by OP
    Code:
     cout << "Enter number of dollars. " << endl;
        cin >> dollars;                                              //user input
    	cin.ignore();
        cout << "You entered " << dollars << "."                    //output of user input
             << " Now enter the amount of change." << endl;         //user input
        cin >> cents;
    	cin.ignore();
        cout << "You entered " << cents << "." << endl;            //output of user input
        total = dollars * 100 + cents;                             //converts dollars to cents and adds cents
    You mean what he is doing?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Thank you all for helping me solve my math operations dilemma. I now need to add an if statement because the program crashes if any of the operations tries to divide by 0.
    This is fun and frustrating at the same time hehe.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    Yes you've understood the modulo (%) operator correctly.

    However, say you need to decide the coinage to return for an input of 137 cents, for example.

    The "total" begins as 137.

    You are taking 137/25 = 5 and then storing it in total. This is incorrect.

    Total should only be updated in the lines which are returning the remainder.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Yes Ocifer, adding more variables allowed me to make things work. As a beginner my code is probably bulkier than it has to be, but I am determined to learn so neater code will surely come when I have a better understanding of the capabilities of the language. I am open to constructive criticism...I may not understand what is suggested but I am open to it. Here is what I have come up with so far.
    Code:
    #include <iostream>
    
    using namespace std;
    const int centsPerQuarter = 25;  //naming constant integers
    const int centsPerDime = 10;     //constants represent coin values
    const int centsPerNickel = 5;
    
    int main()
    {
        int numOfQuarters = 0;
        int numOfDimes = 0;
        int numOfNickels = 0;
        int dollars = 0;
        int cents = 0;
        int total = 0;
        cout << "This program will report change due"
             << "using the minimum number of coins." << endl;
        cout << "Enter number of dollars. " << endl;
        cin >> dollars;                                              //user input
    	cin.ignore();
        cout << "You entered " << dollars << "."                    //output of user input
             << " Next, enter the amount of change." << endl;         //user input
        cin >> cents;
    	cin.ignore();
        cout << "You entered " << cents << "." << endl;            //output of user input
        total = dollars * 100 + cents;                             //converts dollars to cents and adds cents
        cout << "Your total is " << total << "cents" << endl;	//total of input converted to cents
        if (total > 25){
        numOfQuarters = total / centsPerQuarter;
        cout << "You receive " << numOfQuarters << " quarters." << endl;
        total = total % (numOfQuarters * centsPerQuarter);      //calculating remainder of cents
        } if (total > 10) {
        numOfDimes = total / centsPerDime;
        cout << "You receive " << numOfDimes << " dimes." << endl;
        total = total % (numOfDimes * centsPerDime);
        } else cout << "You receive 0 dimes" << endl;
        if (total > 5) {
        numOfNickels = total / centsPerNickel;
        cout << "You receive " << numOfNickels << " nickels." << endl;
        total = total % (numOfNickels * centsPerNickel);
        } else cout << "You receive 0 nickels." << endl;
        if (total < 1) { cout<< "You receive 0 pennies." << endl; }
        else {
        }
        cout << "You receive " << total << " pennies." << endl;
            cin.get();
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    Not sure if everyone will agree with this suggestion, but here goes:

    This program is quite specific.

    In areas that use the dollar / cent construct of money, a quarter is always 25 cents, a dime 10 cents, a nickel 5 cents. I would do away with the constant variables altogether, and hard code the 25, 10, 5 in the program. It would look less cluttered, and then you'd only need a set of variables to store the actual coin quantities.

    Caveat:

    In general it is good to have constants declared if you're using them a lot, and then you need only change them once. Doing so will make more volatile programs easier to maintain, so my suggestion is only for this case.

    You've only got three variables here, and I don't forsee a quarter (for whatever reason) to be redifined to something other than 25 cents. Sure 25 cents might be worth more or less on a given day, but it is a safe assumption that a quarter will be 25 cents out of 100. Same for dimes 10 out of 100.

    Funnily enough, when i googled "decimal currency" this came up currency - Are all modern currencies based on decimals? - Stack Overflow
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ocifer View Post
    Not sure if everyone will agree with this suggestion, but here goes:

    This program is quite specific.

    In areas that use the dollar / cent construct of money, a quarter is always 25 cents, a dime 10 cents, a nickel 5 cents. I would do away with the constant variables altogether, and hard code the 25, 10, 5 in the program. It would look less cluttered, and then you'd only need a set of variables to store the actual coin quantities.
    No, this is called bad programming practice. Even if those values will not change very often, 25, 10, 5 are not very specific. They tell very little. Constants tell a lot more.
    That is why the general advice is "avoid magic numbers."
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You can't avoid named constants because you're used to a concept. That's assuming a lot about people and what they know, and that's why magic numbers are bad. I once did a similar program where I converted old GBP (before decimalization) into its current decimal form for school. I'll be damned if I remembered now what I wrote then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner C - Trouble with getchar and EOF
    By namath91 in forum C Programming
    Replies: 8
    Last Post: 05-11-2011, 08:37 PM
  2. Math Operations in a Variable?
    By MPQC in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2009, 01:32 PM
  3. Beginner in Trouble!
    By Sarina in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 02:35 PM
  4. A beginner in trouble
    By Eldey in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2004, 09:16 AM
  5. Question involving using math operations...
    By Screwz Luse in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 05:20 PM