Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何测试输入是字符串还是int_C++ - Fatal编程技术网

C++ 如何测试输入是字符串还是int

C++ 如何测试输入是字符串还是int,c++,C++,我正在构建一个计算器,用户可以为不同的操作输入不同的字符串cout将所有输入作为字符串。您可以使用atof转换为double。如果使用C++ 11,也可以使用 STOF。通常,你会尝试将输入转换成一个数字。如果失败了,那就不是数字。 #include <iostream> #include <string> #include <cmath> using namespace std; #include "PageFormat.h" #include "Help

我正在构建一个计算器,用户可以为不同的操作输入不同的字符串
cout将所有输入作为字符串。您可以使用
atof
转换为double。如果使用C++ 11,也可以使用<代码> STOF。

通常,你会尝试将输入转换成一个数字。如果失败了,那就不是数字。
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

#include "PageFormat.h"
#include "Help.h"
#include "MathFunctions.h"


int main()
{
//VARIABLES
bool mathMode;
string operatorType;


//OBJECTS
Help ho;                    //object for the help class
MathFunctions mfo;
PageFormat pfo;


//INTRO
cout << "****** CalcPal ******" ;

mathMode = true;

while (mathMode == true){
cout << endl << endl << "+,-,*,/,^,pi,sqrt,clear,quit,help" << endl;
cin >> operatorType;


if (operatorType == "+"){
    mfo.add();
}

if (operatorType == "-"){
    mfo.subtract();
}

if (operatorType == "/"){
    mfo.divide();
}

if (operatorType == "*"){
    mfo.multiply();
}

if (operatorType == "^"){
    mfo.power();
}

if (operatorType == "pi"){
    mfo.pi();
}

if (operatorType == "sqrt"){
    mfo.squareRoot();
}


if (operatorType == "clear" || operatorType == "Clear"){
    pfo.clearPage();
}

if (operatorType == "quit" || operatorType == "Quit"){
    mathMode = false;
}

if (operatorType == "help" || operatorType == "Help" ){                   //Triggers the help menu
    ho.helpMenu();
}

}
}
#include "Help.h"
#include "PageFormat.h"
#include "MathFunctions.h"
#include <iostream>
#include <cmath>
using namespace std;

MathFunctions::MathFunctions()
{
}


int MathFunctions::add(){                   // Addition Function


    float num1;
    float num2;

    cin >> num1;
    cout << "+" << endl;
    cin >> num2;
    float answer = num1 + num2;
    cout << "= " << answer;
}

int MathFunctions::subtract(){              //Subtraction Function


    float num1;
    float num2;

    cin >> num1;
    cout << "-" << endl;
    cin >> num2;
    float answer = num1 - num2;
    cout << "= " << answer;
}

int MathFunctions::divide(){                //Division function


    float num1;
    float num2;

    cin >> num1;
    cout << "/" << endl;
    cin >> num2;
    float answer = num1 / num2;
    cout << "= " << answer;
}

int MathFunctions::multiply(){              //Multiplication function


    float num1;
    float num2;

    cin >> num1;
    cout << "*" << endl;
    cin >> num2;
    float answer = num1 * num2;
    cout << "= " << answer;
}

int MathFunctions::power(){                 //Power function


    float num1;
    int num2;

    cin >> num1;
    cout << "^" << endl;
    cin >> num2;

    float num1Holder = num1;
    for (int powerTower = 1; powerTower < num2; powerTower ++){

        num1 = num1 * num1Holder;
    }
    cout << "= " << num1;
}

int MathFunctions::pi(){
    float num1;
    double pii;
    pii = 3.14159;

    cin >> num1;
    cout << "*" << endl << "pi" << endl;
    float answer = num1 * pii;
    cout << answer;
}

int MathFunctions::squareRoot(){
    float num1;

    cin >> num1;
    float answer = sqrt(num1);
    cout << answer;

}
#ifndef MATHFUNCTIONS_H
#define MATHFUNCTIONS_H


class MathFunctions
{
public:
    MathFunctions();
    int add();
    int subtract();
    int multiply();
    int divide();
    int power();
    int pi();
    int squareRoot();

private:
    float num1();
    float num2();
    float answer();
    double pii();
    int x;
    int y;


};

#endif // MATHFUNCTIONS_H