我可以使用开关来保持功能吗?

TIM*_*THY 0 c++ math switch-statement

我有一个3文件程序,基本上自学c ++.我有一个问题.我做了一个开关来使用数学函数.我需要把它放在一个变量中,但由于某种原因我得到一个零.

还有一个问题,当我选择4(除)时它会崩溃......有原因吗?

主文件:

#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 
Run Code Online (Sandbox Code Playgroud)

这是我的math.h档案

#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
Run Code Online (Sandbox Code Playgroud)

这是我的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 
Run Code Online (Sandbox Code Playgroud)

Ben*_*ett 5

在从用户获取数据之前,您使用a和b调用函数.尝试保存他们输入时选择的数学函数,并在向他们询问a和b后将开关移至.

#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;              

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

           cin >> a;

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

           cin >> b;

           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 << c;

           cin >> test;

           }else if (answer == "no" || answer == "NO" || answer == "No"){       
                 }//GAME ENDS   
}// end of int main 
Run Code Online (Sandbox Code Playgroud)