简体   繁体   中英

I'm having trouble with a bool function where the user enters two letters and it outputs which letter is highest on the alphabet

I'm new to learning functions, I'm basically doing an excercise for college where the user enters in two letters eg aj and then letter one is higher than letter two in the alphabet and vice versa. It has to be a bool function here is the question.

4. a. Write a prototype for a function IsAlphaHigher that can be passed a pair of characters and that will return a Boolean value indicating whether or not the first character is higher alphabetically. (Note 'a' is higher than 'z') b. Write a definition for the function IsAlphaHigher c. Write a test application that will use the function IsAlphaHigher.

Now Its easy if the user enters something like an ,or capital letter AN . Which ever letter is lowest on the ASCII table is higher on the alphabet. But what about when the user enters something like the letters uppercase R lowercase a for example,upper case R is 82 on the ASCII table and small a is 97.

I figured out that if any small case letter - any uppercase letter = greater than 32 then the upper case letter is higher on the alphabet.

The logic behind this is that a - A = 32 , b - B = 32 , q - Q = 32 etc. So anything above 32 the upper case letter is higher than the lower case, anything below 32, the lower case is higher than the uppercase letter.

I used a series of if statements to get around this however i'm getting an error and i'm not quite sure where its coming from or why so i'd really appreciate some help.

thanks.

#include <iostream>
using namespace std;
bool IsAlphaHigher(char letterOne, char letterTwo);

int main()
{
    char letter1, letter2;

    cout << "Enter two letters ";
    cin >> letter1 >> letter2;

    IsAlphaHigher(letter1, letter2);

    if (IsAlphaHigher(letter1,letter2) == 1)
        cout << "letter one is higher on the alphabet than letter two ";
    else
        cout << "letter two is higher on the alphabet than letter one ";
}


bool IsAlphaHigher(char letterOne, char letterTwo)
{

    bool status;

    if (letterOne < 90 && letterTwo < 90)
        {
            if (letterOne < letterTwo)
                status = true;
            else
                status = false;
        }

    if (letterOne > 90 && letterTwo < 90)
        {
            if (letterOne < letterTwo)
                status = true;
            else
                status = false;
        }

    if (letterOne < 90 && letterTwo > 90)
        {
            if (letterTwo - letterOne <= 32)
                status = true;
            else
                status = false;
        }

    if (letterOne > 90 && letterTwo < 90)
        {
            if (letterOne - letterTwo <= 32)
                status = true;
            else
                status = false;
        }
    return status;
}

When I build it, it says theres no errors but when I go into the command shell and type in two letters it gives me a "Run time check failure #3 T" I have no idea why.

This is my first boolean function ever so I imagine i'm doing something wrong, Any tips on where I'm going wrong would be greatly appreciated.

You can just convert the two letters into same cases first. Not sure though which case you want to return true.

bool IsAlphaHigher(char letterOne, char letterTwo)
{

    if (letterOne > 90) letterOne -= 32;
    if (letterTwo > 90) letterTwo -= 32;

    return letterOne > letterTwo;  // true if letterOne is higher.
}

Simply convert all letters to either uppercase or lowercase before making your comparison. If you need to preserve the casing, create temporary variables to make the comparison

#include <iostream>

using namespace std;

void alphabetical_order(char first, char second)
{
    //65 - 90 are the ascii numbers for upper case letters
    if(first >= 65 && first <= 90)
        first = tolower(first);

    if(second >= 65 && second <= 90)
        second = tolower(second);

    if(first > second)
        cout << first << "," << second << endl;
    else
        cout << second << "," << first << endl;
}

int main()
{
    alphabetical_order('A','Z');
}

Hope this helps you understand, play around with this code and see if it will help you out.

EDIT: You might want to add error checking with your program to make sure you are given a letter and not a symbol by the way. Check out http://www.asciitable.com/ to get the right numbers. (65 - 90 and 97 - 122)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM