Oct 2, 2016 at 8:24pm UTC
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 Oct 3, 2016 at 12:45am UTC
Oct 2, 2016 at 10:48pm UTC
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?
Oct 3, 2016 at 12:14am UTC
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?
Oct 3, 2016 at 12:39am UTC
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.
Oct 3, 2016 at 12:44am UTC
@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!
Oct 3, 2016 at 1:02am UTC
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 Oct 3, 2016 at 1:04am UTC