strcat

Need help with a code that I have been working on all day. if someone could help me, would really appreciate it. The problem is that it keeps telling me that i is not defined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void strcat(char t[],const char s[])
{
        unsigned int i;
        for (i = 0; t[i]; i++); //strlen(t);
        strcpy(t+i, s);
}



int main()
{


        char PointerA[] = "Pass it to the mirror";
        char PointerB[] = "Hit it twice pass it back";

        strcat(PointerA,PointerB);
        cout << "=======================================" << endl;
        cout << "=======================================" << endl;
        cout << "Strings for the Pointer/Array are " << strcat(PointerA[i],PointerB[i]) << endl;
        cout << "=======================================" << endl;
        cout << "=======================================" << endl;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstring> // for std::strcpy

// note: use a different name; <cstring> may (is allowed to)
//       expose strcat as a member of the global namespace
void my_strcat( char dest[], const char srce[] )
{
    unsigned int i;
    for( i = 0; dest[i]; ++i ); //strlen(dest);
    std::strcpy( dest+i, srce );
}

void my_strcat2( char dest[], const char srce[] )
{
    while( *dest ) ++dest ; // get to the null character in dest
    // while( *dest++ == *srce++ ) ; // copy chars in srce till a null character is copied
    while( ( *dest++ = *srce++ ) ) ; // copy chars in srce till a null character is copied 
}

int main()
{
    // make sure that the destination array is large enough to cat to
    char dest[1024] = "Pass it to the mirror. ";

    const char srce[] = "Hit it twice pass it back.";

    std::cout << "before my_strcat: " << dest << '\n' ;

    my_strcat( dest, srce );
    std::cout << " after my_strcat: " << dest << '\n' ;

    const char srce2[] = " and so on and so forth..." ;
    // my_strcat( dest, srce2 );
    my_strcat2( dest, srce2 );
    std::cout << "after my_strcat2: " << dest << '\n' ;
}

http://coliru.stacked-crooked.com/a/925660ba418b1ec2
Last edited on
Shouldn't line 16 be an assignment? And line 32 should be calling my_strcat2.
Yes and Yes.

Thank you! I've corrected it now.
thanks guys u are life savers.
Topic archived. No new replies allowed.