codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
//============================================================================ // Name : Comp.cpp // Author : // Version : // Copyright : // Description : //============================================================================ #include <iostream> using namespace std; void userchoice(void); //function "userchoice" it prints out the list of users options void coinsinput(int&,int&,int&,int&,int&,double&); //function "coinsinput" where we receive the deposit amount for each type of coin double withdrawamount(double);//function "withdrawamount" where we receive and validate the amount that the user wants to withdraw void withdrawdetails(int&,int&,int&,int&,int&,double&,double); //function "withdrawdetails" where we validate if the transaction can occur //and letting the user know how much coins of each type he will receive as well as the new piggybank money amount void showtotal(double,int);// function "showtotal" where we let the user know the current amount of money and coins in the piggybank void emptybank(int&,int&,int&,int&,int&,double&);// function "emptybank" where the user receive all money/coins from the piggybank int main() { // declaring variables char choice; int totalcoins=0,fivecent=0,tencent=0,fiftycent=0,oneeuro=0; double totalmoney=0,withdraw; cout<<"Welcome to George's PiggyBank"<<endl; //greeting message do { userchoice(); // calling the function user choice cin>>choice; // reading/receiving the users choice //checking if the users choice is valid, if its not we ask him to re-enter until he get it right while ((choice!='d') && (choice!='w') && (choice!='s') && (choice!='e') && (choice!='q')) { cout<<"Invalid choice, please re-enter: "; cin>>choice; } if (choice=='d') //if the choice is 'd' (deposit) coinsinput(fivecent,tencent,fiftycent,oneeuro,totalcoins,totalmoney); // we call the function "coinsinput" so we can receive the deposit amount for each type of coin if (choice=='w') // if the choice is 'w' (withdraw) { withdraw=withdrawamount(totalmoney); // we call the function "withdrawamount" where we receive and validate the amount that the user wants to withdraw withdrawdetails(fivecent,tencent,fiftycent,oneeuro,totalcoins,totalmoney,withdraw); //therefore we call the function "withdrawdetails" where we validate if the transaction can occur //and letting the user know how much coins of each type he will receive as well as the new piggybank money amount } if (choice=='s')// if the choice is 's' (show total) showtotal(totalmoney,totalcoins); //we call the function "showtotal" which prints on the screen the exact amount of money and coins //that are in the piggybank right now if (choice=='e')// if the choice is 'e' (empty piggybank) emptybank(fivecent,tencent,fiftycent,oneeuro,totalcoins,totalmoney); //we call the function "emptybank" which prints on the screen the exact amount of money //that the user will receive as well as the amount of each type of coin he will receive //finally we give each variable a value of 0, and thats because the user emptied the Piggybank cout<<endl; } while (choice!='q'); //once the user enter the choice 'q'(quit) the program terminates cout << "Goodbye!"; // goodbye message return 0; } void userchoice() { // after calling the function "userchoice" it will print on the screen the menu of the users options cout<<endl; cout<<"Menu Menu Menu"<<endl; cout<<"=================="<<endl; cout<<"d: Deposit"<<endl; cout<<"w: Withdraw"<<endl; cout<<"s: Show total"<<endl; cout<<"e: Empty"<<endl; cout<<"q: Quit"<<endl; cout<<"Please enter choice: "; } void coinsinput(int& totalfivecoins,int& totaltencoins,int& totalfiftycoins,int& totaleurocoins,int& coins, double& money) { int fivecoins,tencoins,fiftycoins,onecoins; if (coins==200) // if piggybank is already full we tell the user that its full he cant deposit anything else cout<<"PiggyBank is full, you can't deposit anything else"<<endl; //after that the current function terminates else { cout<<"Number of 5 cents: "; //asking the user to give the amount of 5 cent coins he wants to deposit cin>>fivecoins; while (fivecoins<0) // checking if the amount is greater or equal to 0 { // if its not we keep asking him to re-enter until he gets it right cout<<"Invalid input, please re-enter: "; cin>>fivecoins; } while ((coins+fivecoins)>200) { //if the amount of coins in the piggybank right now plus the amount of coins the user wants to deposit cout<<"Not enough space for so much coins"<<endl; // is greater than 200 we tell the user that there isnt cout<<"You can deposit "<<200-coins<<" more coins only"<<endl; // enough space in the piggybank and ask him to re-enter do //also we are checking if the new amount given is greater or equal to 0, { //if its not we ask the user to re-enter cout<<"Please re-enter the amount of 5 cents you want to deposit: "; cin>>fivecoins; } while (fivecoins<0); } totalfivecoins+=fivecoins; //after the validation we summarize the new amount of 5 cent coins coins+=fivecoins; // also we summarize the new amount of the total piggybank's coins money+=(fivecoins*0.05); // and calculating the new money amount in the piggybank //------------------------------------------------ cout<<"Number of 10 cents: ";//asking the user to give the amount of 10 cent coins he wants to deposit cin>>tencoins; while (tencoins<0) //checking if the amount is greater or equal to 0 { // if its not we keep asking him to re-enter until he gets it right cout<<"Invalid input, please re-enter: "; cin>>tencoins; } while ((coins+tencoins)>200) { //if the amount of coins in the piggybank right now plus the amount of coins the user wants to deposit cout<<"Not enough space for so much coins"<<endl;// is greater than 200 we tell the user that there isnt cout<<"You can deposit "<<200-coins<<" more coins only"<<endl;// enough space in the piggybank and ask him to re-enter do //also we are checking if the new amount given is greater or equal to 0, { //if its not we ask the user to re-enter cout<<"Please re-enter the amount of 10 cents you want to deposit: "; cin>>tencoins; } while (tencoins<0); } totaltencoins+=tencoins;//after the validation we summarize the new amount of 10 cent coins coins+=tencoins;// also we summarize the new amount of the total piggybank's coins money+=(tencoins*0.10);// and calculating the new money amount in the piggybank //-------------------------------------------------- cout<<"Number of 50 cents: ";//asking the user to give the amount of 50 cent coins he wants to deposit cin>>fiftycoins; while (fiftycoins<0) //checking if the amount is greater or equal to 0 { // if its not we keep asking him to re-enter until he gets it right cout<<"Invalid input, please re-enter: "; cin>>fiftycoins; } while ((coins+fiftycoins)>200) { //if the amount of coins in the piggybank right now plus the amount of coins the user wants to deposit cout<<"Not enough space for so much coins" <<endl;// is greater than 200 we tell the user that there isnt cout<<"You can deposit "<<200-coins<<" more coins only"<<endl;// enough space in the piggybank and ask him to re-enter do //also we are checking if the new amount given is greater or equal to 0, { //if its not we ask the user to re-enter cout<<"Please re-enter the amount of 50 cents you want to deposit: "; cin>>fiftycoins; } while (fiftycoins<0); } totalfiftycoins+=fiftycoins;//after the validation we summarize the new amount of 50 cent coins coins+=fiftycoins;// also we summarize the new amount of the total piggybank's coins money+=(fiftycoins*0.50);// and calculating the new money amount in the piggybank //-------------------------------------------------- cout<<"Number of 1 euro: ";//asking the user to give the amount of 1 euro coins he wants to deposit cin>>onecoins; while (onecoins<0) //checking if the amount is greater or equal to 0 { // if its not we keep asking him to re-enter until he gets it right cout<<"Invalid input, please re-enter: "; cin>>onecoins; } while ((coins+onecoins)>200) { //if the amount of coins in the piggybank right now plus the amount of coins the user wants to deposit cout<<"Not enough space for so much coins"<<endl;// is greater than 200 we tell the user that there isnt cout<<"You can deposit "<<200-coins<<" more coins only"<<endl;// enough space in the piggybank and ask him to re-enter do //also we are checking if the new amount given is greater or equal to 0, { //if its not we ask the user to re-enter cout<<"Please re-enter the amount of 1 euro coins you want to deposit: "; cin>>onecoins; } while (onecoins<0); } totaleurocoins+=onecoins;//after the validation we summarize the new amount of 1 euro coins coins+=onecoins;// also we summarize the new amount of the total piggybank's coins money+=onecoins;// and calculating the new money amount in the piggybank } cout<<"Piggybank has "<<money<<" euros"; // finally we let the user know the current total money amount in the piggybank } double withdrawamount(double totalmoney) { double amount; cout<<"How much would you like to withdraw: "; //asking the user to enter the amount of money he want to withdraw cin>>amount; // reading the amount given by the user while ((amount<0) || (amount>totalmoney)) // as long as the amount is below 0 of greater than the total money { // in the piggybank, we tell him that the amount given is invalid cout<<"Invalid input, please re-enter: "; //and we ask him re-enter a new amount cin>>amount; } return amount; // sending the amount the user would like to withdraw, back to the main program } void withdrawdetails (int& totalfivecoins,int& totaltencoins,int& totalfiftycoins,int& totaleurocoins,int& coins, double& money,double withdrawal) { int euro=0,fifty=0,ten=0,five=0,valid; int valideuro,validfifty,validten,validfive; double validwithdrawal; validwithdrawal=withdrawal; valideuro=totaleurocoins; validfifty=totalfiftycoins; validten=totaltencoins; validfive=totalfivecoins; if ((withdrawal>=1) && (valideuro>0)) //withdrawing the euro coins { if (int(withdrawal)<= valideuro) { euro=int(withdrawal); valideuro-=int(withdrawal); withdrawal-=euro; } if (int(withdrawal)> valideuro) { euro=valideuro; valideuro=0; withdrawal-=euro; } } else euro=0; while ((withdrawal>=0.50) && (validfifty>0)) { fifty+=1; validfifty-=1; withdrawal-=0.50; } while ((withdrawal>=0.10) && (validten>0)) { ten+=1; validten-=1; withdrawal-=0.10; } while ((withdrawal>=0.05) && (validfive>0)) { five+=1; validfive-=1; withdrawal-=0.05; } //--------------------------------------------------------- if (withdrawal==0) valid=1; if (withdrawal!=0) valid=0; //---------------------------------------------------------- if (valid==1) { totaleurocoins=valideuro; totalfiftycoins=validfifty; totaltencoins=validten; totalfivecoins=validfive; money-=validwithdrawal; cout<<endl; cout<<"You will get: "; if ((euro==0) && (fifty==0) && (ten==0) && (five==0)) cout<<"'0' Coins"; if (euro>0) cout<<euro<<" 'Euro Coins' "; if ((euro!=0) && (fifty>0) && (ten==0) && (five==0)) cout<<" and "<<fifty<<" '50 Cent' Coins "; else if (fifty!=0) cout<<fifty<<" '50 Cent' Coins "; if ((ten!=0) && (five==0)) cout<<" and "<<ten<<" '10 Cent' Coins "; if ((ten!=0) && (five>0)) cout<<ten<<" '10 Cent' Coins "; if ((five!=0) && (euro==0) && (fifty==0) && (ten==0)) cout<<five<<" '5 Cent' Coins"; if ((five!=0) && ((euro!=0) || (fifty!=0) || (ten!=0))) cout<<" and "<<five<<" '5 Cent' Coins "; cout<<endl; cout<<"Piggybank has "<<money<<" Euros"<<endl; } else cout<<"The transaction cannot occur"<<endl; } void showtotal(double money, int coins) { cout<<"The current amount in the Piggybank is "<<money<<endl; //showing the user the amount of money in the piggybank cout<<"The amount of coins in the Piggybank is "<<coins<<endl; //showing the user the amount of coins in the piggybank } void emptybank(int& five, int& ten, int& fifty, int& euro,int& coins, double& money) { cout<<"You will receive "<<money<< " euro"<<endl; //telling the user the amount of money he will receive cout<<"1 euro coins: "<<euro<<" coins"<<endl; //also telling the user the amount of each coin type he will receive cout<<"50 cent coins: "<<fifty<<" coins"<<endl; cout<<"10 cent coins: "<<ten<<" coins"<<endl; cout<<"5 cent coins: "<<coins<<" coins"<<endl; // since the user emptied the piggybank there isnt any more money/coins left five=0; ten=0; fifty=0; euro=0; money=0; coins=0; }
Private
[
?
]
Run code
Submit