• Welcome to Valhalla Legends Archive.
 

Problem with my code

Started by Mitosis, April 22, 2004, 06:22 PM

Previous topic - Next topic

K

#15
I think it's generally accepted that you use arrays of a type when they're assosciated values, not just when you have more than one variable of a certain type.  That's why you can name variables.

Here's how I'd do it.  If you wanted to make it easier to add or modify, I'd use an array of char's for each operator and look it up by index, as well as using a function pointer to choose the correct operation, but that would be a little more hardcore than you need.


#include <iostream>
#include <cmath>
// this is a small program and there are no
// naming conflicts. using the whole namespace
// would be ok.

using std::fmod;
using std::cout;
using std::cin;
using std::endl;

void show_commands();
void get_input(float& a, float& b);

enum Operation
{
   OP_Addition = 1, OP_Subtraction,
   OP_Multiplication, OP_Division,
   OP_Modulus, OP_Quit
};

int main()
{
   int nChoice;   // the user's choice of operations.
   float fVar1, fVar2; // the two operands

   cout << "Simple calculator." << endl;
   show_commands();

   do
   {
      cout << "Choice: ";
      cin >> nChoice;

      if ((nChoice >= OP_Addition) && (nChoice < OP_Quit))
         get_input(fVar1, fVar2);

      switch(nChoice)
      {
      case OP_Addition:
         cout << fVar1 << '+' << fVar2 << '=' << (fVar1 + fVar2) << endl;
         break;

      case OP_Subtraction:
         cout << fVar1 << '-' << fVar2 << '=' << (fVar1 - fVar2) << endl;
         break;

      case OP_Multiplication:
         cout << fVar1 << '*' << fVar2 << '=' << (fVar1 * fVar2) << endl;
         break;

      case OP_Division:
         if (fVar2 != 0.0f)
            cout << fVar1 << '/' << fVar2 << '=' << (fVar1 / fVar2) << endl;
         else
            cout << "Divide by zero." << endl;

         break;

      case OP_Modulus:
         cout << fVar1 << '%' << fVar2 << '=' << fmod(fVar1, fVar2) << endl;
         break;

      case OP_Quit:
         break;

      default:
         cout << "Bad choice!" << endl;
         show_commands();
         break;
      }

   }while(nChoice != OP_Quit);

   return 0;
}



void show_commands()
{
   cout << "[1] Addition" << endl
       << "[2] Subtraction" << endl
       << "[3] Multiplication" << endl
       << "[4] Division" << endl
       << "[5] Modulus" << endl
       << "[6] Exit Program" << endl;
}

void get_input(float& a, float& b)
{
   cout << "First operand: ";
   cin >> a;
   cout << "Second operand: ";
   cin >> b;
}

Mephisto

#16
What I would have done if I was going to make a calculator I would later expand would be to create a calculator class and add member functions for each operation taking place.  Maybe even use template functions to pass into the function either floats or integers.  This would be like K said above a bit too hardcore for what you need, but it's just an idea after you get these concepts down.

Again, apologies for using recursion in main(), I had no idea that memory consumption applied to recursing main(), it just didn't occur to me...And as iago puts it, "just because you CAN do it, doesn't mean you SHOULD!"