C++ Class question...

Discussion in 'Mac Programming' started by kmorales13, Nov 30, 2009.

  1. kmorales13 macrumors newbie

    Joined:
    Nov 30, 2009
    #1
    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.

    Code:
    #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;
    }
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    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.
     
  3. kmorales13 thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #3
    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...
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    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.
     
  5. newb16 macrumors regular

    Joined:
    Feb 27, 2008
    #5
    On gcc 3.3.3 from cygwin it complains on
    Code:
    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.
     
  6. Wes macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #6

Share This Page