c++ ASCII value

来源:百度知道 编辑:UC知道 时间:2024/09/20 21:15:58
C++ 菜鸟请教大家2个题目:
1. Write a function that inputs a character and outputs its ASCII value
2. Write a function that inputs a character and returns its ASCII value

在线等 给高分
能详细一点吗? 小弟初学C++ 实在弄不太懂

/* 1. Write a function that inputs a character and outputs its ASCII value. */

#include <iostream>
using namespace std;

bool function1 () // 第1个问题的函数
{
char c;

cout << "Please enter a character :" << endl; // 提示用户输入一个char型数据
cin >> c;
cout << static_cast<int> (c) << endl; // 强制类型转换为int型,即c所对应的ASCII码值
return true;
}

int main()
{
function1(); // 主函数调用函数:function1
}

/* 2. Write a function that inputs a character and returns its ASCII value. */

#include <iostream>
using namespace std;

int function2 () // 第2个问题的函数
{
char c;

cout << "Please enter a character :" << endl; // 提示用户输入一个char型数据
cin >> c;
return ( static_cast<int> (c) ); // 强制类型转换为int型,即c所对应的ASCII码值,并直接返回
}

int main()
{
int c2