-
type casting
ref: type casting
i want to write a program to enter a integer eg 1200, find its reverse and the subtract the orignal value from the reverse ie 1200-0012.
i try to enter the code and give me and error message. Can any one please help me to solve the problem:
int subtract(int year)
//find the reverse of the year, the difference between the original year &
//reverse year eg 1999-9991
{
int len;//string length
int i;
int diff;//absolute difference
int rYear;//reverse year
string sYear;//turn year into string
//change year into string and find its reverse order
sYear = year;
len = sYear.strlen();
i = sYear.strlen()-1;//set i = length of string-1
while (i>=0)
{
sYear.at(i);
i--;
}//end loop
//type cast
rYear = int(sYear);
//change string back into integer
diff = fabs(year - rYear);
return diff;
}//end diff
-
Read this
How are you trying to reverse the string? What are you trying to achieve by calling "sYear.at(i);"?
Also, the string type does not know how to convert to and from int. You need to use istringstream to do that (search the board for many examples).
You might want to get your code to compile and execute before you start working on the logic.
fabs() is for floating point numbers - there is no reason to use it with ints.
gg