当我输入字符类型的数字时,为什么我无法获得整数?

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

本文关键字:整数 为什么 字符 输入 类型 数字      更新时间:2023-10-16

我在字符类型变量中输入一个数字。 比如 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:UserskdwyhsourcereposMyFirstProjectDebugMyFirstProject.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",这就是它不起作用的原因。

您可以将其声明为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-255n");
continue;
}
break;
}
printf("Number is %hhun", value);
}
相关文章: