Conditional, Relational, or Boolean Operators

Hey! I'm a beginner doing a Lab for class. I'm working with ASCII values to get certain outputs. But when I run the below code, It always ends up outputting the first if statement. Even if my input doesn't agree with the statement. Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;

int main()
{
char charInput3;
	cout << "Enter any character among upper case, lower case, or numbers between 0 - 9: ";
	cin >> charInput3;
	cout << "You entered: " << charInput3 << endl;

	if ((49 <= int(charInput3)) || (int(charInput3) <= 57)){
		cout << "You entered the number " << charInput3 << "! \n" << endl;
	}
	else if ((65 <= int(charInput3)) || (int(charInput3) <= 90)){
		cout << "You entered the Upper Case letter: " << charInput3 << "!" << endl;
	}
	else if ((97 <= int(charInput3)) || (int(charInput3) <= 122)){
		cout << "You entered the lower case letter: " << charInput3 << "!" << endl;
	}
Last edited on
In all your, if statements you useless then. I swapped the ones on the left to greater than so it looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;

int main()
{
	char charInput3;
	cout << "Enter any character among upper case, lower case, or numbers between 0 - 9: ";
	cin >> charInput3;
	cout << "You entered: " << charInput3 << endl;

	if ((49 >= int(charInput3)) || (int(charInput3) <= 57)) {
		cout << "You entered the number " << charInput3 << "! \n" << endl;
	}
	else if ((65 >= int(charInput3)) || (int(charInput3) <= 90)) {
		cout << "You entered the Upper Case letter: " << charInput3 << "!" << endl;
	}
	else if ((97 >= int(charInput3)) || (int(charInput3) <= 122)) {
		cout << "You entered the lower case letter: " << charInput3 << "!" << endl;
	}
	return 0;
}


and it now works for me.
Hrrrm. That didn't seem to do it for me. And wouldn't that be a logical error given that I want the values between the two numbers?
closed account (49iURXSz)
Please look at this line:

if ((49 <= int(charInput3)) || (int(charInput3) <= 57)){

Is 49 less than or equal to charInput3 cast as an int most of the time?
Why are you using plain integer values? (49, 65, 97), etc.
For example, would it be more readable if you wrote this instead :
1
2
3
4
5
6
7
8
9
if (('0' >= int(charInput3)) && (int(charInput3) <= '9')) {
		cout << "You entered the number " << charInput3 << "! \n" << endl;
}
else if (('A' >= int(charInput3)) && (int(charInput3) <= 'Z')) {
		cout << "You entered the Upper Case letter: " << charInput3 << "!" << endl;
}
else if (('a' >= int(charInput3)) && (int(charInput3) <= 'z')) {
	cout << "You entered the lower case letter: " << charInput3 << "!" << endl;
}

closed account (49iURXSz)
For what the scope of this program as written does (tells you whether you've entered a number, upper case, or lower case character), I like SakurasouBuster's solution.
@StudentOfJack I'm not sure I understand your question.

@SakurasouBusters Wouldn't the input then be misinterpreted, given that int(charInput3)) will be an integer?

I appreciate your help!


If it helps, this is the entirety of what I'm doing:

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
49
50
51
52
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;

int main()
{
	int numInput, addNum;
	char charInput = 'A';
	clock_t t1;
	clock_t t2;

	cout << charInput << " is " << (int)charInput << " in ASCII value.\n" << endl;

	cout << "Enter any integer between 0 to 255: ";
	cin >> numInput;
	cout << "ASCII value of " << numInput << " is " << (char)numInput << endl;

	cout << "Enter any integer to increment current ASCII value: ";
	t1 = clock();
	cin >> addNum;

	t1 = clock() - t1;
	printf("\nEntering a number took %d ticks (%f seconds),\n\n", t1, ((float)t1) / CLOCKS_PER_SEC);

	cout << "An updated ASCII value " << "is" << numInput + addNum << ": " << (char)(numInput + addNum) << endl;

	int addNum2 = 32;
	char charInput2;
	cout << "\n Enter a character in upper case: ";
	cin >> charInput2;
	cout << "\n" << charInput2 << " is " << (int)charInput2 << " in ASCII value. \n" << endl;
	cout << char((int)charInput2 + addNum2) << " is " << int(charInput2 + addNum2) << " in ASCII value. \n" << endl;

	char charInput3;
	cout << "Enter any character among upper case, lower case, or numbers between 0 - 9: ";
	cin >> charInput3;
	cout << "You entered: " << charInput3 << endl;

	if ((int(charInput3)) >= 49 || (int(charInput3) <= 57)){
		cout << "You entered the number " << charInput3 << "! \n" << endl;
	}
	else if ((int(charInput3)) >= 65 || (int(charInput3) <= 90)){
		cout << "You entered the Upper Case letter: " << charInput3 << "!" << endl;
	}
	else if ((int(charInput3)) >= 97 || (int(charInput3) <= 122)){
		cout << "You entered the lower case letter: " << charInput3 << "!" << endl;
	}

	return 0;
}
int(charInput3)) changes the variable type of charInput3 but actually does not change its original value.

Why are you converting them to int(s) anyways?

Personally, I like this code more :
1
2
3
4
5
6
7
8
9
if (charInput3 >= '0' && charInput3 <= '9') {
		cout << "You entered the number " << charInput3 << "! \n" << endl;
}
else if (charInput3 >= 'A' && charInput3 <= 'Z') {
		cout << "You entered the Upper Case letter: " << charInput3 << "!" << endl;
}
else if (charInput3 >= 'a' && charInput3 <= 'z') {
	cout << "You entered the lower case letter: " << charInput3 << "!" << endl;
}
I see what you mean. That makes sense. But what I have below works now, so I might keep It like that. I really appreciate your help!


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
49
50
51
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;

int main()
{
	int numInput, addNum;
	char charInput = 'A';
	clock_t t1;
	clock_t t2;

	cout << charInput << " is " << (int)charInput << " in ASCII value.\n" << endl;

	cout << "Enter any integer between 0 to 255: ";
	cin >> numInput;
	cout << "ASCII value of " << numInput << " is " << (char)numInput << endl;

	cout << "Enter any integer to increment current ASCII value: ";
	t1 = clock();
	cin >> addNum;

	t1 = clock() - t1;
	printf("\nEntering a number took %d ticks (%f seconds),\n\n", t1, ((float)t1) / CLOCKS_PER_SEC);

	cout << "An updated ASCII value is "  << numInput + addNum << ": " << (char)(numInput + addNum) << endl;

	int addNum2 = 32;
	char charInput2;
	cout << "\n Enter a character in upper case: ";
	cin >> charInput2;
	cout << "\n" << charInput2 << " is " << (int)charInput2 << " in ASCII value. \n" << endl;
	cout << char((int)charInput2 + addNum2) << " is " << int(charInput2 + addNum2) << " in ASCII value. \n" << endl;

	char charInput3;
	cout << "Enter any character among upper case, lower case, or numbers between 0 - 9: ";
	cin >> charInput3;
	cout << "You entered: " << charInput3 << endl;

	if (('0' <= int(charInput3)) && (int(charInput3) <= '9')) {
		cout << "You entered the number " << charInput3 << "! \n" << endl;
	}
	else if (('A' <= int(charInput3)) && (int(charInput3) <= 'Z')) {
		cout << "You entered the Upper Case letter: " << charInput3 << "!" << endl;
	}
	else if (('a' <= int(charInput3)) && (int(charInput3) <= 'z')) {
		cout << "You entered the lower case letter: " << charInput3 << "!" << endl;
	}

	return 0;
}
Last edited on
Topic archived. No new replies allowed.