Menu

Help with filework and parameter passing!

2002-11-03
2012-09-26
  • Nobody/Anonymous

    Hello everyone.  Hoping someone can help me with a some code I am writing for school.  I am brand new to C++ and trying code a program that emulates a calculator.  This program uses a output file and passes parameter to the two simple functions I have written.  I am not sure if I am on the right track here so if someone could take a look and let me know if I am doing this the right way.  Thanks in advace.
    Dave

    /********************************************************************************************************            
      

         Purpose:      This program simulates a hand-held electronic calculator.  The calculator has two registers,
                       r1 and r2, and an accumulator.  The calculator can perform the following mathematical functions:
                       addition, subtraction, multiplication, division, exponentation, square root.
                      
         Error
         Checking:     This program will check for incorrect menu selection and square root of a negative number
                       This program also checks for division by zero.

         Compiler:     Dev-C++ ver 4.9.5.0 Blooshed Software

         Limitations:  Program is limited to a the given mathematical operands and can only work with to registers or
                       numbers at once.
                         

    *******************************************************************************************************/
    #include <iostream.h>                                                     //  preprocessor directive 
    #include <iomanip.h>                                                      //  preprocessor directive
    #include <stdlib.h>                                                       //  preprocessor directive
    #include <ctype.h>                                                        //  preprocessor directive
    #include <math.h>                                                         //  preprocessor directive
    #include <fstream.h>                                                      //  for output file

    float Calculator (char operand, float r1, float r2, float answer, ofstream OutAccumulator);                              // Prototyping of function
    void PrintOutput (ofstream OutAccumulator);                                                        // Prototyping of function                                            

    int main()                                                                 //  function main
    {
        int stop;                                                             //  simple stop
        char operand;
        float r1;                                                             //  assign decimal value to variable                                                             // assign double decimal value to variable
        float r2;                                                             //  assign decimal value to variable
        float answer;
        char cont ='x';                                                       // setting flag
        ofstream OutAccumulator;                                              //  holds solution values for problem    
     
     
        cout << "David W. Skurda              CMIS140: Assignment #4                     11/03/02" << endl;
        do                                                                     // do loop
        {
            cont = 'x';                                                        // flag 
                                                                          
            system("cls");
            cout.precision(3);                                                 // set decimal precision
            cout.setf(ios::fixed,ios::floatfield);                             // set up floating point
            cout.setf(ios::showpoint);                                         // output format
           
            OutAccumulator.open("OutAccumulator.dat");                         // open file
                                                                                                                                                                                                                                        
            cout << "--------------------------------------------------------------------------------" << endl;
            cout << "|                      WELCOME TO DAVE'S MATH MACHINE                          |" << endl;
            cout << "|                                                                              |" << endl;
            cout << "|                This program simulates a hand-calculator and                  |" << endl;
            cout << "|                can perfor the following mathematical operands:               |" << endl;
            cout << "|                Addition       = +                                            |" << endl;   
            cout << "|                Subtraction    = -                                            |" << endl;
            cout << "|                Multiplication = *                                            |" << endl;
            cout << "|                Division       = /                                            |" << endl;
            cout << "|                Exponent       = e or E                                       |" << endl;
            cout << "|                Square Root    = s or S                                       |" << endl;
            cout << "|                To quit type:  q or Q                                         |" << endl;
            cout << "|                                                                              |" << endl;
            cout << "|                Please enter all problems in the folling form:                |" << endl;
            cout << "|                               operand, r1, r2                                |" << endl;
            cout << "|                Sample Problem:                                               |" << endl;
            cout << "|                  operand = +                                                 |" << endl;
            cout << "|                  r1=2                                                        |" << endl;
            cout << "|                  r2=2                                                        |" << endl;
            cout << "|                         r1+r2=4                                              |" << endl;
            cout << "|                                                                              |" << endl;       
            cout << "|                NOTE: If you are using the square root operand,               |" << endl;
            cout << "|                      enter a 0 for r1 and the number you wish                |" << endl;
            cout << "|                      to find the root of for r2.                             |" << endl;
            cout << "|                                                                              |" << endl;
            cout << "--------------------------------------------------------------------------------" << endl;
           
           
            cout << "Enter the operand to be perfomed."<< endl;
            cout << "select one of the following +,-,*,/,e,s, or q: ";
            cin  >> operand;
               cout << "Enter the first number to be used in the probem 'r1'."<< endl;
            cout << "r1: ";
            cin >> r1;
                                              
           
            cout << "Enter the second number to be used in the problem 'r2'."<< endl;
            cout << "r2: ";
            cin >> r2;
          

             cout << "You entered:" <<  r1 << operand <<  r2 << endl;
            
           
            float Calculator (char operand, float r1, float r2, float answer, ofstream OutAccumulator);        // function call for Calculator
            void PrintOutput (ofstream OutAccumulator);                                                       // function call for Printoutput 
           
            while((cont != 'Y') && (cont != 'N'))                                 // prompt user to repeat or close program
            {                                                                     // while loop
                 cout << "  Do you want to solve another problem? (Y/N): ";
                 cin  >> cont;
                 cont = toupper(cont);
                 if((cont != 'Y') && (cont != 'N'))
                 {
                     cout << "  Enter 'Y' for yes or 'N' for no. \n";              // cout statement outputs a character string
                  }
             }
             }while((cont != 'N'));                                                  // while loop
                 cout << "  Type any key and then enter to end program.";           // simple stop added to the program
                 cin >> stop;                                                       // so the output is viewable in dos
        return 0;                                                                   // end function
        }

    }

          
          
    float Calculator (char operand, float r1, float r2, float answer, ofstream OutAccumulator); 
    {
        switch (operand)                                                             // switch statement to chose proper operand
        {
            case '+' :  answer = ( r1 +  r2);                                         
            case '-' :  answer = ( r1 -  r2);                                       
            case '*' :  answer = ( r1 *  r2);                                    
            case '/' :  answer = ( r1 /  r2);
            if( r2 == 0 )                                                          // error checking for dision by zero 
            {                                                                      // if loop
                cout << "  Division by zero is not allowed. \n\n"<< endl;          // cout statement outputs a character string
            }                                       
            case 'E' : answer =  pow(r1, r2);                                         
            case 'S' : answer =  sqrt(r2);
            if( r1 == -float );                                                                     // error checking for square root of a negative number 
            {                                                                                      // if loop
                cout << "  Square root of a negative number is not allowed. \n\n"<< endl;          // cout statement outputs a character string
            }
            case 'Q' :  return 0;
            default : cout << "  You have entered an invalid menu selection.  Please enter an operand from the menu above. \n";  // error checking for invalid menu selection         // cout statement outputs a character string
         }
    }
    void PrintOutput (ofstream OutAccumulator,float answer)
    {
    OutAccumulator << "The answer is"<< answer << endl;                                                // print output from file
    }
       
           
        
       

     
    • Nobody/Anonymous

      I got boared for 5 min so I fixed your code.  You will need to read up on Functions and Switch case.  I made comments on all of my changes.  Then that way you can tell where they are, and try to determine why they needed to happen.  I you get compleatly stuck, then you can contact me by
      MSN - curtis8@hotmail.com
      ICQ - 82286030
      Yahoo! - Curtis8ca
      AIM - csutter2

      here is the code now
      --------------------------------------------------------------------------
      #include <iostream.h> // preprocessor directive
      #include <iomanip.h> // preprocessor directive
      #include <stdlib.h> // preprocessor directive
      #include <ctype.h> // preprocessor directive
      #include <math.h> // preprocessor directive
      #include <fstream.h> // for output file

      float Calculator (char operand, float r1, float r2); // Prototyping of function
      void PrintOutput (float Answer); // Prototyping of function

      int main() // function main
      {
      //int stop; // simple stop
      char operand;
      float r1; // assign decimal value to variable // assign double decimal value to variable
      float r2; // assign decimal value to variable
      float answer;
      char cont ='x'; // setting flag
      ofstream OutAccumulator; // holds solution values for problem

      cout << "David W. Skurda CMIS140: Assignment #4 11/03/02" << endl;
      do // do loop
      {
      cont = 'x'; // flag

      system("cls");
      cout.precision(3); // set decimal precision
      cout.setf(ios::fixed,ios::floatfield); // set up floating point
      cout.setf(ios::showpoint); // output format

      OutAccumulator.open("OutAccumulator.dat"); // open file

      cout << "--------------------------------------------------------------------------------" << endl;
      cout << "| WELCOME TO DAVE'S MATH MACHINE |" << endl;
      cout << "| |" << endl;
      cout << "| This program simulates a hand-calculator and |" << endl;
      cout << "| can perfor the following mathematical operands: |" << endl;
      cout << "| Addition = + |" << endl;
      cout << "| Subtraction = - |" << endl;
      cout << "| Multiplication = * |" << endl;
      cout << "| Division = / |" << endl;
      cout << "| Exponent = e or E |" << endl;
      cout << "| Square Root = s or S |" << endl;
      cout << "| To quit type: q or Q |" << endl;
      cout << "| |" << endl;
      cout << "| Please enter all problems in the folling form: |" << endl;
      cout << "| operand, r1, r2 |" << endl;
      cout << "| Sample Problem: |" << endl;
      cout << "| operand = + |" << endl;
      cout << "| r1=2 |" << endl;
      cout << "| r2=2 |" << endl;
      cout << "| r1+r2=4 |" << endl;
      cout << "| |" << endl;
      cout << "| NOTE: If you are using the square root operand, |" << endl;
      cout << "| enter a 0 for r1 and the number you wish |" << endl;
      cout << "| to find the root of for r2. |" << endl;
      cout << "| |" << endl;
      cout << "--------------------------------------------------------------------------------" << endl;

      cout << "Enter the operand to be perfomed."<< endl;
      cout << "select one of the following +,-,*,/,e,s, or q: ";
      cin >> operand;
      cout << "Enter the first number to be used in the probem 'r1'."<< endl;
      cout << "r1: ";
      cin >> r1;

      cout << "Enter the second number to be used in the problem 'r2'."<< endl;
      cout << "r2: ";
      cin >> r2;

      cout << "You entered:" << r1 << operand << r2 << endl;

      answer = Calculator (operand, r1, r2); // function call for Calculator
      PrintOutput (answer); // function call for Printoutput

      while((cont != 'Y') && (cont != 'N')) // prompt user to repeat or close program
      { // while loop
      cout << " Do you want to solve another problem? (Y/N): ";
      cin >> cont;
      cont = toupper(cont);
      if((cont != 'Y') && (cont != 'N'))
      {
      cout << " Enter 'Y' for yes or 'N' for no. \n"; // cout statement outputs a character string
      }
      }
      }while((cont != 'N')); // while loop
      //cout << " Type any key and then enter to end program."; // simple stop added to the program
      //cin >> stop; // so the output is viewable in dos
      system("PAUSE");
      return 0; // end function
      }

      //} Removed unnessiary brace

      float Calculator (char operand, float r1, float r2)
      {
      float answer;//added
      switch (operand) // switch statement to chose proper operand
      {
      case '+' : answer = ( r1 + r2);
                  break;//added all breaks and fixed error checking
      case '-' : answer = ( r1 - r2);
                  break;
      case '*' : answer = ( r1 * r2);
                  break;
      case '/' : 
      if( r2 == 0 ) // error checking for dision by zero
      { // if loop
      cout << " Division by zero is not allowed. \n\n"<< endl; // cout statement outputs a character string
      break;
      }
          answer = ( r1 / r2);
          break;
      case 'e'://added
      case 'E' : answer = pow(r1, r2);
                  break;
      case 'S' :
      case 's': //added 
              if( r1 < 0 ); // error checking for square root of a negative number //fixed -float
      { // if loop
      cout << " Square root of a negative number is not allowed. \n\n"<< endl; // cout statement outputs a character string
      break;
      }
      answer = sqrt(r2);
      break;
      case 'q'://added
      case 'Q' : return 0;
                  break;
      default : cout << " You have entered an invalid menu selection. Please enter an operand from the menu above. \n"; // error checking for invalid menu selection // cout statement outputs a character string
              break;
      }
      return answer;//added
      }
      void PrintOutput (float answer) //fixed answer output
      {
      cout << "The answer is"<< answer << endl; // print output from file
      }
      --------------------------------------------------------------------------

      This code brought to you by...

      Curtis  ;)

       
    • Nobody/Anonymous

      Thanks much appreciated!
      Dave

       
    • Nobody/Anonymous

      One thing you can do, when you have a long bunch of code in which you are making changes that someone might want to find and look at, is to put a key in your comments.  My initials are WAK, so that is usually what I will use, i.e.

      } //WAK - Remove unneccessary brace

      Then they, or I, can do a search and find what I changed.  Habit I go into when working with others, take it or leave it as sound advice...

      Wayne

       
    • Nobody/Anonymous

      One more quick question!
      In the beginning of the main I open a file.  I want to send the answer to file and then close the file out.

      OutAccumulator.open("OutAccumulator.dat");     
      OutAccumulator >> float answer >> endl;            
      OutAccumulator.close("OutAccumulator.dat");

      Where do I need to implement this?  Thanks in advance.

      Dave

       
    • Nobody/Anonymous

      Thanks Wayne I will keep that in mind.

      Dave - you are trying to use files?  Why would you save the answer to a file?  Oh well here you go...
      //need
      #include <fstream.h>

      //in main
      ofstream OutAccumulator ("OutAccumulator.dat");

      //after you have answer
      OutAccumulator >> answer >> endl;

      //before end of main
      OutAccumulator.close();

      Hope this helps

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.