mercredi 5 décembre 2018

How would you incorporate arrays and file use in a c++ calculator program?

I want to satisfy all rubric requirements for a project I'm doing for class. The array and file system implementation doesn't have to be applicable to this calculator project, however, I want to try and find a way to do this anyway. Any suggestions would be helpful on how to make this feasible. Here is what I have so far for code:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void sum();
void sub();
void multi();
void division();

int main()
{   

  int input; 
    cout<<"Make a selection \n";
    cout<<"Type 1 for addition \n";
    cout<<"Type 2 for subtraction \n";
    cout<<"Type 3 for multiplication \n";
    cout<<"Type 4 for division \n";
    cout<<"Type '0' to quit the program \n";

    cin>> input;
    switch (input)
    {
              case 1:
              sum();
              break;

              case 2:
              sub();
              break;

              case 3:
              multi();
              break;

              case 4:
              division();
              break;

              case 0:
              exit(0);   

              default:

              cout<<"Input " << "'" << input << "'" << " is not valid" << endl ;

    } 

    while (input != 0 && input >=5);
    {
     getchar();
    }

    }

void sum()
{
  double sum;
  double num1, num2;
  cout <<"Enter 2 numbers" << endl;
  cout <<"Enter first number \n";
  cin >> num1;
  cout <<"Enter second number \n";
  cin >> num2;
  sum = num1 + num2;
  cout << num1 << " + " << num2 << " = " << sum << endl;
}

void sub()
{
  double sub;
  double num1, num2;
  cout <<"Enter 2 numbers" << endl;
  cout <<"Enter first number \n";
  cin >> num1;
  cout <<"Enter second number \n";
  cin >> num2;
  sub = num1 - num2;
  cout << num1 << " - " << num2 << " = " << sub << endl;
}

void multi()

{
  double multi;
  double num1, num2;
  cout <<"Enter 2 numbers" << endl;  
  cout <<"Enter first number \n";
  cin >> num1;
  cout <<"Enter second number \n";
  cin >> num2;
  multi = num1 * num2;
  cout << num1 << " x " << num2 << " = " << multi << endl;
}

void division()
     {
      double division;
      double num1, num2;
      cout <<"Enter 2 numbers" << endl;
      cout <<"Enter first number \n";
      cin>>num1;
      cout <<"Enter second number (to divide first number by) \n";
      cin >> num2;
      division = num1/num2;
      cout << num1 << " / " << num2 << " = " << division << endl;
      }      

Aucun commentaire:

Enregistrer un commentaire