8 Replies - 425 Views - Last Post: 03 March 2010 - 09:01 PM Rate Topic: -----

#1 jedidor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 10

Help with a snippet of code

Posted 03 March 2010 - 06:50 PM

Hello,

I am new to C++, and I am having problems getting this code to work. I believe I am close, but I am not sure what I am doing wrong. The object is to get enter a set amount of change as a whole number variable, ex: 467 pennies, and then have it produce the output at bottom using the least amount of coins ex:

dollars 4
quarters 2
dimes 1
nickles 1
pennies 2

#include <iostream>
#include <windows.h>
#include <ctype.h>
#include <iomanip.h>

using namespace std;

int main()
{
        system("cls");
        int dollars = 0;
        int quarters = 0;
        int dimes = 0;
        int nickles = 0;
        int pennies = 0;
        int change;
        int totalC;

        cout << "Enter the number of change: ";
        cin >> change;
        cin.ignore();
        if (dollars > 1; quarters > 1; dimes > 1; nickles > 1; pennies > 1)
        {
        cout << "Dollars" << dollars = change % 100;
        cout << "Quarters" << quarters = change - dollars % 25;
        cout << "Dimes" << dimes = change - dollars - quarters % 10;
        cout << "Nickles" << nickles = change - quarters - dimes % 5;
        cout << "Pennies" << pennies = change - dollars - quarters - dimes - nickles % 1;
        cout << "Change Verification" << totalC = dollars + quarters + dimes + nickles + pennies;
        }
        else (dollars == 1; quarters; == 1; dimes == 1; nickles == 1; pennies == 1)
        {
        cout << "Dollar" << dollars = change % 100;
        cout << "Quarter" << quarters = change - dollars % 25;
        cout << "Dime" << dimes = change - dollars - quarters % 10;
        cout << "Nickle" << nickles = change - dollars - quarters - dimes % 5;
        cout << "Penny" << pennies = change - dollars - quarters - dimes - nickles % 1;
        cout << "Change Verification" << totalC = dollars + quarters + dimes + nickles + pennies;
        }
        cin.get();
system("PAUSE");
return 0;
}



If anyone could help point me in the right direction I would be very appreciative.

This post has been edited by jedidor: 03 March 2010 - 06:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with a snippet of code

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Help with a snippet of code

Posted 03 March 2010 - 06:58 PM

dollars > 1; quarters > 1; dimes > 1; nickles > 1; pennies > 1


That isn't how if statements work. You would need to do:
dollars > 1 && quarters > 1 && dimes > 1 && nickles > 1 && pennies > 1)

Where && means 'and'. Use this if you want every one of those conditions to evaluate to true for the if to execute, and || (or) if you only want one of the conditions to be true for the if to execute.

else (dollars == 1; quarters; == 1; dimes == 1; nickles == 1; pennies == 1)

The else doesn't take any parameters after it. It is the case which is executed no matter what (if the matching if statement evaluates to false)
Was This Post Helpful? 0
  • +
  • -

#3 jedidor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 10

Re: Help with a snippet of code

Posted 03 March 2010 - 07:02 PM

Thank you for the explanation of he if/else statement. I have been looking everywhere to try and gain a clear understanding of how it worked. I appreciate you taking the time to explain.
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Help with a snippet of code

Posted 03 March 2010 - 07:21 PM

Here's a link I suggest bookmarking:
C++ Language Tutorial

That is one of the best tutorials for learning C++ on the web in my opinion.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Help with a snippet of code

Posted 03 March 2010 - 07:35 PM

Well you have a ton of syntax errors... but really it is the logic you have the most problem with.

First of all, how is dollars going to be > 0 or == 1 when it was initialized to zero?

Lets say I gave you 166 pennies and asked for change:

there are 100 cents in a dollar -- there for dollars = 166/100 = 1
with a remainder of 66 (i.e. 166 % 100 = 66)

there are 25 cents in a quarter so in 66 cents there are 66/25 = 2 quarters
with a remainder of 16

there are 10 cents in a dime, so in 16 cents there are 16/10 = 1 dime
and 6 cents remaining

there are 5 cents in a nickle, so 6 / 5 = 1 with 1 penny left over...

166 = 1 dollar, 2 quarters, 1 dime, 1 nickle, and 1 cent.

then to verify:

total = 1 * 100 + 2 * 25 + 1 * 10 + 1 * 5 + 1 = 166

you need to re-work your code. Don't put the calculation inside of the cout << lines. while you technically CAN do this, you really should not. (and if you do, you need to use parentheses).
Was This Post Helpful? 0
  • +
  • -

#6 jedidor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 10

Re: Help with a snippet of code

Posted 03 March 2010 - 07:44 PM

I have been using that website, cprogramming.com, and brief version of starting out with c++. I think I got the code to work for the most part. I just have to get it to print the results now, and make sure the math is calculating right. For some reason I was thinking that I needed the "total = or == change % 25", forgetting that the number would automatically be placed in there by just doing "change % 25". I am just going to use what is there as placeholders to attempt to research the code get the results I need. I am trying to figure out a way to take like change % dollars, save that amount then use it as the next factor. I am thinking I would have to introduce something like int totala; int totalb; int totalc;

and make it look something like:
cout << totalA==change%dollars << endl; 
cout << totalB==totalA%quarters << endl;
cout << totalC==totaB%dimes << endl; 



Please let me know if my logic seems completely wrong on this. Like I was saying I am just looking for the right direction, not someone to do the work for me :)

This is what I have now compared to before.

#include <iostream>
#include <windows.h>
#include <ctype.h>
#include <iomanip.h>

using namespace std;

int main()
{

        const long dollars = 100;
        const long quarters = 25;
        int dimes = 10;
        int nickles = 5;
        int pennies = 1;
        int change;
        int totalC;

        cout << "Enter the number of change: ";
        cin >> change;
        
        if (dollars > 1 && quarters > 1 && dimes > 1 && nickles > 1 && pennies > 1)
        {
        cout << "Dollars" <<  change % dollars << endl;
        cout << "Quarters" << change - dollars % quarters << endl;
        cout << "Dimes" << change - dollars - quarters % dimes << endl;
        cout << "Nickles" << change - quarters - dimes % nickles << endl;
        cout << "Pennies" << change % dollars % quarters % dimes % nickles % pennies << endl;
        }
        else if (dollars == 1 && quarters == 1 && dimes == 1 && nickles == 1 && pennies == 1)
        {
        cout << "Dollar" << change % dollars << endl;
        cout << "Quarter" << change - dollars % quarters << endl;
        cout << "Dime" << change - dollars - quarters % dimes << endl;
        cout << "Nickle" << change - dollars - quarters - dimes % nickles << endl;
        cout << "Penny" << change - dollars - quarters - dimes - nickles % 1 << endl;
        }
        else {
        }
        cin.get();
system("PAUSE");
return 0;
}




Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Help with a snippet of code

Posted 03 March 2010 - 07:46 PM

Well, yes, to me your logic looks completely wrong (if you are trying to do what I demonstrated above).
Was This Post Helpful? 1
  • +
  • -

#8 jedidor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 10

Re: Help with a snippet of code

Posted 03 March 2010 - 07:49 PM

Thank you for the response Nick, those were actually the same conclusions I was starting to come to. I do appreciate you reaffirming what I was thinking that I had wrong though. I am actually reworking the code per your suggestions and the epiphany that came to me.

I do greatly appreciate all the feedback.
Was This Post Helpful? 0
  • +
  • -

#9 jedidor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 10

Re: Help with a snippet of code

Posted 03 March 2010 - 09:01 PM

Ok, I got it working mostly how I want it to. I know the else if statement is somewhat useless right now, and I will be reworking that soon. I just wanted to express my thanks again on the help with the code to Nick & Erik.

Quote

if (dollars > 100 && quarters > 25 && dimes > 10 && nickles > 5 && pennies > 1)
{
cout << "Dollars" << (change/dollars) << endl;
cout << "Quarters" << (change%dollars/quarters) << endl;
cout << "Dimes" << (change%dollars%quarters/dimes) << endl;
cout << "Nickles" << (change%dollars%quarters%dimes/nickles) << endl;
cout << "Pennies" << (change%dollars%quarters%dimes%nickles/pennies) << endl;
}
else if (dollars < 101 && quarters < 26 && dimes < 11 && nickles < 6 && pennies < 2)
{
cout << "Dollar" << " " << (change/dollars) << endl;
cout << "Quarter" << " " << (change%dollars/quarters) << endl;
cout << "Dime" << " " << (change%dollars%quarters/dimes) << endl;
cout << "Nickle" << " " << (change%dollars%quarters%dimes/nickles) << endl;
cout << "Penny" << " " << (change%dollars%quarters%dimes%nickles/pennies) << endl;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1