Problem- strcat with char and char indexed from char array

A

aldonnelley

Hi there.

I'm just learning c++, and this is driving me nuts.

I'm trying to save image files generated in a for loop with a filename
built using strcat with:
- a char base file name
+ a character to identify the individual files that is indexed from a
char array by the int index of the for loop
+ a char file extension.

I keep getting a "cannot convert 'int' to 'const char'" error on the
line where I'm trying to strcat the character I've indexed from the
char array to the base file name. (script attached)

Can anyone tell me what is happening here, and how to fix it?

Cheers, Al.

//code starts

for(i=0;i<(faces ? faces->total:0); i++ )
{
const char arr[25] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' };
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

pt1.x = ( r->x + 0.08*(r->width));
pt2.x = ( r->x + 0.92*(r->width));
pt1.y = (r->y );
CvRect reg;
reg.x = pt1.x;
reg.y = pt1.y;
reg.width = abs( pt2.x - pt1.x);
reg.height = (r->height);
image->roi = NULL;
cvSetImageROI (image, reg);
region = cvCreateImage(cvSize (reg.width, reg.height), image->depth,
image->nChannels);
cvCopy (image, region, 0);

char fileindex = arr;
fileindex = ( const char ) fileindex;
char fileName[50] = "output";
char str[60];
strcpy (str, fileName);
strcat (str, fileindex); //compiler doesn't like this at
all...
strcat (str, ".jpg");
puts(str);
cout << str << endl;
cvSaveImage( str, region);
cvResetImageROI(image);
cvRectangle(image, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);

//code ends
 
P

Peter Jansson

Hi there.

I'm just learning c++, and this is driving me nuts.

I'm trying to save image files generated in a for loop with a filename
built using strcat with:
- a char base file name
+ a character to identify the individual files that is indexed from a
char array by the int index of the for loop
+ a char file extension.

I keep getting a "cannot convert 'int' to 'const char'" error on the
line where I'm trying to strcat the character I've indexed from the
char array to the base file name. (script attached)

Can anyone tell me what is happening here, and how to fix it?

Cheers, Al.

//code starts

for(i=0;i<(faces ? faces->total:0); i++ )
{
const char arr[25] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' };
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

pt1.x = ( r->x + 0.08*(r->width));
pt2.x = ( r->x + 0.92*(r->width));
pt1.y = (r->y );
CvRect reg;
reg.x = pt1.x;
reg.y = pt1.y;
reg.width = abs( pt2.x - pt1.x);
reg.height = (r->height);
image->roi = NULL;
cvSetImageROI (image, reg);
region = cvCreateImage(cvSize (reg.width, reg.height), image->depth,
image->nChannels);
cvCopy (image, region, 0);

char fileindex = arr;
fileindex = ( const char ) fileindex;
char fileName[50] = "output";
char str[60];
strcpy (str, fileName);
strcat (str, fileindex); //compiler doesn't like this at
all...
strcat (str, ".jpg");
puts(str);
cout << str << endl;
cvSaveImage( str, region);
cvResetImageROI(image);
cvRectangle(image, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);

//code ends


Hello,

A suggestion: Change the prolematic code into something like the following;

#include <sstream>
#include <iostreama>
//.... Your beginning code here...
std::eek:stringstream o;
o<<"output"<<arr<<".jpg";
std::cout<<o.str()<<std::endl;
cvSaveImage(o.str().c_str(),region);
// .... Your ending code here...


Regards,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
P

Peter Jansson

Hi there.

I'm just learning c++, and this is driving me nuts.

I'm trying to save image files generated in a for loop with a filename
built using strcat with:
- a char base file name
+ a character to identify the individual files that is indexed from a
char array by the int index of the for loop
+ a char file extension.

I keep getting a "cannot convert 'int' to 'const char'" error on the
line where I'm trying to strcat the character I've indexed from the
char array to the base file name. (script attached)

Can anyone tell me what is happening here, and how to fix it?

Cheers, Al.

//code starts

for(i=0;i<(faces ? faces->total:0); i++ )
{
const char arr[25] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' };
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

pt1.x = ( r->x + 0.08*(r->width));
pt2.x = ( r->x + 0.92*(r->width));
pt1.y = (r->y );
CvRect reg;
reg.x = pt1.x;
reg.y = pt1.y;
reg.width = abs( pt2.x - pt1.x);
reg.height = (r->height);
image->roi = NULL;
cvSetImageROI (image, reg);
region = cvCreateImage(cvSize (reg.width, reg.height), image->depth,
image->nChannels);
cvCopy (image, region, 0);

char fileindex = arr;
fileindex = ( const char ) fileindex;
char fileName[50] = "output";
char str[60];
strcpy (str, fileName);
strcat (str, fileindex); //compiler doesn't like this at
all...
strcat (str, ".jpg");
puts(str);
cout << str << endl;
cvSaveImage( str, region);
cvResetImageROI(image);
cvRectangle(image, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);

//code ends



Hello again,

Just a follow-up; strcat expects two char* arguments. One of the arguments
can't be just char.

Regards,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top