C ++基本变量识别

时间:2017-01-23 14:54:03

标签: c++ integer logic typeid

所以我自己也在努力学习C ++,而且我似乎无法在我的程序中弄清楚这个错误。这是我到目前为止所拥有的。不管怎样,这一切似乎都很有效;我让用户通过我设置的提示输入他们的变量,但对我来说很棘手的是确定他们使用的是什么类型的整数。你可以看到我把几个条件比较字节大小和最大/最小值对于每个int类型(有符号,无符号,长,短),但每当我运行程序时,即使我不使用整数或一些奇怪的数字,thtat应该很容易显示为long,我的回报永远是一个该死的签名int。任何人都可以就我应该采取的后续措施提出一些建议吗?我似乎也无法触发我的错误消息,该错误消息应该在未在命令提示符中输入数字时显示。现在想着解决这个问题,我所要做的就是抛出一个会触发错误的禁止符号/字母的数组列表,对吗?

/*********************************************************************
** Program Filename: int.cpp
** Description: Checks int type and prints various other details
** Input: int
** Output: metadata
*********************************************************************/
#include <iostream>
#include <climits> // redundant, included in iostream
#include <typeinfo>
#include <string>
#include <sstream>
#include <limits>
#include <stdint.h>
#include <cstdio>
using namespace std;

/* define global variables */
int myNumber;
string dataType= "";

/*********************************************************************
** Function: Prompt
** Description: Prompts user for value
** Return: Void
*********************************************************************/
void prompt()
{
    /* define local variables*/
    string input = "";

    /* ask for value until valid value is entered, else show error message */
    while (true) {
        /* Pronmpt user */
        cout << "Please enter an integer: ";
        /*  get user input */
        getline(cin, input);

        /* add user input value to string buffer */
        stringstream streamNum(input);

        /* check to see if user entered a valid value */
        if (streamNum >> myNumber)
            cout << endl;
            break;
        /* error message if not int */
        cout << "ERROR: Invalid integer, please try again" << endl;
    }
}

/*********************************************************************
** Function: Get int type
** Description: conditionals to assign data type. using online docs for bit length
** Return: Void
*********************************************************************/
void typeID()
{
    if ( sizeof(myNumber) == 4 )
    {
        dataType = "signed";

    } else if ( sizeof(myNumber) == 4 && myNumber >= 0 )
    {
        dataType = "unsigned";

    } else if ( sizeof(myNumber) == 4 && myNumber >> -2147483647 && myNumber << 2147483647 )
    {
        dataType = "long";
    }else if ( sizeof(myNumber) == 2 && myNumber >> -32768 && myNumber << 32767 )
    {
        dataType = "short";
    }else
        cout << "You did not enter a valid integer. Try again soon! "<< numeric_limits<int>::min();
}

/*********************************************************************
** Function: limits
** Description: check max and min of various int types
** Return: Void
*********************************************************************/
void limits()
{
    if ( dataType == "signed" )
    {
        cout << "Maximum Signed Int Value: " << INT_MAX << endl;
        cout << "Minimum Signed Int Value: " << INT_MIN << endl;
    }else if ( dataType == "unsigned" )
    {
        cout << "Maximum Unsigned Int Value: " << UINT_MAX << endl;
        cout << "Minimum Unsigned Int Value: " << 0 << endl;
    } else if ( dataType == "long" )
    {
        cout << "Maximum Long Int Value: " << LONG_MIN << endl;
        cout << "Minimum Long Int Value: " << LONG_MAX << endl;
    }else if ( dataType == "short" )
    {
        cout << "Maximum Short Int Value: " << SHRT_MAX << endl;
        cout << "Minimum Short Int Value: " << SHRT_MIN << endl;
    }
}

/*********************************************************************
** Function: int info
** Description: prints info of user variable
** Return: Void
*********************************************************************/
void info()
{
    /* print details */
    cout << "You entered: " << myNumber << endl;
    cout << "Integer type: " << dataType << endl; /* demangles and prints type operator of user entered int */
}

/*********************************************************************
** Function: main function
** Description: runs program
*********************************************************************/
int main()
{
    prompt();
    typeID();
    info();
    limits();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

变量的类型在编译时由编译器在分析您如何声明变量时确定(因此它不会改变,与您输入程序的值无关)。您已将myNumber声明为int myNumber,因此它是signed int myNumber;(C ++语言以这种方式指定它,无法使其与众不同),这意味着它只能存储 < / strong>签名号码。如果您尝试将其声明为unsignedunsigned int,则会保留无符号值。如果您尝试读取unsigned变量并输入负值,则会出现问题,因为结果未定义(根据定义,即使听起来像是讽刺:) :)