Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. // CS 1A Lab 4
  2. // Student: Adam Creeger
  3. // Student ID: 20160381
  4. #include <iostream>
  5. #include <string>
  6. #include <sstream>
  7. using namespace std;
  8. // main client --------------------------------------------------------
  9. int main()
  10. {
  11. int stamps = 0;
  12. int userInYogurt = 0;
  13. string userInReward;
  14. string userInStr;
  15. string bufferString;
  16. //Main Loop
  17. while (true) {
  18. //Menu Loop
  19. while (cout << "In order to make a purchase: Press 'P'. To shut down: Press 'S'." << endl &&
  20. getline(cin, bufferString)) {
  21. istringstream iss{ bufferString };
  22. if ((iss >> userInStr)) {
  23. if ((userInStr.substr(0, 1) == "P") || (userInStr.substr(0, 1) == "p")) {
  24. break;
  25. }
  26. else if ((userInStr.substr(0, 1) == "S") || (userInStr.substr(0, 1) == "s")) {
  27. cout << "Program terminated by user." << endl;
  28. return 0;
  29. }
  30. }
  31. }
  32. //Reward purchase
  33. if (stamps >= 10) {
  34. cout << "You have " << stamps << " reward stamps!" << endl;
  35. cout << "You can redeem a yogurt for 10 stamps. Would you like to accept? Y/N?" << endl;
  36. getline(cin, userInReward);
  37. cout << "You received " << stamps / 10 << " yogurts!" << endl;
  38.  
  39. if ((userInReward.substr(0, 1) == "Y") ^ (userInReward.substr(0, 1) == "y")) {
  40. cout << "Enjoy your free yogurt!" << endl;
  41. stamps -= 10;
  42. }
  43. else
  44. while ((cout << "How many yogurts would you like to purchase?" << endl)
  45. && getline(cin, bufferString))
  46. {
  47. istringstream iss{ bufferString };
  48. if (iss >> userInYogurt) {
  49. if (userInYogurt > 0 && userInYogurt < 1000) {
  50. //Adjust stamp counter
  51. stamps += userInYogurt;
  52. cout << "You purchased " << userInYogurt << " yogurts!" << endl;
  53. cout << "You have " << stamps << " yogurt stamps!" << endl;
  54. break;
  55. }
  56. cout << "ERROR: Character input not valid." << endl;
  57. }
  58. }
  59. }
  60. //Normal purchase
  61. else if (stamps < 10) {
  62. while ((cout << "How many yogurts would you like to purchase?" << endl)
  63. && getline(cin, bufferString))
  64. {
  65. istringstream iss{ bufferString };
  66. if (iss >> userInYogurt) {
  67. if (userInYogurt > 0 && userInYogurt < 1000) {
  68. //Adjust stamp counter
  69. stamps += userInYogurt;
  70. cout << "You purchased " << userInYogurt << " yogurts!" << endl;
  71. cout << "You have " << stamps << " yogurt stamps!" << endl;
  72. break;
  73. }
  74. cout << "ERROR: Character input not valid." << endl;
  75. }
  76. }
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement