Errors with an ordinals function ... :(  
Author Message
PeteJM01





PostPosted: Visual C++ Language, Errors with an ordinals function ... :( Top

I'm using an ordinals function for my current program, I found the code written in javascript so I decided to translate it into C++, but I have a whole lot of errors that are coming up now...

System::String^ getOrdinal(int number)
{
System::String^ ord = 'th';

if((number % 10) = 1 && (number % 100) != 11)
{
ord = 'st';
}
if((number % 10) = 2 && (number % 100) != 12)
{
ord = 'nd';
}
if((number % 10) = 3 && (number % 100) != 13)
{
ord = 'rd';
}
return ord;
}

The errors are:

  1. Everytime I have used the varaible 'ord' including the initialization I get an error saying that the computer can't convert string to int type. I think this is being caused by the int default return type rule. But that should not even be being used, because I have defined the return type as String anyway.
  2. on each of the if statments I get an error saying ' left operand must be 1-value' and this error is one I have no ideas about...

Please reply asap...




Visual C++14  
 
 
Brian Kramer





PostPosted: Visual C++ Language, Errors with an ordinals function ... :( Top

1. Literal strings need to be surrounded by double quotes, not single quotes.  The error is saying that the constant character, which is directly encoded as an integer, cannot be assigned to a string variable.
2. The equality operator is ==.  = is the assignment operator.  The error is saying that number % 10 cannot be an assigned a value (in particular, it is not the left-value, i.e. l-value, of an assignment expression).

 
 
PeteJM01





PostPosted: Visual C++ Language, Errors with an ordinals function ... :( Top

OMG, I can't believe I didn't notice that, the problem was the translation from javascript, because javascript uses the '' :|