Click to See Complete Forum and Search --> : help needed with C++ code
sadia
11-15-2003, 09:02 AM
tell me how 2 use strcat operator in a loop
i'm trying 2 do dis but it isn't working
for(i=4; i<7; i++) length = strcat(length, cbuf[i])
plz help me out
Khalid Ali
11-15-2003, 10:56 AM
your question seems to be confusing.
Here is what I see..
strcat is a concatenation function,and the paramter that are required for it are
StrCat(char sourceChar,char DestinationChar);
where as in your code it looks like you are passing length and the char
I think you want to use a substring function which in C looks like this
string.substr(int startingPoint,int numberOFCharsTobStripped)
I hope this helps
Jeff Mott
11-15-2003, 01:13 PM
:confused:
All we see is a loop performing a concatentation. How did you suddenly decide that they need to get a substring rather than concatenate?
char *strcat(s, ct) concatenate string ct to the end of string s; return s.
Given that, sadia, your code appears to be fine. If it is not doing what you expected then can you tell us what it is doing? Or if it is returning an error then what is the error?
sadia
11-16-2003, 08:47 AM
dis code is not working
i get an error:type mismatch in assignment
n a warning:passing argument 2 of strcat makes pointer from integer without a cast
n if i do it the other way like u said
char *strcat(SrcAddr, cbuff[i]);
den i get dis error:parse error before '[' token
here is what i want 2 do
add cbuf[0] 2 length , add cbuf[1] 2 length ,.............. add cbuf[i] 2 length
if u can help me
thanx 4 ur help anyways
Khalid Ali
11-16-2003, 10:18 AM
I am not sure if this is what you are trying to do,but here it is.
the program below creates an array with three names,then it displays the names on screen,after that it adds the number of chars in each name to a length varaible and displays the total length and displays that length,
then it concatenates all three names with each other creating one long string of names
I hope this points you in right direction
#include<iostream.h>
#include<conio.h>
int main(){
char names[3][10]={"Khalid","Amtul","Sadia"};
for(int x=0;x<3;x++){
cout<<"Index ["<<x<<"] - Value = "<<names[x]<<"\n";
}
//get length of each name string
//and add it to a toal length;
int length=0;
for(int x=0;x<3;x++){
int len = strlen(names[x]);
cout<<"Name = "<<names[x]<<" length = "<<len<<"\n";
length+=len;
}
cout<<"Total characters in all names = "<<length<<"\n";
//now concatenate all 3 names
char longName[16];
for(int x=0;x<3;x++){
strcat(longName, names[x]);
}
cout<<"Long names string after concatenation = ["<<longName<<"]\n";
getch();
return 0;
}
Jeff Mott
11-16-2003, 11:56 AM
i get an error:type mismatch in assignmentMake sure that length and each element of cbuf are pointers to character arrays. e.g., cbuf[4] must point to an array of characters, not just a single character.
sadia
11-17-2003, 08:36 AM
well dats what i am doing (the last for loop)
but its not working, did i tell u dat i'm working in Linux
Khalid Ali
11-18-2003, 06:00 AM
too bad...I don't have a linux Box to validate the code.
I used boreland C++ Builder,and it should typically work on linux as well.
sadia
11-19-2003, 09:36 AM
thanx a lot 4 ur help
dis code is working v well now
but dere is a problem, i am having data in only one dimensional array , n u used 2 dimensional .........
"char names[3][10]={"Khalid","Amtul","Sadia"};"
y did u used it , is dere any way dat i can use 1 dimensional array here