Psst... Ready to join TalkBass and start posting, make new friends, sell your gear, and more?  Register your free account in 30 seconds.

Anyone know C++?

Discussion in 'Off Topic [BG]' started by h3llRa][seR, Apr 26, 2003.


  1. I'm fairly new to it and I need some help writing a program.
     
  2. moley

    moley

    Sep 5, 2002
    Hampshire, UK
    I know C++, though I may be a bit rusty. What's the problem?
     
  3. geshel

    geshel

    Oct 2, 2001
    Seattle
    Yeah, heh. Hmm, writing a program. OK, put this here, and that there. . . ;)

    Got anything specific?
     
  4. haven't programmed anyting at all for a few months, but I know C++, so what do you want to know
     
  5. It's for my Computer Science class. It's just an assignment, but seeing as I have the worst teacher in the world I am having some trouble.

    Here's the assignment:

    Write a program that will promt the user to enter an amount of money between $0 and $10 (decimal places allowed.) Determine how many quarters, dimes, nickels, and cents are needed to make up that amount and this information to the screen. Additionally, ensure that the output makes grammatical sense. (for example, if you need only 1 dime , then the output should not write the plural "1 dimes".)


    I have been working on this for 2 days. I was totally confused by my teacher. About 90% of my class was having trouble with this and she explained it briefly. I was using "double" as my data type and it semed like I was going in the right direction then she said to use %, but if she wants us to use decimal points then that won't work because % doesn't work with the "double" data type and you can't get decimals from the "int" data type.

    What I have figured out so far, or at least think it's right, is to multiply my variable by 100 then divide it but it doesn't seem to work. It's logical on paper but it won't read the entered amout if it is a decimal. For example if I put in 2.10, it only reads the 2 and it totally disregards the .10. In order for it to work right I think I have to use the "double" data type but I'm pretty **** confused about where. I need some help. I am so **** frustrated/
     
  6. Phat Ham

    Phat Ham

    Feb 13, 2000
    DC
    For reading in a decimal value a double would work and so would a float. I'd use a float just to try to be a little more efficient. If you try to use an int it won't read decimal values. Use it when you read in the input. For example,

    cout << "Enter amount of money: ";
    float Input;
    cin >> Input;

    After checking to make sure the input is between 0 and 10 you can then multiply that decimal value by 100 to get the number of cents. If you want to use % you can take your float and copy it into an int. For example,

    int NumberofCents = 100 * Input;

    It might give you a warning about truncation or something like that but don't worry about it.

    I'd tell you more but I have to go now. Hope I at least gave you somewhere to start.
     
  7. you can get rid of the warning by writing
    int NumberofCents = 100 * (int)Input;
    instead of
    int NumberofCents = 100 * Input;
     
  8. moley

    moley

    Sep 5, 2002
    Hampshire, UK
    Personally, I think that using a float or a double for reading in monetary values is sorta messy (unless you actually do want fractions of a cent). I would personally, read it in as an int, in cents. So for $5, you'd type 500.

    Something of that sort, anyway.

    Having obtained your number of cents, as an int, you then gotta work out how many coins of each type you need to make it up - is that right? From what you've said, it looks like you're saying a quarter is the largest coin value you're using?

    Ok, that's actually fairly simple.

    Lets say you've got a variable called Cents, which is an int, and contains the number of cents.

    (This is the part where I show my ignorance of US currency :D)

    You need to work out, first, how many whole quarters will fit into that - which means you need to divide by 25 and discard the remainder.

    Integer division will do that for you automatically.

    So:

    int Quarters = Cents / 25;

    Now, since this is integer division, any fractional part will be lost. So, for example, if Cents was 100 - Quarters would be 4, as you'd expect. However, if Cents was 124, Quarters would still be 4. It would need to be 125 or more to make Quarters 5.

    Ok, now you need to subtract that amount you've just made up in quarters from the original - leaving only what you have left to work with. So:

    Cents -= (Quarters * 25);

    So, if Cents had been 124 to begin with, it would now be 24. For this, if you want to preserve the Cents variable and use it later - then you'll want to use an intermediate variable for this part. For example:

    int Cents, Quarters, Dimes, Nickels, Temp;
    cin >> Cents;
    Quarters = Cents / 25;
    Temp = Cents - (Quarters * 25);

    Then you need to do the same for dimes. [Ignorance alert] How much is a dime worth? Is it 10c? I'll assume it is:

    Dimes = Temp / 10;
    Temp -= (Dimes * 10);

    Now nickels - how much are they? 5c? I'll assume it is 5c:

    Nickels = Temp / 5;
    Temp -= (Nickels * 5);

    Ok - now you've done it. Quarters holds the number of quarters, Dimes holds the number of dimes, Nickels holds the number of nickels, and Temp holds the remaining number of cents.

    Now you just gotta output it.

    I think that should work. If it's complete BS, or I've misunderstood the question, or something, then... well, my bad :D
     
  9. Turock

    Turock Supporting Member

    Apr 30, 2000
    Melnibone
    Perhaps you could use an "if" statement to make grammatical sense; maybe something like:

    if(dimes > 0) AND (dimes < 2)
    dimes = dime;
     
  10. moley

    moley

    Sep 5, 2002
    Hampshire, UK
    Heh... if it's greater than 0 and less than 2, there is only one number it can possibly be - 1 :D

    Also, that's not valid C++ syntax. It would be something like:

    cout << "You have " << Dimes;

    if (Dimes == 1)
    {
    cout << "dime" << endl;
    }
    else
    {
    count << "dimes" << endl;
    }
     
  11. Turock

    Turock Supporting Member

    Apr 30, 2000
    Melnibone
    Well, isn't "1" is what you want for one "dime"? However, I see your point, "(dime == 1)" is much more efficient.
    Sorry about the syntax; it's been several years since I wrote a C++ program. But, still my point was that an "if" statement should take care of the "grammatical sense".
     
  12. moley

    moley

    Sep 5, 2002
    Hampshire, UK
    Indeed :)
     
  13. Pah, C++....Pascal is the way forward! :D
     
  14. geshel

    geshel

    Oct 2, 2001
    Seattle
    Or, better yet, you can get with the times ;) and use

    Code:
    int NumberOfCents = static_cast<<int>int>(100 * Input);
    
    That's a C++ only thing, and is generally safer than the old C-style (int)x type of cast. But notice also that I didn't cast it to an int until after I did the multiplication. In this case it won't make much difference, but you may want to think about why in other cases that's a good habit to get into.

    Some questions to ask yourself:

    - can they type in a negative number? (if not, maybe use 'unsigned int' instead of int? though that might not be a good idea if your program will ever allow them to.)

    - when you convert the float to an int, do you care about any remaining decimals that get truncated? Can you warn the user if this happens?
     
  15. moley

    moley

    Sep 5, 2002
    Hampshire, UK
    lol, funny man :D

    Heh, up on your C++ eh geshel? You are right of course, static_cast is the way to go.

    About 3 years ago, I would have been the first to point that out, but it's been a fair while since I went near C++.
     
  16. jade

    jade

    Mar 8, 2002
    YYC
    double amountindollars = 3.61;
    double amountincents = amount*100;

    int dollarsneeded = amountincent/100;
    amountincent = amountincent % 100;
    if (dollarsneeded ==1)
    {
    cout << "1 dollar";
    }
    else
    {
    cout << dollarsneeded << "dollars";
    };// if

    int quartersneeded = amountincent/100;
    amountincent = amountincent % 50;
    if (quartersneeded ==1)
    {
    cout << "1 quarter";
    }
    else
    {
    cout << quartersneeded << "quarters";
    };// if

    just do that for all the coins. start with dollars and go down to quarters and then to dimes etc. i think the reason they want you to use a double is because the value might get higher then the max of int. i had to write a program like this for my com sci AP class. the teach told us to change the amount into cents and stubtract whole cents instead of working in decimals.

    if all else fails .. ask john davis, he knows everything about C++
     
  17. Phat Ham

    Phat Ham

    Feb 13, 2000
    DC
    ahh that's what i get for posting so early in the morning (yes 9am is very early for me).
    You could do a simple check just to make sure the input value is between 0 and 10. Something like:

    float Input = -1;
    while ((input < 0) || (input >10)) {
    cout << "input value blah blah blah";
    cin >> Input;
    }

    Good point Geshel. I'd write something about how to do this but I'm too drunk at the moment.:D (Yes I am drunk at 5 in the afternoon. Hey I'm in college give me a break.)
     
  18. moley

    moley

    Sep 5, 2002
    Hampshire, UK
    He said the maximum is $10 - so, no it will never exceed the maximum for an int. $10 is only 1000 cents - and 1000 will never be more than an int can hold. The minimum required maximum value for an int is 32767.

    And, it's likely that the program is being written on a 32-bit system, in which case, it's likely that the maximum value for an int is the the region of 2 billion.
     
  19. jade

    jade

    Mar 8, 2002
    YYC
    ahh sorry.. i should learn to read.
     
  20. geshel

    geshel

    Oct 2, 2001
    Seattle
    Of course, in this country at least, "quarters" are worth 25 cents, so those two lines should be

    int quartersneeded = amountincent / 25;
    amountincent = amountincent % 25.

    ;)