I'm having trouble with "if else" statements

So I'm very new to C++ and I taking a class on intro to programming and this is one of my assignments and I'm having a problem with the last part of the code where i'm trying to be able to get the ASCII value for anything like all Upper case letters and Lower, and numbers 0 - 9. However my InputAnswer isn't grabing anything else but the specified variables in numberInput (so 0 and 9), UcapInputX (just A) but not LcapInputX. Does anyone have any idea how I could fix it? Also I don't really get if else statements. The more simple the better, and sorry if my code looks a little gross.

Cheers,

Edit: sorry first post don't really know what to do when posting, hopefully i did what you asked. also I cut it down to the one part which isn't working, everything else is working fine now thanks.

[Code]
<iostream>
<iomanip>
<time.h>
/* Idon't understand if else statements.*/


char numberInput = ('0', '9');
char UcapInputX = 'A';
char LcapInputX = (UcapInput +32);
char InputAnswer;

cout << "\n";
cout<<"1. Enter any character in Lower case, Upper case or numbers between 0 - 9 then we will show you its ASCII value: ";
cin>> InputAnswer;

if(InputAnswer == numberInput){
cout<< "The ASCII value of " << numberInput << " is equal to " << (int)numberInput << "." << endl;
}
else if(InputAnswer == UcapInputX){
cout<< "You entered " << UcapInputX << " which has a ASCII value of " << (int)UcapInputX << "." << endl;
cout<< " " << UcapInputX << "is part of the Upper case group. "<< endl;
}
else if(InputAnswer == LcapInputX){
cout<< "The ASCII value of " << LcapInputX << " is equal to " << (int)LcapInputX << "." << endl;
}




system("pause");
return 0;
}
[/code]
Last edited on
Please use (and edit your post) to put [code][/code] tags around your code (the <> format).

> if(UcapInput == 'A'|| 'B'|| 'C')
This does not compare UcapInput with three letters.

What you've written is this
if ( UcapInput == 'A' || 'B' != 0 || 'C' != 0 )
ok I added the format. But I don't get the thing you're asking about. and thanks for the input so far I greatly appreciate it.
Try editing it again.
You need to put [/code] at the end as well for it to format properly.
Use the "Preview" button to check you did it right.


And for obviousness, write
if ( UcapInput == 'A' || UcapInput == 'B' || UcapInput == 'C' )
ok I think I did it
oh no I was replying to salem c's comment on formating my post properly, I haven't got the code to work yet.
figured it out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        char nInput = '0';
	char Ucap = 'A';
	char Lcap = 'a';
	
	
	cout<< "Enter either a upper case letter, a lower case letter, or a number between 0-9: ";
	cin>> nInput, Ucap, Lcap;
	
	if(nInput){
		cout<<"You entered " << nInput << " which has a ASCII value of " << (int)nInput << ". " <<endl;
		cout<< nInput << " belongs in the Numbers Group."<<endl;
	}else if(Ucap){
		cout<<"You entered " << Ucap << " which has a ASCII value of " << (int)Ucap << ". " <<endl;
		cout<< Ucap << " belongs in the Upper Case Group."<<endl;
	}else if(Lcap){
		cout<<"You entered " << Lcap << " which has a ASCII value of " << (int)Lcap << ". " <<endl;
		cout<< Lcap << " belongs in the Lower Case Group."<<endl;
	}
> cin>> nInput, Ucap, Lcap;
I would suggest you print out those 3 variables to see what you typed in.

Your input doesn't do what you expect it to.

As with your if(UcapInput == 'A'|| 'B'|| 'C') thing, you can write a lot of things which are syntactically valid (they compile), but the meaning (what it actually does) is a lot different to what you expect.

Topic archived. No new replies allowed.