【问题标题】:C++ Basic Variable IdentificationC++ 基本变量识别
【发布时间】:2017-01-23 14:54:02
【问题描述】:

所以我正在尝试自己学习 C++,但我似乎无法在我的程序中找出这个特定的错误。这是我到目前为止所拥有的。无论如何,这一切似乎都很好。我让用户通过我设置的提示输入他们的变量,但对我来说棘手的是确定他们使用的是哪种整数。如您所见,我将一些条件放在一起比较字节大小和每个的最大/最小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;
}

【问题讨论】:

  • 您已将myNumber 声明为int,因此它每次都是一个有符号整数。如果你想改变它,你实际上必须将变量声明为short myNumber;unsigned myNumber;。字符串流只会获取他们输入的数字并尝试将其放入您传递的 POD 中。
  • sizeof(x) 不依赖于x 的值。 (而且你的逻辑完全不合逻辑。而且你拼错了&lt;&gt;。)
  • @RyanP myNumber 变量应该保留为 int,但是您告诉我的是,一旦声明为 int,它就是一个 int,并且用户不能输入 long int 例如?是静态的吗?因为我相信我应该编写一个函数,该函数接受用户输入并根据我们的处理方式将其声明为长短有符号或无符号。
  • @molbdnilo,对不起,你能扩大 sizeof 吗?这会影响我在 OP 中提到的问题吗?也来吧,伙计,我正在尽力自学这一点。我的逻辑有什么问题?我真的很想走上正确的道路。
  • @RyanP 我正在研究类型转换,这可能可以解决我的问题。

标签: c++ integer logic typeid


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-07
    • 2015-10-23
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    相关资源
    最近更新 更多