Question..Help..Real Number Object ?

This is a discussion on Question..Help..Real Number Object ? within the C++ Programming forums, part of the General Programming Boards category; Hello, I coded this program today for an intro C++ class, works great and all that good stuff I just ...

  1. #1
    C0de 3
    Join Date
    Oct 2005
    Posts
    3

    Question..Help..Real Number Object ?

    Hello,

    I coded this program today for an intro C++ class, works great and all that good stuff I just had a question maybe someone can help with.

    The teacher says we must use real number objects that identify price,payment, and change.

    My question is did I do this already or am I missing the concept, or have I missed it completely ?

    Any help is appreciated, if I am asking for to much information without putting enough into my source I am sorry.

    Thank you in advance...

    Code:
    /* 
    	Program Description:This Program operates as a Cash Register.
    	User enters amount of purchase, then enters amount paid toward
    	purchase. Change to return to user is calculated as well as
    	number of returned coins are counted. All information is formatted
    	and output to user.
    */
    
    //	system defined preprocessor statement for cin/cout operations
    #include < iostream.h > 
    
    //	programmer defined preprocessor statement for setreal operation 
    #include "textlib.h" 
    
    //	system defined preprocessor statement for set width operation
    #include <iomanip.h>
    
    int main( ) 
    { 
    	// comment constant object above the code 
    	const int QUARTERS = 25;
    	//
    	const int DIMES = 10;
    	//
    	const int NICKELS = 5;
    	//
    	const int PENNIES = 1;
    
    	// comment object above the code 
    	double change;
    	//
    	double price;
    	//
    	double payment;
    	
    	//
    	int coinChange;
    	//
    	int dollars;
    	//
    	int quarterChange;
    	//
    	int dimeChange;
    	//
    	int nickelChange;
    	//
    	int pennyChange;
    	
    	cout << "Enter the purchase total $: ";
    	cin  >> price;
    	cout << "Enter the payment $: ";
    	cin  >> payment;
    
    	// comment calculations 
    	change = payment - price;
    
    	//
    	dollars = int(change);
    
    	//
    	coinChange = (int((change-dollars)*100));
    
    	//
    	quarterChange = coinChange/QUARTERS;
    	coinChange -= (quarterChange*QUARTERS);
    
    	//
    	dimeChange = coinChange/DIMES;
    	coinChange -= (dimeChange*DIMES);
    
    	//
    	nickelChange = coinChange/NICKELS;
    	coinChange -= (nickelChange*NICKELS);
    
    	//
    	pennyChange = coinChange/PENNIES;
    	
    	cout << "Purchase Total" << setw(7) << price << setreal(1,2) << endl;
    	cout << "Payment" << setw(14) << payment << setreal(1,2) << endl;
    	cout << "Change" << setw(15) << change << setreal(1,2) << endl;
    	cout << "Dollars" << setw(14) << dollars << setreal(1,0) << endl;
    	cout << "Quarters" << setw(13) << quarterChange << setreal(1,1) << endl;
    	cout << "Dimes" << setw(16) << dimeChange << setreal(1,1) << endl;
    	cout << "Nickels" << setw(14) << nickelChange << setreal(1,1) << endl;
    	cout << "Pennies" << setw(14) << pennyChange << setreal(1,1) << endl;
    
    	return 0; 
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    c and c++ languages do not have a "real" data type, only float and double. I guess you could typedef a real
    Code:
    typedef float real;
    ...
    real  change, price, payment;

  3. #3
    C0de 3
    Join Date
    Oct 2005
    Posts
    3
    Heres a little bit of a revised code I added all the comments.

    Code:
    //	Benjamin Shively
    /* 
    	Program Description:This Program operates as a Cash Register.
    	User enters amount of purchase, then enters amount paid toward
    	purchase. Change to return to user is calculated as well as
    	number of returned coins are counted. All information is formatted
    	and output to user.
    */
    
    //	system defined preprocessor statement for cin/cout operations
    #include < iostream.h > 
    
    //	programmer defined preprocessor statement for setreal operation 
    #include "textlib.h" 
    
    //	system defined preprocessor statement for set width operation
    #include <iomanip.h>
    
    int main( ) 
    { 
    	//	quarter value
    	const int QUARTERS = 25;
    	//	dime value
    	const int DIMES = 10;
    	//	nickel value
    	const int NICKELS = 5;
    	//	penny value
    	const int PENNIES = 1;
    
    	//	total purchase price
    	double price;
    	//	total of payment towards purchase
    	double payment;
    	//	cash due to customer after payment
    	double change;
    	
    	//	change part of cash due to customer 
    	int coinChange;
    
    	//	dollar part of cash due to customer
    	int dollars;
    
    	//	number of quarters returned to customer
    	int quarterChange;
    
    	//	number of dimes returned to customer
    	int dimeChange;
    
    	//	number of nickels returned to customer
    	int nickelChange;
    
    	//	number of pennies returned to customer	
    	int pennyChange;
    	
    	cout << "Enter the purchase total $: ";
    	cin  >> price;
    	cout << "Enter the payment $: ";
    	cin  >> payment;
    
    	//	change result ater total price is subtracted from payment 
    	change = payment - price;
    
    	//	dollars returned in change due to customer	
    	dollars = int(change);
    
    	//	change returned from cash to customer
    	coinChange = (int((change-dollars)*100));
    
    	//	number of quarters returned to customer
    	quarterChange = coinChange/QUARTERS;
    	coinChange -= (quarterChange*QUARTERS);
    
    	//	number of dimes returned to customer
    	dimeChange = coinChange/DIMES;
    	coinChange -= (dimeChange*DIMES);
    
    	//	number of nickels returned to customer
    	nickelChange = coinChange/NICKELS;
    	coinChange -= (nickelChange*NICKELS);
    
    	//number of pennies returned to customer
    	pennyChange = coinChange/PENNIES;
    	
    	cout << "Purchase Total" << setw(7) << price << setreal(1,2) << endl;
    	cout << "Payment" << setw(14) << payment << setreal(1,2) << endl;
    	cout << "Change" << setw(15) << change << setreal(1,2) << endl;
    	cout << "Dollars" << setw(14) << dollars << setreal(1,0) << endl;
    	cout << "Quarters" << setw(13) << quarterChange << setreal(1,1) << endl;
    	cout << "Dimes" << setw(16) << dimeChange << setreal(1,1) << endl;
    	cout << "Nickels" << setw(14) << nickelChange << setreal(1,1) << endl;
    	cout << "Pennies" << setw(14) << pennyChange << setreal(1,1) << endl;
    
    	return 0; 
    }

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Meth
    Heres a little bit of a revised code I added all the comments.
    all you had to do was edit your original post

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If your intro to C++ class is teaching you to use <iostream.h> and <iomanip.h>, then it is behind the times. That code is non-standard and won't work on modern compilers. Hopefully your teacher can update the material to use standard C++.

    As for your use of real numbes, you are using double, and a double holds a real number, so you are probably fine. My guess is that the teacher meant that you should not use int, since int only holds integers instead of any real number.

  6. #6
    C0de 3
    Join Date
    Oct 2005
    Posts
    3
    Quote Originally Posted by Daved
    If your intro to C++ class is teaching you to use <iostream.h> and <iomanip.h>, then it is behind the times. That code is non-standard and won't work on modern compilers. Hopefully your teacher can update the material to use standard C++.

    As for your use of real numbes, you are using double, and a double holds a real number, so you are probably fine. My guess is that the teacher meant that you should not use int, since int only holds integers instead of any real number.
    Thank you,

    Yes I have gathered this, this teacher just seems like she is coding on the 80s I have done a lot of coding classes and so I am not a total newbee, she hammers us hard on comments and spacing not so much on coding so lol

    But that is what I was asking

    Thx again my friend.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 02:48 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  4. Replies: 5
    Last Post: 05-30-2003, 01:46 AM
  5. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21