Can i use a switch to hold a function?

Posted by TIMOTHY on Stack Overflow See other posts from Stack Overflow or by TIMOTHY
Published on 2010-06-02T20:49:59Z Indexed on 2010/06/02 20:54 UTC
Read the original article Hit count: 191

Filed under:
|
|

I have a 3 file program, basically teaching myself c++. I have an issue. I made a switch to use the math function. I need and put it in a variable, but for some reason I get a zero as a result.

Also another issue, when I select 4 (divide) it crashes... Is there a reason?

Main file:

#include <iostream>
#include "math.h"
#include <string>

using namespace std;

int opersel;
int c;
int a;
int b;
string test;

int main(){

cout << "Welcome to Math-matrix v.34"<< endl;
cout << "Shall we begin?" <<endl;

//ASK USER IF THEY ARE READY TO BEGIN 

string answer;
cin >> answer;

if(answer == "yes" || answer == "YES" || answer == "Yes")
{

           cout << "excellent lets begin..." << endl;

           cout << "please select a operator..." << endl  << endl;


           cout << "(1) + " << endl;
           cout << "(2) - " << endl;
           cout << "(3) * " << endl;
           cout << "(4) / " << endl;

           cin >> opersel;

           switch(opersel){

                  case 1:
                  c = add(a,b);
                  break;
                  case 2:
                  c = sub(a,b);
                  break;
                  case 3:
                  c = multi(a,b);
                  break;
                  case 4:
                  c = divide(a,b);
                  break;
                  default:
                  cout << "error... retry" << endl;

                  }// end retry


           cout << "alright, how please select first digit?" << endl;

           cin >> a;

           cout << "excellent... and your second?" << endl;

           cin >> b;

           cout << c;

           cin >> test;

           }else if (answer == "no" || answer == "NO" || answer == "No"){


                 }//GAME ENDS








}// end of int main 

Here is my math.h file

#ifndef MATH_H
#define MATH_H

int add(int a, int b);


int sub(int a, int b);



int multi(int a, int b);


int divide(int a, int b);

#endif

Here is my math.cpp:

int add(int a, int b)
{

 return a + b;   

}

int sub(int a, int b)
{

 return a - b;   

}

int multi(int a, int b)
{

 return a * b;   

}

int divide(int a, int b)
{

 return a / b;   

}






}// end of int main 

© Stack Overflow or respective owner

Related posts about c++

Related posts about math