Tuesday, September 18, 2012

C++ Assignment

Chrichtian Neal : C++ Assignment, calc function

This is a C++ program that invokes a function calc(), intakes two integers and an arithmetic operator, and prints the corresponding result. 

 

#include <cstdlib>   /* the program works fine without this header but I'm sure it's cool since the library uses a namespace*/   

#include <iostream>    /* input and output stream header*/


#include <conio.h>   /* input and output console header. used for clrscr(), getch() functions.*/

#include <process.h> /*threads and processes function declarations and macros header */

using namespace std; /*instructing compiler to use everything in the std namespace.*/

int main () /* declaration of main function: program execution starts here */ 

          system("CLS"); //command that clears the screen

          void calc(int, int, char); //declaring calc function with variables 
          int a, b;  // declaring variables as integer 
          cout<<"Please enter first number: "; //Sends out what is between the " " to the monitor, and expects a input from keyboard 
          cin>>a /*retrieves numeric inputs from keyboard and pass to variable 'a' /

          char ch; //retrieves character string from keyboard 
          cout<<"\nPlease enter an arithmetic operator (+,-,*,/,%) : "; 
          cin>>ch; 

          cout<< "\nPlease enter second number: "; 
          cin>>b; 

          calc(a, b, ch); // calling of function 

          calc getch(); /* the function prompts the user to press a character but the character is not printed on screen */


int calc(int x, int y, char ch)/*initializing calc function byreferencing 'a' and 'b' value with 'x' and 'y' */
 {
          switch(ch) /*evaluates the result to each of the case expressions in relation to the 'ch' values (+,-,*,/,%) */
          { 
               /*calculating each case based on inputted values and by operators selected */

        case '+' :cout<<"\nThe sum of "<<x<<" plus "<<y<< " is "<<(x+y);
        break;

        case '-' :cout<<"\nThe difference of "<<x<<" minus "<<y<< " is "<<(x-y);
        break;

        case '*' :cout<<"\nThe product of "<<x<<" multiplied by "<<y<< " is "<<(x*y);
        break;

        case '/' :cout<<"\nThe quotient of "<<x<<" divided by "<<y<< " is "<<(x/y);
        break;

case '%' :cout<<"\nThe reminder of "<<x<<" divided by "<<y<< " is "<<(x%y);
        break;
    }
   
    return 0; /* returns 0 at end of program to show normalcy since the main function 'int main()' demands a value of an integer type be returned */
   
}


Chrichtian Neal

No comments:

Post a Comment