Thursday, July 9, 2009

Need very simple help with c++ code?

I'm new to c++ and have to do an assignment... so part of my code goes like this:





---------------------


else if ((hitvalue %26lt;= "9") %26amp;%26amp; (hitvalue %26gt;= "2")) {


if (hitvalue == "2") score = 2;


if (hitvalue == "3") score = 3;


if (hitvalue == "4") score = 4;


if (hitvalue == "5") score = 5;


if (hitvalue == "6") score = 6;


if (hitvalue == "7") score = 7;


if (hitvalue == "8") score = 8;


if (hitvalue == "9") score = 9;


cout %26lt;%26lt; "Your card is " %26lt;%26lt; hitvalue %26lt;%26lt; ". Your score right





now is " %26lt;%26lt; score %26lt;%26lt; endl;


}


-------------------------





now what should happen when that code is compiled is that else if should only execute if the string hitvalue is more than/equal to 2 or less than/equal to 9... but what happens is that if the number is more than 19, the code still executes even though it shouldn't!





Do you guys know why this happens?





Remember that the hitvalue is a STRING, not an integer! The situation forces me to use a string because I also have to convert values for letters.





Please help

Need very simple help with c++ code?
Use casting to convert data types. For example,





if ((int)hitvalue == 4) score = 4;





(int)hitvalue converts the string to an integer.








Also, try using single-quotes when comparing a string value





if (hitvalue == '4') score = 4;





On some compilers it makes a difference
Reply:for(int x=2; x%26lt;10; x++)


{


if(x==(int)hitvalue)


cout %26lt;%26lt; "your card is" %26lt;%26lt; hitvalue %26lt;%26lt;". Your score right now is " %26lt;%26lt; score %26lt;%26lt; endl;


}
Reply:Without knowing how hitvalue is declared, I can't tell. Is it a classic char * or char xxx[ ]? Is it a CString? Some type named String?





It's generally not a good idea to use comparison operators with strings (the exception being when comparing for an empty string). Here, for instance, it looks like you really want to compare the first character of the string (hitvalue[0]) with the single-digit possibilities. What the compiled code actually does may be quite different, however. Does it compare the entire string? If so, a two-character string is ALWAYS %26gt; a single character string, no matter what the characters are.





Or, if hitvalue is a classical char* or char xx[ ], does it compare hitvalue's address with the single characters? Syntactically, that's what the statement would call for, although I'm surprised any modern compiler would let it get through without at least a warning.





Why not just use a switch statement for all possible values (I'm guessing the others are JQKA, right?) and eliminate that compound if altogether?
Reply:Are you sure that hitvalue has to be a string? You can not use operators like %26lt;,%26gt;,%26lt;=,%26gt;= with strings. I have only recently looked into C++, but I would try using type casts, such as:


int hitvalueint = (int)hitvalue;


Or, if that does not work, I would create/find online a custom function to convert string to numbers. Note that you would then have to replace every occurrence of hitvalue with hitvalueint and then change what you are comparing hitvalue to too a number.





Also, and I just noticed this, based on how your code looks, you could always try


score = hitvalue;


instead of


if (hitvalue == "2") score = 2;


if (hitvalue == "3") score = 3;


if (hitvalue == "4") score = 4;


if (hitvalue == "5") score = 5;


if (hitvalue == "6") score = 6;


if (hitvalue == "7") score = 7;


if (hitvalue == "8") score = 8;


if (hitvalue == "9") score = 9;
Reply:Try using the Switch Case structure instead of the nessted if statements.





switch (hitvalue){


case '1':


score=1;


break;


case'2':


score=2;


break;


default: //gets executed if none of the conditions were met


MsgBox.Show("Error, the value was not between 1-9");


break;


}//end switch
Reply:What is happening that you are comparing pointers not actual values.


To compare 'numbers', convert them using one of atoi, atol or atof functions.





Use atoi() function to convert hitvalue from string to integer.


so in the hitvalue test, change to this for it to work correctly.


else if (atoi(hitvalue) %26gt;= 2 %26amp;%26amp; atoi(hitvalue) %26lt;= 9)


{


score = atoi(hitvalue);


cout %26lt;%26lt; ..... %26lt;%26lt; endl;


}


To get a string from a number, you can use itoa()


itoa(value, string, radix)


radix = 10 for decimal, 16 for hexadecimal.


string is where the converted value goes.


or use sprintf(buffer, "%d", value)


Operates the same as printf but stores the result into char array called buffer in this case.


Buffer must be allocated via malloc/new or declared as an array of characters.


No comments:

Post a Comment