Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ 1. A Bookseller has a book club that awards points to its customers based on

ID: 3757939 • Letter: C

Question

C++

1. A Bookseller has a book club that awards points to its customers based on the numbers of books purchased each month. The points awarded are as follows: If a customer purchases 0 books, he or she earns 0 points. If a customer purchases 1 book, he or she earns 5 points. If a customer purchases 2 books, he or she earns 15 points. If a customer purchases 3 books, he or she earns 30 points. If a customer purchases 4 or more books, he or she earns 60 points. Write a program that asks the user to enter the number of books that he or she has purchased this month and then displays the number of points awarded. Print an error (using exceptions: try and catch) and exit if the user enters an invalid amount. 1)When the program begins, ask the user: How many books have you purchased this month? 2)If the user enters an invalid number, print the following message and exit. Invalid entry! Goodbye! 3)If the user enters a valid number, print the following message and exit. Points earned:

2. Write a program to reverse the digits of a positive integer number. For example, if the number 8735 is entered, the number displayed should be 5378. Criteria for a correct solution: 1)must work for any positive integer. 2)keep asking the user for input if input is negative. 3)user can enter unlimited bad input.

Explanation / Answer

#include <iostream>

using namespace std;

int main() {

   int noOfbooks;
   cout << "How many books have you purchased this month?" <<endl;
   try
      {
       cin >> noOfbooks;
       if(cin.fail())
           throw 99;
       //invalid entry
       if(noOfbooks < 0)
           throw 100;

       //valid entry

/*       If a customer purchases 0 books, he or she earns 0 points.
       If a customer purchases 1 book, he or she earns 5 points.
       If a customer purchases 2 books, he or she earns 15 points.
       If a customer purchases 3 books, he or she earns 30 points.
       If a customer purchases 4 or more books, he or she earns 60 points
*/
       int points = 0; // 0 books

       if(noOfbooks == 1)
           points = 5;
       else if(noOfbooks == 2)
           points = 15;
       else if(noOfbooks == 3)
           points = 30;
       else if(noOfbooks >=4)
           points = 60;

       cout << "Points earned: " <<points <<endl;
      }
      catch (int e)
      {
        cout << "Invalid entry! Goodbye!" << endl;
      }

}

----------output------------------

How many books have you purchased this month?
dfsd
Invalid entry! Goodbye!

How many books have you purchased this month?
50
Points earned: 60

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

#include <iostream>

using namespace std;

int main() {
   int num;
   do {
       cout <<"Enter a positive number: " <<endl;
       cin >> num;
   } while(num <= 0);

   int temp = num;
   string reverse = "";

   int rem;

   while(temp > 0) {
       rem = temp % 10;
       temp = temp /10;
       stringstream sstm;
       sstm << rem;

       reverse = reverse+sstm.str();

   }

   cout << "Number: " << num << " Reverse number: " << reverse <<endl;
}

Enter a positive number:
123
Number: 123 Reverse number: 321