簡體   English   中英

當我輸入一個char類型的數字時,為什么我不能得到一個整數?

[英]When I input a number in char type, why i can't get a whole number?

我在 char 類型變量中輸入了一個數字。 像 12 或 22。但是,控制台顯示 1 或 2。我如何在控制台中獲得整數 12,22?

#include <iostream>

int main()
{
    using namespace std;

    char a = 0;
    cin >> a;
    cout << a << endl;
    return 0;
}

這是控制台結果。

12
1

C:\Users\kdwyh\source\repos\MyFirstProject\Debug\MyFirstProject.exe(프로세스 18464개)이(가) 종료되었습니다(코드: 0개).
이 창을 닫으려면 아무 키나 누르세요...

我不使用 int、string 和其他東西的原因是因為我想在一個變量中同時獲取數字和字符。 所以我想同時看到組合數字和字符的結果。 在那個過程中,我無法得到一個整數。

#include <iostream>

using namespace std;

int index = 0;
constexpr int pagenum = 10;

void chapterlist(void);

void nextlist(void);

void beforelist(void);

void movechapter(char a);


int main(void)
{   
    char userin = 0;
    bool toggle = 0;
    
    cout << "결과를 볼 챕터를 고르시오." << endl;
    
    chapterlist();

    cout << "다음 페이지로 이동: n" << endl;
    
    cin >> userin;

    if (userin == 'n')
    {
    backflash:

        while(toggle == 0)
        {
            nextlist();
            cin >> userin;

            if (userin == 'b')
            {
                toggle = 1;
                goto backflash;
            }
            else if (userin == 'n')
                continue;
            else
            {
                system("cls");
                movechapter(userin);
                break;
            }
        }

        while(toggle == 1)
        {
            beforelist();
            cin >> userin;

            if (userin == 'n')
            {
                toggle = 0;
                goto backflash;
            }
            else if (userin == 'b')
                continue;
            else
            {
                system("cls");
                movechapter(userin);
                break;
            }
        }
    }
    else
    {
        system("cls");
        movechapter(userin);
    }
    
    return 0;
}

void chapterlist(void)
{
    int x = 0;

    for (x = index + 1; x <= index + 10; x++)
        cout << "Chapter." << x << endl;
}

void nextlist(void)
{
    system("cls");

    cout << "결과를 볼 챕터를 고르시오." << endl;

    index = index + pagenum;
    chapterlist();

    cout << "다음 페이지로 이동: n" << endl;
    cout << "이전 페이지로 이동: b" << endl;
}

void beforelist(void)
{
    system("cls");

    cout << "결과를 볼 챕터를 고르시오." << endl;

    index = index - pagenum;
    chapterlist();

    cout << "다음 페이지로 이동: n" << endl;
    cout << "이전 페이지로 이동: b" << endl;
}

void movechapter(char a)
{
    cout << "선택한 Chapter." << a << "의 결과입니다." << endl;
}

在 movechapter() 中,控制台顯示 a 是 1 或 2,而不是 12、22。

首先,您必須了解什么是char類型。

字符類型:它們可以表示單個字符,例如“A”或“$”。 最基本的類型是char ,它是一個單字節字符。 還為更寬的字符提供了其他類型。

為簡化起見, char只能容納一個字符。

與您的代碼一樣,“12”實際上是 2 個單獨的字符,“1”和“2”,這就是它不起作用的原因。

您可以將 a 聲明為int類型,而不是將a聲明為char類型,該類型旨在保存數字。 所以你會有:

int a = 0;

但是,請注意int的最大值通常為 2^31。

或者您可以使用std::string來存儲字符串。 但是,請注意,如果您希望對string類型進行任何計算,則需要先將它們轉換為數字類型:

int myInt = std::stoi(myString);

編輯:

因此,我在您更新后重新檢查了您的代碼,在您的情況下使用std::string沒有任何問題。 您仍然可以通過以下方式檢查用戶是否輸入了nb

if (userin == "n")

請注意,您將在要檢查的內容周圍使用雙引號或"letter"

另一方面,您可以使用:

if(std::all_of(userin .begin(), userin.end(), ::isdigit))

檢查用戶是否輸入了數字。

雖然char只是一個數字,但這里假定它的意思是“單個字符”用於輸入。 通過要求其他內容來解決此問題:

int a = 0;

您總是可以根據需要將其轉換為char ,當然,測試是否溢出。

您應該將字符讀入字符串,然后將該字符串轉換為 int。 使用諸如getline()類的東西來讀取輸入,而不是cin >> a也可能更有意義。

#include <string>
#include <iostream>
#include <stdexcept>
#include <stdio.h>

int main() {
    std::string input_string;
    /* note that there is no function that will convert an int string
       to a char, only to an int. You can cast this to a char if needed,
       or bounds check like I do */
    int value;

    while(1) {
        getline(std::cin, input_string);
        /* std::stoi throws std::invalid_argument when given a string
           that doesn't start with a number */
        try {
            value = std::stoi(input_string);
        } catch (std::invalid_argument) {
            printf("Invalid number!\n");
            continue;
        }
        /* You wanted a char, the max value of a `char` is 255. If 
           you are happy for any value, this check can be removed */
        if (value > 255) {
            printf("Too big, input a number between 0-255\n");
            continue;
        }
        break;
    }
    printf("Number is %hhu\n", value);
}

暫無
暫無

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

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