Whoa whoa!
You forgot to ask the user how much change he/she has!
Code:cout << "What is the change amount? "; cin >> change;
Whoa whoa!
You forgot to ask the user how much change he/she has!
Code:cout << "What is the change amount? "; cin >> change;
Ok, look at this
This crashes BUT I can see just as it crashes, that the logic is correct. (I entered 35, and got 1 quarter, and 1 dime) then it crashed.Code:#include <iostream> //#include <conio.h> Not standard C++ :( // We could do: using namespace std; // But we'd be including more than we need/want! using std::cout; using std::cin; using std::endl; int main() { int change; int quarters=0, dimes=0, nickels=0, pennies=0; cout << "What is the change amount? "; cin >> change; while(change > 0) { if (change >= 25) { quarters++; change = change - 25; continue; } if(change >= 10) { dimes++; change = change - 10; continue; } if(change >= 5) { nickels++; change = change - 5; continue; } if(change >=1) { pennies++; change = change - 1; continue; } } // <-- You forgot to stop your while loop cout << "Quarters: " << quarters << endl; cout << "Dimes: " << dimes << endl; cout << "Nickels: " << nickels << endl; cout << "Pennies: " << pennies << endl; return 0; }
Probably not crashing. Probably just closing. Check the FAQ for information on how to stop this from happening.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
hey pal listen to scribbler. you are going to complex for an easy task. Look at the program that I posted on the first page. On 1 pass it calculates the change.
fMoney would be the change. If you have questions on this implementation just ask.Code:void change(float fMoney, int* iQuart, int* iDime, int* iNick, int* iPenn) { //Determine how many quarters, dimes, nickels and pennies are in the money provided *iQuart = (int)(fMoney / .25); fMoney = fMoney - (.25 * *iQuart); *iDime = (int)(fMoney / .10); fMoney = fMoney - (.10 * *iDime); *iNick = (int)(fMoney / .05); fMoney = fMoney - (.05 * *iNick); *iPenn = ceilf(fMoney*100); //ceilf used to counter rounding error experienced with floating point numbers }
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
Originally Posted by xlokixTry adding the snippets that to the source so that the window doesn't autoclose!Code:#include <stdio.h> ------------------[BLAHBLAHBLAH] cout << "Quarters: " << quarters << endl; cout << "Dimes: " << dimes << endl; cout << "Nickels: " << nickels << endl; cout << "Pennies: " << pennies << endl; getchar();getchar(); return 0; }
Well I'm trying to keep it simple, My teacher probably wouldn't believe I wrote the code if I included stdio.h, since we've never learned that before. Or getchar (whatever the hell that is ). But yes, that did fix the crash. Is there another way around it without stdio.h ?
Here, I'll update on what I have so far.
Code:#include <iostream.h> #include <stdio.h> main() { int change; int q=0,d=0,n=0,p=0; cout << "What is the change?"; cin >> change; while(change > 0) { if (change >= 25) { q++; change = change - 25; continue; } if(change >= 10) { d++; change = change - 10; continue; } if(change >= 5) { n++; change = change - 5; continue; } if(change >=1) { p++; change = change - 1; continue; } } cout << "Amount of quarters " << q << '\n'; cout << "Amount of dimes " << d << '\n'; cout << "Amount of nickels " << n << '\n'; cout << "Amount of pennies " << p << '\n'; getchar();getchar(); return 0; }
This is what getchar() does: It basically just asks for user input.
If your teacher looks at the source and goes: "WTH", then you can explain it to him, as far as what everything does! And if he still doesn't believe you, then send him to this forum topic, and he'll probably be like:Code:#include <iostream> // Allow us to use cout and endl #include <stdio.h> // Allows us to use getchar(); // We could also do: // using namespace std; using std::endl; using std::cout; int main() { // Main always returns an integer! // Gets a character from console input (like cout), except only ONE character! // Not a string int myCharacter = getchar(); cout << myCharacter << endl; return 0; }
"Wow somebody asked for help! Holy ****! 50 points extra credit for you!" lol
---EDIT---
Be sure to test this out, practice makes perfect!
Last edited by Kleid-0; 01-17-2005 at 04:17 PM.
I don't recommend telling a C++ student to use C functions, and then challenge the instructor. There's a curriculum to follow and all you'll do is throw a rock into the machine. Not to mention that is a complete garbage solution to what he's asking. There is a member function of cin which would suit perfectly, however I'd sooner let his curriculum teach the method first. Just giving the answers robs him of a valuable learning tool...studying.
In my experience, instructors don't look at the compiled/executable file. They are only interested in the sourcecode which they look over themselves to verify the student has grasped the concepts the project places before them (as well as observe their logic skills). The window closing upon program exit is a non issue.
Alright, heres a C++ approach still using the one pass way yet no floats or strange rounding functions:
Code:#include <iostream> #include <math.h> using namespace std; int main() { int fChange; int Quart, Dime, Nick, Penn; cout << "Enter the change:"; cin >> fChange; //calculate change on 1 pass Quart = (fChange / 25); fChange = fChange - (25 * Quart); Dime = (fChange / 10); fChange = fChange - (10 * Dime); Nick = (fChange /5); fChange = fChange - (5 * Nick); Penn = fChange; cout << "Quarters: "<< Quart << endl; cout << "Dimes: " <<Dime << endl; cout << "Nickels: "<< Nick << endl; cout << "Pennies: " << Penn << endl; return 0; }
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
Actually, I'm in C++ 1, and there will be no C++ 2 next year. So I will not learn more. And it's only a half a year class, so I'm finished with the class in about a week.
Mine was very similar. And since so many people have already posted code, I might as well follow suit.Originally Posted by andyhunter
Code:#include <iostream> using std::cout; using std::cin; using std::endl; main() { int quarters, dimes, nickels, pennies; int change; cout << "What is the change amount? "; cin >> change; quarters = change / 25; change %= 25; dimes = change / 10; change %= 10; nickels = change / 5; change %= 5; pennies = change; cout << "Amount of quarters " << quarters << endl; cout << "Amount of dimes " << dimes << endl; cout << "Amount of nickels " << nickels << endl; cout << "Amount of pennies " << pennies << endl; return 0; }
Last edited by Scribbler; 01-17-2005 at 04:43 PM.
Before the way I'm using now, I wrote something similar to scribblers way and got weird numbers so I changed my method.
Ok. Next up, Project 2!
Oh boy this is going to be a long one. The user thinks of a number, and the program guesses. The program makes a guess of 50, then the user tells whether it's higher or lower until the program eventually gets it.
Booo! I'm gonna eat dinner before I start this. Leave some suggestions on how to start it if you want. I presume that there will be a hell of a lot of nested if statements.
I won't post any code for that project, but it's a typical learning tool.
Hint. Search your textbook for binary search method.
Of course, there are only so many ways of solving a particular problem. And I do recall saying :
Quote:
Originally Posted by andyhunter
Alright, heres a C++ approach still using the one pass way yet no floats or strange rounding functions:
Mine was very similar. And since so many people have already posted code, I might as well follow suit.
One solution was originally in C and one in C++. No reason to get all huffy.hey pal listen to scribbler
edit: Not to mention I wasn't attempting to give him a solution for his first post. Just show him a similar solution where the algorithm was important, not the implementation. That's why my first post said this:
Here is a program that calculates the amount of change in a given sum. I believe this might point you in the right direction for what you are doing.
Last edited by andyhunter; 01-17-2005 at 04:57 PM.
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem