【发布时间】:2021-02-26 14:14:24
【问题描述】:
我必须编写一个简短的脚本,它将 int 更改为 ASCII char 并将 char 更改为 ASCII int。但我不知道怎么做。我写了这个简短的代码,但是出了点问题。我正在编程半年。 有人可以编写它吗?这是我第一次在c++中使用函数
#include <iostream>
#include <conio.h>
using namespace std;
char toChar(int n) {
return n + '0';
}
int toInt(char c) {
return c - '0';
}
int main()
{
int number;
cout << "Int: ";
cin >> number;
cout << "ASCII: " << static_cast<char>(number);
getch();
return 0;
}
【问题讨论】:
-
到底出了什么问题?当前输出和预期输出是多少?你是如何运行程序的(你输入的数字是多少)?另请参阅minimal reproducible example。
-
假设您有
int x = 1;,您可以将其解释为ascii1,或者您可以获得字符'1'的ascii 代码。为了确定你想要什么,你应该包括输入、输出和预期输出 -
如果
_getch有错误,请不要使用_getch!请改用getch -
@Dock 好的,我会试试这个
-
我编辑了它,但我不知道如何使用这个函数 toChar 和 toInt