C++ Learning Community Forum
August 01, 2010, 03:30:17 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Calculator - first complete program on my own.  (Read 688 times)
prometheus
semi-N00b
**
Posts: 39


View Profile
« on: August 24, 2009, 05:00:54 AM »

I know it is not that great, just a simple calculator. I would be interested in accolades and criticisms.

two things which I couldn't figure out.
 
one: is the limited amount of precision only 7 didgits - from what i could find this is a limitation of the computer. but isn't there away around this? I thought
Code:
cout << setprecision(15);
woud do this but it doesn't seem to.

two: when the user puts in more than one character in the final exit loop,  the loop checks all the characters going through the loop for each character. How can you get it to just check the first character and discard the rest?

Code:

#include <iostream>
#include <limits>
#include <iomanip>

using namespace std;

//***************Function Declarations********************

                float fadd (float x, float y);
                float fsub (float x, float y);
                float fmul (float x, float y);
                float fdiv (float x, float y);
               
//**************Function Definitions************************
               
          float fadd (float x, float y)
          {
               float total;
               total = x + y;
               return total;
          }     
         
          float fsub (float x, float y)
          {
               float total;
               total = x - y;
               return total;
          }
         
          float fmul (float x, float y)
          {
               float total;
               total = x * y;
               return total;
          }
         
          float fdiv (float x, float y)
          {
               float total;
               total = x / y;
               return total;
          }         
 
//*****************Start Main Function********************               

int main()
{

     cout << ".........................................\n";
     cout << ".                                       .\n";
     cout << ".          Simple Calculator            .\n";
     cout << ".            by Prometheus              .\n";
     cout << ".            copyright 2009             .\n";
     cout << ". General Public Non-Commercial License .\n";
     cout << ".                                       .\n";
     cout << ".........................................\n\n";
     cout << "To end Program at any time, type <ctrl>-c \n";
     cout << "This calculator is only significant upto 7 digits \n\n\n";
     cout << "Begin by entering the first number or \nthe complete expression in the form: 5+5 \n\n";
     
     
    char exit = 'n';
    cout << setprecision(15);
     
//****************primary while loop to determine exit condition*************
   
    while(exit!='x' && exit!='X')
    {
                       
          float x, y, z, total;
          char op;
          exit = 'n';
           
//***************secondary while loop to determine new or continue status******
         
     while (exit == 'n' || exit == 'c')     
      {   
         
          if (exit == 'n')//If it is a new calculation do this
          {
          cout << "\nfirst number: ";
         
          while(!(cin >> x))
              {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "\nenter a valid number please: ";
              }
             
         
                                   
          cout << "\noperation: ";
          cin >> op;
         
          while (((op != '+') && (op != '-') && (op != '*') && (op != '/')))
             {
               
               cout << "I don't understand your operation, please try again: ";
               cin >> op;
               
              }
               
          cout << "\nsecond number: ";
          while(!(cin >> y))
              {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "\nenter a valid number please: ";
              }
          }//close brace - if exit is n
         
         
          if (exit == 'c')//If it is a continueing Calculation do this
          {
          cout << "\nfirst number: " << z;
         
          cout << "\n\noperation: ";
          cin >> op;
          while (((op != '+') && (op != '-') && (op != '*') && (op != '/')))
             {
               
               cout << "I don't understand your operation, please try again: ";
               cin >> op;
               
              }
             
             
          cout << "\nsecond number: ";
          while(!(cin >> y))
              {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
                cout << "\nenter a valid number please: ";
              }
          }//close brace - if exit is c
         
                       
          switch (op)//determines which function to call depending on the operation sign
             {
                 case '+':
                      if (exit == 'n')
                      total = fadd (x, y);
                      else
                      total = fadd (z, y);
                      break;
                 case '-':
                      if (exit == 'n')
                      total = fsub (x, y);
                      else
                      total = fsub (z, y);     
                      break;
                 case '*':
                      if (exit == 'n')
                      total = fmul (x, y);
                      else
                      total = fmul (z, y);
                      break;
                 case '/':
                      if (exit == 'n')
                      total = fdiv (x, y);
                      else
                      total = fdiv (z, y);
                      break;
                 default:
                      break;
              }
 
 
//********************Output*************************************
                                             
          if (exit == 'n')
          cout << " \n\n " << x << " " << op << " " << y << " = " << total << endl;
          else
          cout << " \n\n " << z << " " << op << " " << y << " = " << total << endl;
         
          cout << " \n\n ";
          cout << "\nRunning Total: " << total << " \n\n " << endl;         
          z = total;         
   
//********************end of loops - asking user for instructions   
         
          cout << " c to continue with running total, \n n for a new calculation,  \n x to exit program: ";
          cin >> exit;
          cout << "\n\n";
          short i=0;
          while (((exit != 'c') && (exit != 'n') && (exit != 'x') && (i < 5)))
             {
                   i=i+1;
                   cout << " Please choose from the availabe options (c, n, x): " << endl;
                   cin >> exit;
                   if (i >= 5)
                   {exit = 'x';}
             }     
         
      }// close brace - while loop for continue or new             
    }//close brace - while loop for exit
   
     
   
   
    return 0;
}//close brace for main function
Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« Reply #1 on: August 24, 2009, 06:00:22 AM »

Code
GeSHi (cpp):
         float fadd (float x, float y)
         {
              float total;
              total = x + y;
              return total;
         }
Created by GeSHI 1.0.7.18
could be written as
Code
GeSHi (cpp):
         float fadd (float x, float y)
         {
              return x + y;
         }
Created by GeSHI 1.0.7.18
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #2 on: August 24, 2009, 07:10:08 AM »

Your main function is toooooo long.
A good rule of thumb is that the function should fit in on one screen.
Split it up into some logical units that are easier to analyze.
Logged
prometheus
semi-N00b
**
Posts: 39


View Profile
« Reply #3 on: August 24, 2009, 07:14:05 AM »

ih8 - cool, that is a little more efficient. good to know. thanks

myork - how do you make it shorter? do you put the logical units into different functions?
Logged
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #4 on: August 24, 2009, 07:25:37 AM »

Rather the reading each number and the op separately read it all in one go and validate.

Code:
char getInputs(double& lhs,double& rhs)
{
    bool validInput = false;
    char op;

    while(!validInput)
    {
        std::cout << "Enter a simple expression" << std::endl;

        std::string    line;
        std::getline(cin,line);

        std::stringstream  linestream(line);
        linestream >> lhs >> op >> rhs;

        if (linestream && (op == '+' || op == '-' || op == '*' || op == '/'))
        {
            validInput = true;
        }
        else
        {
            std::cout << "Input not valid." << std::endl;
        }
    }
    return op;
}
Logged
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #5 on: August 24, 2009, 07:30:07 AM »

Some people think the following makes the code look neater (I am divided on the concept).

Code:
case '*':
    if (exit == 'n')
        total = fmul (x, y);
    else
        total = fmul (z, y);
    break;

    // OR

case '+':  total = fadd( (exit == 'n')?x:z, y); break;
case '-':  total = fsub( (exit == 'n')?x:z, y); break;
case '*':  total = fmul( (exit == 'n')?x:z, y); break;
case '/':  total = fdiv( (exit == 'n')?x:z, y); break;
« Last Edit: August 24, 2009, 07:31:49 AM by myork » Logged
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #6 on: August 24, 2009, 07:35:56 AM »

Code
GeSHi (cpp):
         float fadd (float x, float y)
         {
              float total;
              total = x + y;
              return total;
         }
Created by GeSHI 1.0.7.18
could be written as
Code
GeSHi (cpp):
         float fadd (float x, float y)
         {
              return x + y;
         }
Created by GeSHI 1.0.7.18
I prefer to keep the temporary variable. Underneath the compiler will generate exactly the same code. But if you have the temporary variable it makes it easier when debugging as you will be able to inspect the value of the variable while stepping through the code. Its nit picking in this code but its just a good habit.

Code
GeSHi (cpp):
         float fadd (float x, float y)
         {
              float total  = x+y;
              return total;
         }
Created by GeSHI 1.0.7.18
« Last Edit: August 24, 2009, 07:43:03 AM by myork » Logged
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #7 on: August 24, 2009, 07:39:34 AM »

Think of using a map to hold the functions.

Code:
typedef  double(*MATHFUNC)(double,double);
std::map<char,MATHFUNC>   funtions;

functions['+'] =fadd;
functions['-'] = fsub;
// etc

// Then call the function:
double result = (functions[op])(lhs,rhs);

By doing this your code is data driven.
i.e. The main part of the program can be extended without modification, by updating the data which can be configured by runtime properties.
« Last Edit: August 24, 2009, 07:41:23 AM by myork » Logged
prometheus
semi-N00b
**
Posts: 39


View Profile
« Reply #8 on: August 24, 2009, 07:02:38 PM »

myork, I must confess that I don't understand some of the code you suggest. I'm just familiar with the basics right now, I haven't even begun learning classes yet. I will revisit your suggestions once I have learned more.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!