Last active
November 4, 2017 04:12
-
-
Save ryo1kato/4a1ca98cfdc2596b73a19cef50525265 to your computer and use it in GitHub Desktop.
Kei's lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This is lab2 the vending machine creat by Kei Kato. | |
#include <iostream> | |
#include <iomanip> | |
#include <cstdlib> | |
#define PLURAL(variable, word) \ | |
variable << word << (variable > 1 ? "s" : "") | |
using namespace std; | |
int ChooseSnack(int numPotatoChips, int numCookies, int numCandies); | |
int PutCoins(string kindOfCoin); | |
int calTotal(int numQuaters, int numDimes, int numNickels); | |
int main(int argc, char** argv){ | |
const int pricePotatoChips = 125, | |
priceCookies = 85, | |
priceCandies = 95; | |
if (argc != 4) { | |
cerr << "ERROR: wrong number of arguments. Usage: " << argv[0] | |
<< " NUM_POTATOCHIPOS NUM_COOKIES NUM_CANDIES" | |
<< endl; | |
return 1; | |
} | |
int numPotatoChips = atoi(argv[1]); | |
int numCookies = atoi(argv[2]); | |
int numCandies = atoi(argv[3]); | |
if (numPotatoChips < 0 || numCookies < 0 || numCandies < 0) { | |
cerr << "ERROR: Invalid number or non-numeric arguments: ['" | |
<< argv[1] << "', '" | |
<< argv[2] << "', '" | |
<< argv[3] << "'] " | |
<< endl; | |
return 1; | |
} | |
int customerChoice = 0; | |
int numQuaters, numDimes, numNickels, | |
numChangeQuaters, numChangeDimes, numChangeNickels; | |
int price, total = 0, numSnack, change; | |
//cout << numPotatoChips << numCookies << numCandies << endl; | |
while (customerChoice != 4){ | |
//reset numbers | |
numQuaters = 0; | |
numDimes = 0; | |
numNickels = 0; | |
numChangeQuaters = 0; | |
numChangeDimes = 0; | |
numChangeNickels = 0; | |
price = 0; | |
total = 0; | |
numSnack = 0; | |
change = 0; | |
// choose a snack | |
customerChoice = ChooseSnack(numPotatoChips, numCookies, numCandies); | |
switch (customerChoice){ | |
case 1 : | |
price = pricePotatoChips; | |
numSnack = numPotatoChips; | |
numPotatoChips--; | |
break; | |
case 2 : | |
price = priceCookies; | |
numSnack = numCookies; | |
numCookies--; | |
break; | |
case 3 : | |
price = priceCandies; | |
numSnack = numCandies; | |
numCandies--; | |
break; | |
case 4 : | |
cout << "Thank you!" << endl; | |
break; | |
default: | |
cerr << "ERROR: unexpected error. Abort." << endl; | |
return 1; | |
} | |
if(customerChoice == 4){ | |
break; | |
} | |
if(numSnack == 0){ | |
cout << "Sold out, please choose other snack." << endl; | |
customerChoice = 0; | |
} | |
else{ | |
//put coins | |
while(total < price){ | |
cout << "Please put coins." << endl; | |
numQuaters = PutCoins("quaters"); | |
numDimes = PutCoins("dimes"); | |
numNickels = PutCoins("nickels"); | |
total += calTotal(numQuaters, numDimes, numNickels); | |
if(total < price){ | |
cout << price-total << " cnets more, please." << endl; | |
} | |
} | |
if(total > price){ | |
//calculte change | |
change = total-price; | |
numChangeQuaters = change/25; | |
change = change%25; | |
numChangeDimes = change/10; | |
change = change%10; | |
numChangeNickels = change/5; | |
cout << "Please take youa snack and change:" << endl; | |
cout << PLURAL(numChangeQuaters, " quater") << endl; | |
cout << PLURAL(numChangeDimes, " dime") << endl; | |
cout << PLURAL(numChangeNickels, " nickel") << endl; | |
cout << "Thank you!" << endl; | |
} | |
else{ | |
cout << "Please take your snack! Thank you!" << endl; | |
} | |
} | |
} | |
return 0; | |
} | |
int ChooseSnack(int numPotatoChips, int numCookies, int numCandies){ | |
int customerChoice; | |
while (1) { | |
cin.clear(); | |
cin.sync(); | |
cout << endl << "\t\t *** Please choose a snack from menu ***" << endl; | |
cout << "1. Potato Chips $1.25\t" << PLURAL(numPotatoChips, " pack") << " available" << endl; | |
cout << "2. Cookies $0.85\t" << PLURAL(numCookies, " pack") << " available" << endl; | |
cout << "3. Candies $0.95\t" << PLURAL(numCandies, " pack") << " available" << endl; | |
cout << "4. QUIT" << endl; | |
cout << "Input: "; | |
cin >> customerChoice; | |
if (cin.eof()) { | |
return 4; //better error handling? | |
} | |
if (cin.good()) { | |
if(customerChoice < 1 || customerChoice > 4){ | |
cout << "Please type number 1 to 4." << endl; | |
} | |
else { | |
return customerChoice; | |
} | |
} else { | |
cout << "Error reading input. Please type number 1 to 4." << endl; | |
cin.clear(); //clear the error | |
cin.ignore(INT_MAX, '\n'); //clear the invalid input | |
} | |
} | |
} | |
int PutCoins(string kindOfCoin){ | |
usigned int numCoins; | |
while(1){ | |
cout << "How many " << kindOfCoin << " ? "; | |
cin >> numCoins; | |
if (cin.eof()) { | |
return 0; //FIXME: better aborting the program here. | |
} | |
if (cin.good()) { | |
return numCoins; | |
} else { | |
cout << "Error reading input. Please input a number." << endl; | |
cin.clear(); | |
cin.ignore(INT_MAX, '\n'); | |
} | |
} | |
} | |
int calTotal(int numQuaters, int numDimes, int numNickels){ | |
int total; | |
total = (numQuaters*25) + (numDimes*10) + (numNickels*5); | |
return total; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment