From: J H. <jsh...@ho...> - 2009-03-16 01:46:58
|
So, after a day of experimenting, I found out how to write a function allowing me to double pennies. Like in that old saying would you rather have a million dollars or a penny doubled every day for a month? I would like to know if anyone could suggest a better/ more efficient way to accomplish what the program does. I will work on using the loop in the function itself so as not to call the pdt function every time, but for a newbie I wouldn't mind suggestions at this point. Thanks, 42 #include <cmath> #include <iostream> #include <string> using namespace std; string next; int pdt(int); int prevd; double long long prevt = 1; int main(void) { do { prevd = 1; cout << "How many days?" ; int number; cin >> number; cout << "For being good the tooth fairy doubles your pennies \n"; for (int y=1;y!=number;++y) { cout << "after " << y << " nights, you wake to find "; cout << pdt(prevd) << " pennies for a total of "; prevt += prevd; cout << "$" << prevt/100 << "\n" ; } prevt=1; cout << "Again?"; cin >> next;} while (next != "no"); } int pdt(int temp ) { prevd = temp * 2; return prevd; } |