c++ - 如何在两个字符之间添加所有 Ascii 值?

标签 c++

我的任务是:

Write a function called sumConsonants that will take as arguments two upper case letters. Your function should return a sum of all the ascii values of the consonants between the starting character and the ending character. Your main function should pass two values to the function and output the sum of all the consonant character values including the first and the last

这是我目前所拥有的:

#include <iostream>
using namespace std;

int sumConsonants(char, char);

int main()
{
  char char1, char2;
  cout << "Enter two upper case chars" << endl;
  cin >> char1 >> char2;

  cout << "The sum of the consonants between the two chars is " << sumConsonants(char1, char2) << endl;


  return 0;
}

int sumConsonants(char first, char last)
{
  char f = first;
  char l = last;

  int first1 = static_cast<int>(f);
  int last2 = static_cast<int>(l);

  return first1 + last2;
}

不太确定如何制作它所以它添加了 char 输入之间的所有值

最佳答案

您正在寻找的是 A-Z 范围内的字符(虽然这可以断言,因为它被定义为输入)- 但不是 AEIOU

如果您不知道我所说的“断言”是什么意思 - 那么请查阅“c++ 断言”。

之后,一个 for 循环从头到尾检查每个步骤是否(不是)这些字符之一应该完成这项工作。

关于c++ - 如何在两个字符之间添加所有 Ascii 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39822059/

相关文章:

C++ - 在运行时从基类指针识别派生类

c++ - operator new 已经有一个主体

c++ - 如何使用C++根据当前时间自动生成新的CSV文件

c++ - 在 Visual C++ 中是否有可视化缓存的工具?

c++ - 防止 Windows 应用程序窃取焦点

c++ - 无法解决,因为它需要 *.dll

c++ - OStringStream 和命名空间问题 C++

c++ - Catch2:检查结果是否为 0.0 且相对错误失败

c++ - 如何在嵌入式 V8 中重置全局对象?

c++ - 关于虚函数,错误