Tuesday, July 28, 2009

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?

A question about "if" function on C++?
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
Reply:use "isalpha" - checks if the parameter is a member of the alphabet.





if(isalpha(w))
Reply: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
Reply: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

garland flower

No comments:

Post a Comment