首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么判断输入的数据是整型还是字符型

2012-06-03 
如何判断输入的数据是整型还是字符型?C/C++ code#include iostream#include stringusing namespace st

如何判断输入的数据是整型还是字符型?

C/C++ code
#include <iostream>#include <string>using namespace std;char continueWorking ();char ch;void main (){    char operation;    char FirstNumber[100];    int SecondNumber;    do    {        cout << "Key in an operator: + - * /" << endl;        cin >> operation;        if (operation != '+' && operation != '-' && operation != '*' && operation != '/')        {            cout << "you enter a bad operation!" << endl;            continue;        }        cout << "enter the first number:" << endl;        cin >> FirstNumber;        for (int j=0; j<strlen(FirstNumber); j++)//如何判断用户输入的是字符还是整型数据呢?        {                        if ((int)FirstNumber[j] < 0 || (int)FirstNumber[j] > 9)            {                cout << "date error !" << endl;            }                break;            }//这个循环有错误!当输入整数的时候,却没有break?!!!!应该如何改正呢?        cout << "enter the second number:" << endl;        cin >> SecondNumber;                switch (operation)        {        case '+':            cout << FirstNumber + SecondNumber << endl;            break;        case '-': cout <<  FirstNumber - SecondNumber << endl;            break;        case '*': cout << (int)FirstNumber * SecondNumber  << endl;            break;        case '/': cout << (int)FirstNumber / SecondNumber<< endl;            break;        default: break;        }            }while (continueWorking () == 'y');}char continueWorking (){    do    {        cout << "Would you like to continue ? (y / n)" << endl;        cin >> ch;    }while (ch != 'y' && ch != 'n');    return ch;}


[解决办法]
C/C++ code
#include <iostream>#include <string>using namespace std;char continueWorking ();bool isInteger(const char* str);char ch;void main (){    char operation;    char FirstNumber[100];    int SecondNumber;    do    {        cout << "Key in an operator: + - * /" << endl;        cin >> operation;        if (operation != '+' && operation != '-' && operation != '*' && operation != '/')        {            cout << "you enter a bad operation!" << endl;            continue;        }        cout << "enter the first number:" << endl;        cin >> FirstNumber;        if ( !isInteger(FirstNumber) ) continue;               cout << "enter the second number:" << endl;        cin >> SecondNumber;                int first = atoi(FirstNumber);        switch (operation)        {        case '+':            cout << first + SecondNumber << endl;            break;        case '-': cout <<  first - SecondNumber << endl;            break;        case '*': cout << first * SecondNumber  << endl;            break;        case '/': cout << first / SecondNumber<< endl;            break;        default: break;        }            }while (continueWorking () == 'y');}char continueWorking (){    do    {        cout << "Would you like to continue ? (y / n)" << endl;        cin >> ch;    }while (ch != 'y' && ch != 'n');    return ch;}bool isInteger(const char* str){   bool isInt = true;   for (int j=0; j<strlen(str); j++)//如何判断用户输入的是字符还是整型数据呢?   {      if ( str[j] < '0' || str[j] > '9')      {         cout << "date error !" << endl;         isInt = false;         break;          }               }//这个循环有错误!当输入整数的时候,却没有break?!!!!应该如何改正呢?   return isInt;} 

热点排行