Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CS 1A Lab 4
- // Student: Adam Creeger
- // Student ID: 20160381
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- // main client --------------------------------------------------------
- int main()
- {
- int stamps = 0;
- int userInYogurt = 0;
- string userInReward;
- string userInStr;
- string bufferString;
- //Main Loop
- while (true) {
- //Menu Loop
- while (cout << "In order to make a purchase: Press 'P'. To shut down: Press 'S'." << endl &&
- getline(cin, bufferString)) {
- istringstream iss{ bufferString };
- if ((iss >> userInStr)) {
- if ((userInStr.substr(0, 1) == "P") || (userInStr.substr(0, 1) == "p")) {
- break;
- }
- else if ((userInStr.substr(0, 1) == "S") || (userInStr.substr(0, 1) == "s")) {
- cout << "Program terminated by user." << endl;
- return 0;
- }
- }
- }
- //Reward purchase
- if (stamps >= 10) {
- cout << "You have " << stamps << " reward stamps!" << endl;
- cout << "You can redeem a yogurt for 10 stamps. Would you like to accept? Y/N?" << endl;
- getline(cin, userInReward);
- cout << "You received " << stamps / 10 << " yogurts!" << endl;
- if ((userInReward.substr(0, 1) == "Y") ^ (userInReward.substr(0, 1) == "y")) {
- cout << "Enjoy your free yogurt!" << endl;
- stamps -= 10;
- }
- else
- while ((cout << "How many yogurts would you like to purchase?" << endl)
- && getline(cin, bufferString))
- {
- istringstream iss{ bufferString };
- if (iss >> userInYogurt) {
- if (userInYogurt > 0 && userInYogurt < 1000) {
- //Adjust stamp counter
- stamps += userInYogurt;
- cout << "You purchased " << userInYogurt << " yogurts!" << endl;
- cout << "You have " << stamps << " yogurt stamps!" << endl;
- break;
- }
- cout << "ERROR: Character input not valid." << endl;
- }
- }
- }
- //Normal purchase
- else if (stamps < 10) {
- while ((cout << "How many yogurts would you like to purchase?" << endl)
- && getline(cin, bufferString))
- {
- istringstream iss{ bufferString };
- if (iss >> userInYogurt) {
- if (userInYogurt > 0 && userInYogurt < 1000) {
- //Adjust stamp counter
- stamps += userInYogurt;
- cout << "You purchased " << userInYogurt << " yogurts!" << endl;
- cout << "You have " << stamps << " yogurt stamps!" << endl;
- break;
- }
- cout << "ERROR: Character input not valid." << endl;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement