当我输入字符类型的数字时,为什么我无法获得整数?
When I input a number in char type, why i can't get a whole number?
我在字符类型变量中输入一个数字。 比如 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
没有错。您仍然可以通过以下方式检查用户是否有输入n
或b
:
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);
}
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 为什么:不同符号的整数比较只是偶尔发生?
- 当我输入字符类型的数字时,为什么我无法获得整数?
- 为什么C++总是显示十六进制内存地址,而不仅仅是整数?
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 为什么乘以常量有符号整数分数没有优化?
- 在C++中,如果我可以直接将整数分配给指针而不使用"new",为什么要使用"new"?
- 为什么初始化 std::vector 时不能使用模板整数?
- C++字符串大小减去整数不是预期值,为什么?
- 为什么此指针值不能转换为整数的规则是什么?
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 为什么我的 Char* 在填充整数后为空?
- 为什么将两个浮点数相加会得到一个整数C++?
- 为什么可以将整数分配给字符串变量?
- C++:为什么我的掷骰子函数在掷骰子数量时只返回偶数整数?
- 为什么 Clang 和 GCC 中两个无符号整数之和的结果类型不同
- 为什么对无符号字符进行算术运算会将它们提升为有符号整数
- 为什么输出是整数而不是双精度?
- 如果我们在其中输入一个整数,则字符会给出整数作为输出,但是当分配给它一个整数时,这不会发生。为什么?