我的任务是:
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/