Thread: Need help understanding the program solution for a book exercise

  1. #1
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33

    Need help understanding the program solution for a book exercise

    The exercise ask to make a calculator program to perform computations in a separate function for each type of computation.

    Here is what my solution looks like and it works perfectly fine.
    Code:
    #include <iostream>
    
    using namespace std;
    
    // prototypes 
    int add(int x, int y);
    int sub(int x, int y);
    int mult(int x, int y);
    int quo(int x, int y);
    
    
    int main()
    {
      int userChoice;
      int num1, num2;
    
      do {
        cout << "Which operation would you like to perform: \n";
        cout << "1. Add\n";
        cout << "2. Subtract\n";
        cout << "3. Multiply\n";
        cout << "4. Divide \n";
        cout << "5. Exit menu\n";
        cin >> userChoice;
    
        switch (userChoice) {
        case 1:
          cout << "Choose the first number you would like to add: \n";
          cin >> num1;
          cout << "Choose the second number you would like to add: \n";
          cin >> num2;
          cout << "The sum of the numbers is: " << add(num1, num2) << '\n';
          break;
        case 2:
          cout << "Pick the first number you would like to subtract: \n";
          cin >> num1;
          cout << "Choose the second number you would like to subtract: \n";
          cin >> num2;
          cout << "The difrence between the two numbers is: " << sub(num1, num2) << '\n';
          break;
        case 3:
          cout << "Choose the first nummber you would like to multiply: " << '\n';
          cin >> num1;
          cout << "Choose the second nummber you would like to multiply: " << '\n';
          cin >> num2;
          cout << "The product of the two numbers is: " << mult(num1, num2) << '\n';
          break;
        case 4:
          cout << "Choose the first nummber you would like to divide: " << '\n';
          cin >> num1;
          cout << "Choose the second nummber you would like to divide: " << '\n';
          cin >> num2;
          cout << "The quotient of the two numbers is: " << quo(num1, num2) << '\n';
          break;
        default:
          cout << "Exiting program...cya! \n";
          break;
        }
      } while (userChoice != 5);// exits menu 
    
      system("pause");
      return 0;
    }
    
    // returns sum
    int add(int x, int y) {
      return x + y;
    }
    
    // returns the difference 
    int sub(int x, int y) {
      return x - y;
    }
    
    // returns the product
    int mult(int x, int y) {
      return x * y;
    }
    
    // returns the quotient
    int quo(int x, int y) {
      return x / y;
    }
    The solution in the book looks like this:
    Code:
    #include <iostream>
    
    int multiply(int x, int y)
    {
        return x*y;
    }
    
    int divide(int x, int y)
    {
         return x/y;
    }
    
    int add(int x, int y) 
    {
        return x+y;
    }
    
    int subtract(int x, int y)
    {
        return x-y;
    }
    
    
    using namespace std;
    
    int main()
    {
        char op='c';
        int x, y;
        
        while(op!='e')
        {
            cout<<"What operation would you like to perform: add(+), subtract(-), divide(/), multiply(*), [e]xit?";cin>>op;
            switch(op)
            {
                case '+':
                cin>>x;
                cin>>y;
                cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
                break;
    
                case '-':
                cin>>x;
                cin>>y;
                cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
                break;
                
                case '/':
                cin>>x;
                cin>>y;
                
                cout<<x<<"/"<<y<<"="<<divide(x,y)<<endl;
                break;
                case '*':
                cin>>x;
                cin>>y;
                cout<<x<<"*"<<y<<"="<<multiply(x,y)<<endl;
                break;
                
                case 'e':
                return 0;
                
                default:
                cout<<"Sorry, try again"<<endl;
            }
        }
        return 0;
    }
    What I want to know is why does the solution use
    Code:
    char op = 'c';
    I ran the solution with
    Code:
     char op;
    and it still worked perfectly fine. Why is that and why do they use 'c'?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    So that
    while(op!='e')
    Doesn't compare with a random uninitialized value which might randomly be e.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33
    Thanks for the clarification! I have one more question. Would you say the book solution is better than the solution I came up with? Is there anything I could have done to make my solution better? Sorry, if what I'm asking is a dumb question.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Perhaps you could add another function to replace the code you copy and pasted.

    Void prompt ( int &x, int &y, string prompt)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R Exercise 1.13 Solution - Seeking Constructive Criticism
    By Evan D Williams in forum C Programming
    Replies: 11
    Last Post: 02-20-2018, 06:41 PM
  2. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  3. Replies: 2
    Last Post: 02-16-2012, 03:03 PM
  4. Understanding the word count program from K&R book
    By alter.ego in forum C Programming
    Replies: 11
    Last Post: 06-13-2011, 04:55 AM
  5. Request for feedback -- exercise solution.
    By msh in forum C Programming
    Replies: 4
    Last Post: 07-07-2010, 12:48 PM

Tags for this Thread