Thank you
It says thoug "want to try again?"
should I just make it reset ? If I just use printf to say yes or no it might be to simple. I dont know. Also dont know how I would do it though. Couldn't find a good C code database online.
Edit:
I gues I could do it with if. If xx then yes if yy then no. Im not sure what xx could be in order to make it ask this.
Last edited by XodoX; 07-03-2008 at 06:07 PM.
Usually a user query is done something like this:
This was what you were trying to show me before, right? I thought you were showing me something with a newline because of the backslash in it - \n.Code:do { print Enter your Sales Price scanf(" %f", &salesPrice); print Enter your Cash Paid etc. print Buy Another Item (y/n) ? scanf(" %c", &another); }while(another != 'n' || another != 'N')
Is this something I would use outside the classroom? Absolutely not. Scanf is just not well suited for the real world, these days. For a homework assignment, it's OK.
If you have more questions, please post your code so I can see what you're writing about. It's hard to understand the details from a posted description of something you can't see.
Last edited by Adak; 07-03-2008 at 07:11 PM.
After this part there gotta be something that asks "Want to do this(try this) again?"Meaning want to calculate something again or not. Thats what I meant.Code:Quarters = Remaining / 25; // How many quarters? 0 Remaining = Remaining % 25; // How much left? 9 Dimes = Remaining / 10; // How many dimes? 0 Remaining = Remaining % 10; // How much left? 9 Nickels = Remaining / 5; // How many nickels? 1 Pennies = Remaining % 5; // How many pennies? 4
So basically this part would work .
Which I would just add to the end.Code:print Buy Another Item (y/n) ? scanf(" %c", &another); }while(another != 'n' || another != 'n')
Last edited by XodoX; 07-03-2008 at 07:08 PM.
Right. The do loop I posted above, needs to wrap around your posted code.
do
your code all goes here
want to buy another item?
while, etc
Nope. The do needs to go near the top of your logic, and the question "want to buy another item?" needs to go after all the other logic has finished. Then comes the while line.
One of those small n's needs to be a capital N - my mistake.
Last edited by Adak; 07-03-2008 at 07:13 PM.
oh well this gets confusing now. But I am trying to understand.
Thats how I understand it.Code:do { SPDollars = (int)SalesPrice; // SPDollars = 5 SPCents = (int)(SalesPrice * 100.00) % 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents CPDollars = (int)CashPaid; CPCents = (int)(CashPaid * 100.00) % 100; if (Remaining < 0) { // We had to borrow a dollar Dollars--; Remaining += 100; }; printf ( "Buy Another Item (y/n)" ) ? scanf(" %c", &another); }while(another != 'n' || another != 'N');
Unless you meant to put the code in yours and put it to the very end of the entire code.
Last edited by XodoX; 07-03-2008 at 07:43 PM.
Another correction: || should be replaced with && in the while loop.
The code to handle each purchase and return of change (if any), needs to go inside the do loop.
I don't know what you mean by "put the code in yours" ?? When you bring the code blocks together into the program, I believe you'll see where they need to go.
If not, post the code, and describe the problem *in detail*. You have an ability to leave me floating on a sea of question marks, from some of your posts.
No, if this is correct than everything is fine. The code who needs to be handled is in there now.
It looks like this now. So the single purchase is in it.
If you dont mean it like this I assume you mean the ENTIRE code in the loop instead of just my part but I hope like THIS is ok.Code:#include <iostream.h> main() { double SalesPrice = 0.0; double CashPaid = 0.0; int SPDollars; // Sales price dollars int SPCents; // Sales price cents int CPDollars; // Cash paid dollars int CPCents; // Cash paid cents int Dollars; // Number of $1 bills int Quarters; // Number of Quarters int Dimes; // Number of Dimes int Nickels; // Number of Nickels int Pennies; // Number of Pennies int Remaining; do { SPDollars = (int)SalesPrice; // SPDollars = 5 SPCents = (int)(SalesPrice * 100.00) % 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents CPDollars = (int)CashPaid; CPCents = (int)(CashPaid * 100.00) % 100; if (Remaining < 0) { // We had to borrow a dollar Dollars--; Remaining += 100; }; printf ( "Buy Another Item (y/n)" ) ? scanf(" %c", &another); }while(another != 'n' && another != 'N') Quarters = Remaining / 25; // How many quarters? 0 Remaining = Remaining % 25; // How much left? 9 Dimes = Remaining / 10; // How many dimes? 0 Remaining = Remaining % 10; // How much left? 9 Nickels = Remaining / 5; // How many nickels? 1 Pennies = Remaining % 5; // How many pennies? 4 return 0; }
Last edited by XodoX; 07-03-2008 at 08:22 PM.
Like that is NOT OK, sorry.
For each purchase, you will have to have the code INSIDE the do loop, that gives back change, of course.
THEN you ask them if they'd like to make another purchase.
So drop this line of code, down below where you return the change (or print the amount of
change to be returned): (and you will print that up properly won't you?)
Code:}while(another != 'n' && another != 'N')
Last edited by Adak; 07-03-2008 at 08:44 PM.
Not that bad:
Put some good print statements in there to show what the Dollars, Quarter...Pennies, of change are, and aren't you all done?Code:#include <iostream.h> main() { double SalesPrice = 0.0; double CashPaid = 0.0; int SPDollars; // Sales price dollars int SPCents; // Sales price cents int CPDollars; // Cash paid dollars int CPCents; // Cash paid cents int Dollars; // Number of $1 bills int Quarters; // Number of Quarters int Dimes; // Number of Dimes int Nickels; // Number of Nickels int Pennies; // Number of Pennies int Remaining; do { SPDollars = (int)SalesPrice; // SPDollars = 5 SPCents = (int)(SalesPrice * 100.00) % 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents CPDollars = (int)CashPaid; CPCents = (int)(CashPaid * 100.00) % 100; if (Remaining < 0) { // We had to borrow a dollar Dollars--; Remaining += 100; }; printf ( "Buy Another Item (y/n)" ) ? scanf(" %c", &another); //}while(another != 'n' && another != 'N') Quarters = Remaining / 25; // How many quarters? 0 Remaining = Remaining % 25; // How much left? 9 Dimes = Remaining / 10; // How many dimes? 0 Remaining = Remaining % 10; // How much left? 9 Nickels = Remaining / 5; // How many nickels? 1 Pennies = Remaining % 5; // How many pennies? 4 }while(another != 'n' && another != 'N') //from 7 lines up, to here, that's all. :) return 0; }
Last edited by Adak; 07-03-2008 at 09:35 PM.
would something like this work? Use Rands so not sure what nickle is....Code:#include <string.h> #include <stdlib.h> typedef struct { int dollars; int nickel; int pennies; }CHANGE; // Calculate Change CHANGE convertChange(char s_money[], float f_money) { char dot = '.'; CHANGE change; char *m_ptr, pennies[5]; // Get the dollar part change.dollars = f_money; // Find the decimal point m_ptr = strchr(s_money, dot); // And get the pennies part strcpy(pennies, ++m_ptr); change.pennies = atoi ( pennies ); // Not sure what is a nickle return change; }
Yes, that way was mentioned back on the first page of this thread iirc, and can work just fine (although I didn't run your code).
Clearly, you'd need to add Quarters - 25 pennies, dimes - 10 pennies, and nickels are worth 5 pennies, to your change struc.
This is a problem that can be solved a lot of different ways.