A question about "if" function on C++?

I am trying to do a HW. It is about identify an input from the user, then apply the equation to what was inputed, then show the answer. i.e. assume that i ask for w,x,y,z from the user. If the user inputs 3, 4, 5, 5, my equation will be x+y+z, as 4+5+5 = 14. (ignore the 3 for now) but the equation will apply "if" w =3.
code:
if ( w = 3)
answer = x+y+z

ok, thats kool. However, what if the input for "w" is suppose to be a letter? let say if the first input is letter "S", then do the equation. and not a number? when i do that, it wont compile!
i am sure it will not be
if (w = "C") or if (w = C)
any idea plz? or am i not clear enuff?

hOsEiN m2007-04-13T05:03:16Z

Favorite Answer

You probably defined W as integer to get a number from the user. If you put w == c then c is not a number, and also if you put w== "c" it won't compile because w is integer and you are comparing it with a string ("c") so it gives you error.
Explain what you mean by using a letter. Do you mean if i enter 2,3,4 then you print this:
s*2 + 3 + 4
if you mean this, you have to define the first variable as string, like:
string w;
int x,y,z;
then user inputs x and y and z as integers, and w as string.
Explain more what you mean by w being a letter
cheers

?2016-09-05T16:08:14Z

As MD stated, && is the logical AND operator. But what are you rather seeking to do right here, and what's x? If x is a bool, then this may increasingly paintings: if(x && y > eight) if x is an int, and you're seeking to see if x and y are each > eight, then you wish to have this: if( x > eight && y > eight)

bhuvana2007-04-13T04:33:54Z

you have to use ascii values if u want the input as character and not a number then u should check the condition with ascii values

Ali Yavari2007-04-13T04:36:25Z

you should type

if(w==3);

you shoud use two "=="
in c++ one "=" means equation not condition.

if w is char you should dim it as char,
for example

{
char w;
if (w=='s')
w='a';
}


Cheers

Taran2007-04-13T04:52:32Z

use "isalpha" - checks if the parameter is a member of the alphabet.

if(isalpha(w))