Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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?

5 Answers

Relevance
  • 1 decade ago
    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

  • 5 years ago

    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)

  • 1 decade ago

    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

  • 1 decade ago

    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

  • How do you think about the answers? You can sign in to vote the answer.
  • 1 decade ago

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

    if(isalpha(w))

Still have questions? Get your answers by asking now.