functions, loops and decisions. please help?

my teacher wants us to do this 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.

Required:

Your function must allow for either argument to be larger. For instance, passing A, C should yield the same as passing C, A.
You must also insure that the arguments to this function are upper case characters. If either argument is not you should return a -1 from the function;
It is best to use the isupper function see below.
You are allowed to use one decision (or two conditional statements) before the for loop to set the order of the characters.
You are allowed to use one decision before the for loop to determine the case of the characters.
You are allowed to use one decision inside the for loop to determine if the character is a vowel or consonant.
The order of the arguments must be taken care of prior to the beginning of the loop.
What not to do

Using any of the following will drop your grade for this assignment by 70%

global variables
cin in sumConsonants function
cout in sumConsonants function
goto statements
Note:

A char really is nothing more than an integer value. It is a short int value in between 0 and 255. What is different is how it is treated. Since you want to add a char to an int you will want to cast it:

char c = 'A';

int x = static_cast<int>(c);

x now has the value of 65

heres the code i have so far but im stuck how do i make them add together like he wants? im lost please help me. its due tonight

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  Put the#include <iostream>
#include <stdio.h>
#include <ctype.h>


using namespace std;

int main()
{
	int i = 0;
	int x = 65;
	char str[] = "Test string.\n";
	char c;
	cout << "enter two upper case chars" << endl;
	cin >> x;
}

void sumconstants()
{
	char c = 'A';
	int x = static_cast<int>(c);
}



 code you need help with here.
how do i make them add together like he wants?


An example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
	char letter1{ 'A' };
	char letter2{ 'Z' };

	int valOfChar1{ static_cast<int>(letter1) };
	std::cout << "Value of letter1 is: " << valOfChar1 << std::endl;

	int valOfChar2{ static_cast<int>(letter2) };
	std::cout << "Value of letter2 is: " << valOfChar2 << std::endl;

	int sumOfChars{ valOfChar1 + valOfChar2 };
	std::cout << "Sum of " << letter1 << " + " << letter2 << " is " << sumOfChars << std::endl;

	return 0;
}


Output:
Value of letter1 is: 65
Value of letter2 is: 90
Sum of A + Z is 155
You may want to start by following the instructions:

my teacher wants us to do this Write a function called sumConsonants that will take as arguments two upper case letters.


Where are the two arguments to the function?

Your main function should pass two values to the function


Where are you getting two characters from the user in main()?

Topic archived. No new replies allowed.