PDA

View Full Version : C++ Class question...




kmorales13
Dec 1, 2009, 01:55 AM
Will someone please copy and run this program for me to confirm that this program runs in Xcode 3.2 on Mac OS 10.6.1? I'm having trouble in a class because, apparently, my teacher wasn't prepared for a student using the Xcode IDE... granted it may not be the most efficient design but give me a break, it's a programming fundamentals class heh.

#include <iostream>
#include <cmath>
using namespace std;

const int DOLLAR = 1;
const float QUARTER = .25;
const float DIME = .10;
const float NICKEL = .05;
const float PENNY = .01;

//Function prototypes
void itemizedChange(float, float, //Input
int&, int&, int&, int&); //Output

int main()
{
float total; //Input - Total ammount due
float cashTendered; //Input - Cash tendered
float changeDue; //Output - Change Due
int dollarReturn;
int quarterReturn;
int dimeReturn;
int nickelReturn;
int pennyReturn;

//Get total amount due.
cout << "Enter total amount due: $";
cin >> total;

while(total != 0)
{
//Get amount tendered.
cout << "Enter total cash tendered: $";
cin >> cashTendered;
cout << endl;

if (cashTendered > total)
{
//Display amount due and ammount tendered.
cout << "Amount due: $" << total << endl;
cout << "Amount tendered: $" << cashTendered << endl << endl;

//Display total change due.
changeDue = cashTendered - total;
cout << "Change due is: $" << changeDue << endl;

//Find exact amount of DOLLARS needed and subtract it from the change due.
dollarReturn = floor(changeDue);
cashTendered -= dollarReturn;

//Itemized change due and pass totals to main function
itemizedChange(total, cashTendered, //Input-Ammount due and cash tendered
quarterReturn, dimeReturn, nickelReturn, pennyReturn); //Output-Itemized change due

//Display an itemized list of change due.
cout << " " << dollarReturn << " dollars - " << quarterReturn << " quarters - "
<< dimeReturn << " dimes - " << nickelReturn << " nickels - " << pennyReturn << " pennies." << endl << endl;
}
else if(cashTendered < total)
{
//Display error message.
cout << "Amount Due: $" << total << endl;
cout << "Amount tendered: $" << cashTendered << endl << endl;
cout << "Cash tendered does not cover the total ammount due." << endl << endl;
}
else
{
//Display equal change message.
cout << "Amount Due: $" << total << endl;
cout << "Amount tendered: $" << cashTendered << endl << endl;
cout << "Cash tendered is equal to the total ammount, no change due." << endl << endl;
}

//Get total amount due.
cout << "Enter total amount due: $";
cin >> total;
}

//Display programmer ID
cout << endl;
cout << "KMorales M867L5p2.cpp" << endl;
cout << "Lab5 part2 11-19-09" << endl;

return 0;
}

//******************* Function Definitions *******************
//Calculate and display an itemized list of the change due
void itemizedChange(float total, float cashTendered, //Input-User defined input
int& quarterReturn, int& dimeReturn, //Output-Itemized change calculations
int& nickelReturn, int& pennyReturn) //Output-Itemized change calculations
{
float itemizedChangeDue;//Output - Holding place for total change due
float temp; //Output - Temporary holding place for value to be deducted from itemizedChangeDue

itemizedChangeDue = cashTendered - total + 0.0005;

//Find exact amount of QUARTERS needed and subtract it from the change due.
quarterReturn = itemizedChangeDue / QUARTER;
quarterReturn = floor(quarterReturn);
temp = quarterReturn * QUARTER;
itemizedChangeDue -= temp;

//Find exact amount of DIMES needed and subtract it from the change due.
dimeReturn = itemizedChangeDue / DIME;
dimeReturn = floor(dimeReturn);
temp = dimeReturn * DIME;
itemizedChangeDue -= temp;

//Find exact amount of NICKELS needed and subtract it from the change due.
nickelReturn = itemizedChangeDue / NICKEL;
nickelReturn = floor(nickelReturn);
temp = nickelReturn * NICKEL;
itemizedChangeDue -= temp;

//Find exact amount of PENNIES needed and subtract it from the change due.
pennyReturn = itemizedChangeDue/ PENNY;
pennyReturn = floor(pennyReturn);
temp = pennyReturn * PENNY;
itemizedChangeDue -= temp;
}



Cromulent
Dec 1, 2009, 02:50 AM
Frankly it shouldn't make a difference what IDE you used. Just get the teacher to compile it from the command line.

Yes it compiles just fine from the command line on Snow Leopard although I would say you should give the user an option to quit after they have done the transaction :).

Edit: You have also spelt "Amount" incorrectly.

kmorales13
Dec 1, 2009, 03:20 AM
Not that I think many MacRumor people are running Windows too but what happens if you compile it on a PC?

Thanks on the amount problem...

Cromulent
Dec 1, 2009, 04:29 AM
Not that I think many MacRumor people are running Windows too but what happens if you compile it on a PC?

Thanks on the amount problem...

It depends what C++ compiler your teacher is using. The Microsoft C++ compiler? G++? Intel C++ compiler?

I'm guessing the Microsoft one. Anyway it shouldn't make a difference, as far as I can tell the code you posted is perfectly fine and should compile with any of the compilers I mentioned above (untested assertion).

This is the number one problem when it comes to IDEs. If people never learn how to use the command line to compile programs they end up thinking code is broken or does not work simply because they don't understand how to do things without an IDE holding their hand.

newb16
Dec 1, 2009, 09:05 AM
On gcc 3.3.3 from cygwin it complains on

100: error: call of overloaded `floor(int&)' is ambiguous
/usr/include/math.h:90: error: candidates are: double floor(double)
/usr/include/c++/3.3.3/cmath:345: error: long double
std::floor(long double)
/usr/include/c++/3.3.3/cmath:334: error: float
std::floor(float)

because you call floor with integer argument. The same on VS 2005.

Wes
Dec 1, 2009, 03:24 PM
Not strictly related to your question but after a 10 second look at your class you are using floating point values to represent money which is asking for rounding inaccuracies.

http://docs.sun.com/source/806-3568/ncg_goldberg.html