Need to do what isalpha does, without using isalpha.

Basically, make a program that has to check whether the user-given input is an alphabetic character or not.
I also have to use a function to test the input, and have it return either true or false, then have an output depending on whether the function returned true or false.
This is what I have so far:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

using namespace std;

bool IsCharAlpha(char cAnswer);

int main()
{
    char cInput, cAnswer;

    cout << "Please enter a character: " << endl;
    cin >> cInput;
    cAnswer = cInput;
    IsCharAlpha(cAnswer);

    if (cAnswer = '1')
    {
        cout << "Your character is an alphabetic character!" << endl;
    }

    else if (cAnswer = '0')
    {
        cout << "That is not a valid character." << endl;
    }
}

bool IsCharAlpha(char cAnswer)
{
    char asciilower = 97;
    char asciilowerlower = 122;
    char asciiupper = 65;
    char asciiupperupper = 90;

    if ((cAnswer >= asciilower && cAnswer <= asciilowerlower))
    {
        return 1;
    }

    else if ((cAnswer >= asciilowerlower) && (cAnswer <= asciiupperupper))
    {
        return 1;
    }

    else
    {
        return 0;
    }
}


The problem is that no matter what the input is, it's always returning 'true' and the console is always displaying "Your character is an alphabetic character." Help, please!
Last edited on
1 is different than '1'. Same thing with 0 and '0'.
Last edited on
@ModShop: Thanks, I didn't know that! Regardless though, I still have the same problem.
= is assignment
== is comparison

You want the latter.
Line 16, you're testing whether or not cAnswer = 0 or 1. cAnswer is your users input, not the return from the function IsCharAlpha(). To have this work properly, you will need to assign the result of IsCharAlpha() to a variable, on line 14

 
bool result = IsCharAlpha(cAnswer);
Last edited on
Sigh, I completely overlooked these seemingly 'small' details. Thanks so much.
Also, I did have the comparison operator when I first did this, but then nothing showed up on my console after I ran it so I thought it would be different since I was using a bool function. In any case, everything's clear now.

Again, thank you!
Topic archived. No new replies allowed.