Click here to Skip to main content
16,006,013 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Need help. How to get frames from link-layer in Windows XP Pin
geo_m17-Jan-05 0:19
geo_m17-Jan-05 0:19 
GeneralRe: Need help. How to get frames from link-layer in Windows XP Pin
Chuaaico26-Jan-05 0:48
Chuaaico26-Jan-05 0:48 
GeneralRe: Need help. How to get frames from link-layer in Windows XP Pin
geo_m26-Jan-05 1:04
geo_m26-Jan-05 1:04 
GeneralRe: Need help. How to get frames from link-layer in Windows XP Pin
Chuaaico26-Jan-05 15:21
Chuaaico26-Jan-05 15:21 
GeneralRe: Need help. How to get frames from link-layer in Windows XP Pin
geo_m26-Jan-05 20:05
geo_m26-Jan-05 20:05 
Generalbeginner question Pin
Anonymous16-Jan-05 14:44
Anonymous16-Jan-05 14:44 
GeneralRe: beginner question Pin
David Crow17-Jan-05 4:21
David Crow17-Jan-05 4:21 
GeneralRe: beginner question Pin
Adam Fowler17-Jan-05 5:31
Adam Fowler17-Jan-05 5:31 
Thanks for replying. I am not getting any errors, just a faulty answer. payed and cost are both entered by the user at run-time. All of the others are int's. Just to avoid confusion, here's the code in it's entirety: (I've included sample output at the end.)

<br />
/* Lab 1<br />
   Adam Fowler<br />
   for CS 102, spring '05<br />
   ----------------------<br />
   This program prompts for a cost of something at the store (up to $99.99) and the <br />
   amount given to pay for it. The person gives a variable amount of money to the <br />
   merchant and this program displays the number of twenties, tens, fives, ones, <br />
   quarters, dimes, nickels, and pennies that are to be given in change.<br />
*/<br />
#include <iostream><br />
#include <iomanip> // needed for cin/cout.precision()<br />
using namespace std;<br />
<br />
void makeChange(double, double);<br />
void displayChange();<br />
bool isNotCool(double,double);<br />
<br />
int twenty, ten, five, one, quarter, dime, nickel, penny; // global variables<br />
<br />
int main() {<br />
	double cost, payed;<br />
	<br />
	twenty = ten = five = one = quarter = dime = nickel = penny = 0;<br />
<br />
	cout << fixed;    // forces fixed precision<br />
	cin.precision(2); // sets precision to appropriate values for dollars and cents<br />
	cout.precision(2);<br />
<br />
	cout << "This program takes in the cost of an item and the amount given to " <br />
             << endl<br />
	     << "pay for it, and displays the change." << endl << endl;<br />
	do {<br />
	cout << "Enter the cost and the amount given " << endl<br />
	     << "to pay for it separated by a space: ";<br />
	cin >> cost >> payed;<br />
	} while (isNotCool(cost,payed));<br />
<br />
	makeChange(cost, payed);<br />
	displayChange();<br />
<br />
	cout << endl << "END PROGRAM" << endl;<br />
<br />
	return 0;<br />
}<br />
<br />
//function to test sanity<br />
bool isNotCool(double cost, double payed) {    // All this seems good for is causing an<br />
	if (cost > 0 && cost < 100)            // infinite loop, at the moment.  You'd<br />
		if (payed > 0 && payed <= 100) // think it would stop at the<br />
			if (payed >= cost)     // cin >> cost >> payed;<br />
				return false;  // statement.<br />
	return true; // it isn't cool<br />
}<br />
<br />
// function to make change<br />
void makeChange(double cost, double payed) {<br />
	double difference;<br />
	<br />
	difference = payed - cost;<br />
<br />
	twenty = (int) difference / 20;          // count twenties<br />
	difference = difference - (twenty * 20); // subtract from difference<br />
                                                 // move on down the line<br />
	ten = (int) difference / 10;<br />
	difference = difference - (ten * 10);<br />
<br />
	five = (int) difference / 5;<br />
	difference = difference - (five * 5);<br />
<br />
	one = (int) difference;<br />
	difference = difference - one;<br />
<br />
	quarter = ((int) (difference * 100)) / 25;<br />
	difference = difference - (quarter * .25);<br />
<br />
	dime = ((int) (difference * 100)) / 10;<br />
	difference = difference - (dime * .1);<br />
<br />
	nickel = ((int) (difference * 100)) / 5;<br />
	difference = difference - (nickel * .05);<br />
<br />
// -------------------- Fix Me! ----------------------------------------------------------<br />
cout << endl << "Difference before penny: " << difference << endl;<br />
cout << "Value of penny before: " << penny << endl;<br />
cout << "> penny = (int) (difference * 100); // Why isn't this line working?" << endl;<br />
cout << "> difference = difference - (penny * .01); // This \"should\" be 0 now." << endl;<br />
	<br />
	penny = (int) (difference * 100); // Why isn't this line working?<br />
	difference = difference - (penny * .01); // This "should" be 0 now.<br />
<br />
cout << "Value of penny after: " << penny << endl;<br />
cout << "Difference after penny: " << difference << endl << endl;<br />
cout << "cout.flush = " << cout.flush << " <-- Does this matter?" << endl << endl;<br />
// ---------------------------------------------------------------------------------------<br />
}<br />
<br />
// time to display the data<br />
void displayChange() {<br />
<br />
	if (twenty) {<br />
		cout << twenty << " twent";<br />
		if (twenty != 1) {<br />
			cout << "ies" << endl;<br />
		} else {<br />
			cout << "y" << endl;<br />
		}<br />
	}<br />
<br />
	if (ten) {<br />
		cout << ten << " ten";<br />
		if (ten != 1) {<br />
			cout << "s" << endl;<br />
		} else {<br />
			cout << endl;<br />
		}<br />
	}<br />
<br />
	if (five) {<br />
		cout << five << " five";<br />
		if (five != 1) {<br />
			cout << "s" << endl;<br />
		} else {<br />
			cout << endl;<br />
		}<br />
	}<br />
<br />
	if (one) {<br />
		cout << one << " one";<br />
		if (one != 1) {<br />
			cout << "s" << endl;<br />
		} else {<br />
			cout << endl;<br />
		}<br />
	}<br />
<br />
	if (quarter) {<br />
		cout << quarter << " quarter";<br />
		if (quarter != 1) {<br />
			cout << "s" << endl;<br />
		} else {<br />
			cout << endl;<br />
		}<br />
	}<br />
<br />
	if (dime) {<br />
		cout << dime << " dime";<br />
		if (dime != 1) {<br />
			cout << "s" << endl;<br />
		} else {<br />
			cout << endl;<br />
		}<br />
	}<br />
<br />
	if (nickel) {<br />
		cout << nickel << " nickel";<br />
		if (nickel != 1) {<br />
			cout << "ies" << endl;<br />
		} else {<br />
			cout << endl;<br />
		}<br />
	}<br />
<br />
	if (penny) {<br />
		cout << penny << " penn";<br />
		if (penny != 1) {<br />
			cout << "ies" << endl;<br />
		} else {<br />
			cout << "y" << endl;<br />
		}<br />
	}<br />
}


This program takes in the cost of an item and the amount given to
pay for it, and displays the change.

Enter the cost and the amount given
to pay for it separated by a space: .79 1

Difference before penny: 0.01
Value of penny before: 0
> penny = (int) (difference * 100); // Why isn't this line working?
> difference = difference - (penny * .01); // This "should" be 0 now.
Value of penny after: 0
Difference after penny: 0.01

cout.flush = 1 <-- Does this matter?

2 dimes

END PROGRAM


But it does what it's supposed to, under certain circumstances:
This program takes in the cost of an item and the amount given to
pay for it, and displays the change.

Enter the cost and the amount given
to pay for it separated by a space: 25.99 100

Difference before penny: 0.01
Value of penny before: 0
> penny = (int) (difference * 100); // Why isn't this line working?
> difference = difference - (penny * .01); // This "should" be 0 now.
Value of penny after: 1
Difference after penny: 0.00

cout.flush = 1 <-- Does this matter?

3 twenties
1 ten
4 ones
1 penny

END PROGRAM


Thanks for your help.
GeneralRe: beginner question Pin
David Crow17-Jan-05 6:00
David Crow17-Jan-05 6:00 
GeneralRe: beginner question Pin
Adam Fowler17-Jan-05 6:12
Adam Fowler17-Jan-05 6:12 
GeneralRe: beginner question Pin
David Crow17-Jan-05 6:28
David Crow17-Jan-05 6:28 
GeneralRe: beginner question Pin
Adam Fowler17-Jan-05 6:43
Adam Fowler17-Jan-05 6:43 
QuestionWhy .net create new project error? Pin
willyfu16-Jan-05 14:40
willyfu16-Jan-05 14:40 
QuestionModeless dialog receiving unwanted messages? Pin
Cpt Rick16-Jan-05 14:11
Cpt Rick16-Jan-05 14:11 
QuestionControl Messages and Direct/Indirect Parent Window? Pin
Maverick16-Jan-05 13:37
Maverick16-Jan-05 13:37 
GeneralCListCtrl ItemSelected Notification... Pin
Maverick16-Jan-05 13:23
Maverick16-Jan-05 13:23 
GeneralRe: CListCtrl ItemSelected Notification... Pin
PJ Arends16-Jan-05 13:47
professionalPJ Arends16-Jan-05 13:47 
GeneralDifference between SBP_LOWERTRACKHORZ and SBP_UPPERTRACKHORZ Pin
Luis Mejia16-Jan-05 12:24
Luis Mejia16-Jan-05 12:24 
GeneralIcon Serialization Pin
smcd16-Jan-05 9:47
smcd16-Jan-05 9:47 
QuestionHow to write an IDE Context Manager Pin
fabiocannizzo16-Jan-05 7:36
fabiocannizzo16-Jan-05 7:36 
AnswerRe: How to write an IDE Context Manager Pin
Roger Allen16-Jan-05 23:27
Roger Allen16-Jan-05 23:27 
GeneralRe: How to write an IDE Context Manager Pin
Anonymous17-Jan-05 5:57
Anonymous17-Jan-05 5:57 
GeneralMoving the caret in a CRichEditCtrl Pin
dlarkin7716-Jan-05 5:22
dlarkin7716-Jan-05 5:22 
GeneralRe: Moving the caret in a CRichEditCtrl Pin
rodgersgb15-Apr-05 16:24
rodgersgb15-Apr-05 16:24 
GeneralChanging Background Color of edit box in VC++ Pin
phijophlip16-Jan-05 2:15
phijophlip16-Jan-05 2:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.