簡體   English   中英

C ++基本變量識別

[英]C++ Basic Variable Identification

因此,我嘗試自己學習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;
}

變量的類型是在編譯時由編譯器在分析變量聲明方式時確定的(因此,無論您輸入程序的值如何,變量都不會改變)。 您已將myNumber聲明為int myNumber ,因此已將其signed int myNumber; (語言C ++規定是這樣,沒有辦法讓不同),這意味着它只能存儲符號數。 如果嘗試將其聲明為unsignedunsigned int ,則它將僅包含無符號值。 如果您嘗試讀取一個unsigned變量並輸入一個負值,則會出現問題,因為這樣,結果是不確定的(即使聽起來像是諷刺的,也可以定義):)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM