Occlet Occlet - 13 days ago 7
C Question

How to pass an element of an array to an other?

I am trying to take a new plate for a car and change the old one. I need the three letters to be capitals. The thing is that I cannot pass the "capital" element of newPlate array to the new_Plate array. The program compiles but the answer is soometimes %^&#%@$ and some other times nothing. I know I have a problem in the pointers.

void Car::set(char *newBrand, char *newPlate)
{
char new_Brand[80];
char new_Plate[8];

if(strlen(newPlate)==8)
{
for(int i=0;i<3;i++)
{
if(65<=(int)newPlate[i]<=90)
{
new_Plate[i]=newPlate[i]; // probably the problem
}

if(97<=(int)newPlate[i]<=122)
{
new_Plate[i]=(newPlate[i]+32); // probably the problem
}

cout<<new_Plate;
}
}
}

Answer

Your new_Plate string does not include a zero terminator. Furthermore, 65<=(int)newPlate[i]<=90) is not valid in C++. You should write something like

'A'<=newPlate[i] && newPlate[i]<='Z')