Sunday, August 2, 2009

Helping with C++ program to compare the value of two letters. please!?

The Question: What does the following statement print?(All variable are of type int.)





- this is in Visual C++ as well





My code so far:





#include %26lt;iostream%26gt;


using namespace std;





int main ()





{


int j;


int k;





if(j %26lt; k)





if (k %26lt; j)


cout %26lt;%26lt;1;


else


cout %26lt;%26lt;2;





else


if (j %26lt; k)


cout %26lt;%26lt; 3;


else


cout %26lt;%26lt;4;





return 0;


}





It keeps returning to me initialized local variables for j and k. When I use char j and char k, it works fine, but I have to compare them as int. It should print 2 if j %26lt; k and 4 otherwise...





Any help please? I have searched around and can't find anything.





Thank you!








In C++ each letter has a numerical value that goes with it, ex j = 56 and k = 57. So my program needs to compare the letters as integers(to compare there numerical value to see which is larger). but I am not supposed to set there values, and I am not supposed to use type char. Does anyone have any idea?





Thanks in advance.

Helping with C++ program to compare the value of two letters. please!?
The cin fstream object only accepts a character array, so cast them as characters frist. Once they're read from the stream, cast them as numerical then compare.





if ((float)j %26lt; (float)k)


{


cout%26lt;%26lt;3;


}


else


{


cout%26lt;%26lt;4;


}

flamingo plant

No comments:

Post a Comment