DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   The Calculator (http://www.daniweb.com/forums/thread7184.html)

MaxC Jun 17th, 2004 10:11 pm
The Calculator
 
Hello fellow members,
it's me again with the silly questions.

I was give a scenario to write a program for and I finished it (I think).
Although is pretty simple (first program) I had a hard time trying to assemble it.
It’s exactly as the scenario I was given ( validations, clear screen, return, etc.) apart from the page titles, headers and little visual display (for what I know), which is just something I had fun doing it.

If you run the program you are presented with two choices. Either one you choose after that you are asked to enter a number. If by mistake you enter anything else than a number the program crashes. Apart from that it works.

I was wondering maybe you know there is a way I can validate the cin choice.
I hope is not to confusing to read (man, I lost myself a few times in it).

Thanks anyway

//---------------------------------------------------------------------
//                                        T H E  C A L C U L A T O R
//---------------------------------------------------------------------


#include <iostream.h>
#include <iomanip.h>        // contains the declarations for the data manipulators.
#include <stdlib.h>                // system("cls"); used for clearing the screen.


void HD1();            // program front main page heading
int ProcessLoop();                // processing loop.

//---------------- MULTIPLICATION FUNCTION PROTOTYPE ---------------------
void HD_M();                        // multiplication table heading
int Process_M();                // ask user for input
float Calculation_M();        // compute and display result

//---------------- CALCULATION FUNCTION PROTOTYPE -------------------------
void Process_C();                // Calculation Loop
void Menu();            // Displays menu options.
float input();          // Ask user to enter data.
float Add(), Sub();    // Calculation functions.
float Mul(), Div();
int Mod();              // Modulos function.
void LoopBack();                // return control to function.

//---------------- FRONT MAIN PAGE TITLE --------------------------------------

char PT1[] ="\n\t\t ----------------------------------------------"
                        "\n\t\t T H E  G U I L D S  P R I M A R Y  S C H O O L"
                        "\n\t\t ----------------------------------------------";
char PT2[] ="\n\n\n\t\t          T H E  C A L C U L A T O R        \n\n";

//---------------- CALCULATION PAGE TITLE ----------------------------------

char HD_C1[] = "\n\t\t\tC A L C U L A T I O N\n\n";
char HD_C2[] = "\t\t  -->  Only Numbers between 0 and 1000  <--\n\n\n";

//----------------- GLOBAL VARIABLES ---------------------------------------

char choice1;                                                // Multiplication Table
float row, column, product;                        // variables.


float firstNum, secondNum, result ; // Calculator 
char choice;                        // variables. 

//----------------- MAIN CONTROL -------------------------------------------

int main(){
        ProcessLoop();
        return 0;
}

//----------------- PROCESSING LOOP -----------------------------------------

int ProcessLoop(){
        cout << PT1 << PT2 ;
        HD1();
       
        while (choice1 !='C' && choice1 !='M'){
                cout << "\a\n\t\tERROR...Enter (M) or (C) only: ";
                cin >> choice1;
        }
                if (choice1 == 'M'){
                Process_M();
                }
                else if (choice1 == 'C'){
                Process_C();
                }
                return 0;
}

// ---------------- PRINT PROGRAM FRONT PAGE HEADING ----------------------------------

void HD1(){
        cout << "\n\n\t\tTo Create a Multiplication Table --> Enter (M)"
                <<        "\n\n\n\t\t\tTo Perform a Calculation --> Enter (C)\n"
                << "\n\n\n\t\tEnter M or C only: ";
        cin >> choice1;
}

//------------------------------------------------------------------------------
//                                                        MULTIPLICATION PROCESS                                       
//------------------------------------------------------------------------------


// ---------------- PRINT MULTIPLICATION TABLE FRONT PAGE ----------------------

void HD_M(){
        int row, column;
        cout << "\n                  M U L T I P L I C A T I O N  T A B L E\n\n";
       
        cout << "        | 1 2 3 4 5 6 7 8 9 10 11 12  13  14  15  16  17  18  19  20\n";
        cout << "        |                                                          \n";
        cout << "      ===============================================================\n";
        for (row = 1; row <=15; row++){
                cout << setw(7)<< row <<" |" ;

                for (column = 1; column <= 20; column++){
                        cout<<"__|";
                        }
                        cout << "\n";
        }
}

//----------------- INPUT DATA --------------------------------------------------

int Process_M(){
        system("cls");
        HD_M();
        cout << "\n\tEnter Table Number reqired: ";
        cin >> row ;
        while (row < 0 || row > 20){
                cout << "\a\tERROR...Enter Table Number 1 ---> 20: ";
                cin >> row;
        }
        system("cls");
        HD_M();
        cout << "\n\tEnter Lines required 1 --> 15: ";
        cin >> column;
        while (column < 0 || column > 15){
                cout << "\a\tERROR...Enter Lines Number 1 ---> 15: ";
                cin >> column;
        }
        Calculation_M();
        return 0;       
}

//----------------- CALCULATE TABLE -------------------------------------------

float Calculation_M(){
        system("cls");
        char exit;                        // Local
        int i;                                // variables.
       
        cout << "\n\t\t\t MULTIPLICATION TABLE OF " << row <<"\n";
        cout << "\t\t\t===========================\n\n";
        for (i = 1; i <= column; i++){
                cout <<"\t\t\t  "<< setw(2) << i <<" __|" << "_____________ ";
                product = row * i;
                cout << "" << setw(3) << setprecision(6) << product <<"\n\t\t\t      |\n";
                }
                cout <<"\n\n\t\tType \'E\' to Exit or Any other Key to continue...";
                cin >> exit;
                if (exit != 'E' && exit != 'e'){
                        system("cls");
                        Process_M();
                }
                else{
                        system("cls");
                        ProcessLoop();
                }
                return 0;
}

//-------------------------------------------------------------------------------
//                                                CALCULATION PROCESS
//-------------------------------------------------------------------------------


//---------------- CALCULATION LOOP -------------------------------------------

void Process_C(){
        system("cls");
        Menu();
        while (choice != 'E' && choice != 'e'){
                switch(choice) {
                case '+': Add(); break;
                case '-': Sub(); break;
                case '*': Mul(); break;
                case '/': Div(); break;
                default: Mod(); break;
                }
                system("cls");
                Menu();
        }
}

//--------------- DISPLAY CALCULATION MENU --------------------------------------

void Menu(){
    cout << HD_C1 << endl;
        cout << "\t\t\t (+)  <--  Addition\n\n";
        cout << "\t\t\t (-)  <--  Subtraction\n\n";
        cout << "\t\t\t (*)  <--  Multiplication\n\n";
        cout << "\t\t\t (/)  <--  Division\n\n";
        cout << "\t\t\t (%)  <--  Modulos(%)\n\n\n";
        cout << "\t\t\t (E)  <--  Exit\n\n";
        cout << "\n\t\t\tEnter an Operator for\n";
        cout << "\t\t\tthe Calculation of Your Choice: ";
        cin >> choice;

        while (choice == 'E' || choice == 'e'){
                system("cls");
                ProcessLoop();
        }
        while (choice!='+' && choice!='-' && choice!='*' && choice!='/' && choice!='%'){
                cout <<"\a\t\t\tERROR...enter correct sign: ";
                cin >> choice;
        }
}

//--------------- CALCULATION FUNCTIONS -------------------------------------------               
       
        float Add(){
                system("cls");
                cout << "\n\n\t\t\t\t(+)ADDITION\n\n\n";
                cout  << HD_C2;
                input();
                result = firstNum + secondNum;
                cout << "\n\n\t\t\t"<<firstNum<<" + "<<secondNum<<" = "<<result <<endl;
                LoopBack();
                return result;
        }
        float Sub(){
                system("cls");
                cout << "\n\n\t\t\t\t(-)SUBTRACTION\n\n\n";
                cout << HD_C2;
                input();
                result = firstNum - secondNum;
                cout << "\n\n\t\t\t"<<firstNum<<" - "<<secondNum<<" = "<<result <<endl;
                LoopBack();
                return result;
        }
        float Mul(){
                system("cls");
                cout << "\n\n\t\t\t\t(*)MULTIPLICATION\n\n\n";
                cout << HD_C2;
                input();
                result = firstNum * secondNum;
                cout << "\n\n\t\t\t"<<firstNum<<" * "<<secondNum<<" = "<<result <<endl;
                LoopBack();
                return result;
        }
        float Div(){
                system("cls");
                cout << "\n\n\t\t\t\t(/)DIVISION\n\n\n";
                cout << HD_C2;
                input();
                result = firstNum / secondNum;
                cout << "\n\n\t\t\t"<<firstNum<<" / "<<secondNum<<" = "<<result <<endl;
                LoopBack();
                return result;
        }
        int Mod(){
                system("cls");
                cout << "\n\n\t\t\t\t(%)MODULOS\n\n\n";
                cout << HD_C2;
                cout << "\t\t\tNote: The modulos operator (%) is used\n";
                cout << "\t\t\tto calculate the remainder from\n";
                cout << "\t\t\tdividing two \"integer numbers\".\n\n";
                int firstNum, secondNum, result;        // local variables
                cout << "\n\t\t\tEnter First Number: ";
                cin >> firstNum;
                cout << "\n\t\t\tEnter Second Number: ";
                cin >> secondNum;
                result = firstNum % secondNum;
                cout << "\n\n\t\t\t"<<firstNum<<" % "<<secondNum<<" = "<<result <<endl;
                LoopBack();
                return result;
        }

//-------------- INPUT DATA ----------------------------------------------------
       
        float input(){
                cout << "\n\t\t\tEnter First Number: ";
                cin >> firstNum;
                cout << "\n\t\t\tEnter Second Number: ";
                cin >> secondNum;
                while ((firstNum < 0 || firstNum > 1000)||(secondNum < 0 || secondNum > 999)){
                      cout << "\t\t\t\aERROR...numbers of digits exceed --> (1 - 1000)\n";
            input();
                }
                return 0;
        }
       
//-------------- RETURN CONTROL TO FUNCTION --------------------------------------
       
void LoopBack(){
char exit;    // Local variable
cout <<"\n\n\n\t\tType \'E\' to Exit or Any other Key to continue...";
        cin >> exit;
        if (exit != 'E' && exit != 'e'){
                system("cls");
                Process_C();
        }
        else{
                system("cls");
                ProcessLoop();
        }
}

BinaryMayhem Jun 17th, 2004 11:28 pm
Re: The Calculator
 
the problem is caused because you are trying to store a character in a float...

you should make the input a string, so you can test the data to make sure its valid.

char crows[255];

cin >> crows;

row = atof(crows);

....
while (row < 0 || row > 20){

btw....
why are you using a float. rows are always going to be an integer... it is bad programming practice to use so many glocal variables.
atof = float, atoi = integer (stlidb.h or math.h)...


All times are GMT -4. The time now is 12:43 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC