Passing char array into a function

Oct 10, 2013 at 6:39pm
I have been assigned the following task in my second week of c++ in college and am having difficulty in getting it to compile. The compiler is stopping at line 27 but I don't no what the error is. I would really appreciate any assistance with this. It's taken me forever to get this far.

The task is as follows:

Write two functions with the following function prototypes:
int my string len(char str[])
void my string cat(char dest[], char src[], int dest size)
Both functions should take zero-terminated strings as input. The first function should return the length of the string to the calling function. The second function should take a source and a destination string and the total size of the array pointed to by dest. It should then concatenated the source string onto the end of the destination string, if and only if the destination string has the capacity to store both strings. If the destination string does not have the capacity it should not alter either, print and error, and return to the calling function. Demonstrate the use both the above functions by using them in a program in which you initialise two character arrays with two strings, print out their length, print out the combined length, and print out the combined string


And this is my code so far:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* A program to demonstrate string concatenation */

#include <iostream>
#include <string.h>
using namespace std;

int my_string_len(char str[]){ // function to calculate length of a chracter array
    int len = 0;
    int i = 0;
    while (str[i]!='\0'){
        len = len +1;
        i++;
    }
    return len;
}

int main(){

    char string1[20]; //declare 2 strings
    char string2[20];

    cout << "Please enter the first string: "; // request user to input 2 strings
    cin >> string1;
    cout << "\n Please enter the second string: ";
    cin >> string2;

    my_string_cat(string1, string2, concatLen);

    int strlen1= my_string_len(string1); //calculte lenth of each string
    int strlen2= my_string_len(string2);
    // calculate total length of two strings
    int concatLen = (my_string_len(string1)) + (my_string_len(string2));

}
 //join 2 strings if there is enough capacity in the destination array
void my_string_cat(char dest[], char src[], int dest size){

    if (my_string_length(dest)+my_string_length(src))<=dest size{
        char fullStr [concatLen];
        strcat(fullStr, dest)
        strcat(fullStr, " ");
        strcat(fullStr, src);

        cout << "String 1 length: " << strlen1 << endl;
        cout << "String 2 length: " << strlen2 << endl;
        cout << "Combined String: " << fullStr << endl;
    }
    else{
        cout << "String 1 length: " << strlen1 << endl; //if not enough capacity
        cout << "String 2 length: " << strlen1 << endl; //print this message
        cout << "ERROR: The strings can not be joined."
    }
    return 0;
}
Oct 10, 2013 at 6:43pm
concatLen is not defined here. You should move line 27 after line 32.

Also you trying to call nonexistent function my_string_length() on line 38
Oct 10, 2013 at 7:18pm
Hi MiiNiPaa! Thank you for your assistance. I applied your corrections to the code and now the compiler is stopping at line 32.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* A program to demonstrate string concatenation */

#include <iostream>
#include <string.h>
using namespace std;

int my_string_len(char str[]){ // function to calculate length of a chracter array
    int len = 0;
    int i = 0;
    while (str[i]!='\0'){
        len = len +1;
        i++;
    }
    return len;
}

int main(){

    char string1[20]; //declare 2 strings
    char string2[20];

    cout << "Please enter the first string: "; // request user to input 2 strings
    cin >> string1;
    cout << "\n Please enter the second string: ";
    cin >> string2;

    int strlen1= my_string_len(string1); //calculte lenth of each string
    int strlen2= my_string_len(string2);
    // calculate total length of two strings
    int concatLen = (my_string_len(string1)) + (my_string_len(string2));

    my_string_cat(string1, string2, concatLen);
}
 //join 2 strings if there is enough capacity in the destination array
void my_string_cat(char dest[], char src[], int dest size){

    if (my_string_len(dest)+my_string_len(src))<=dest size{
        char fullStr [concatLen];
        strcat(fullStr, dest)
        strcat(fullStr, " ");
        strcat(fullStr, src);

        cout << "String 1 length: " << strlen1 << endl;
        cout << "String 2 length: " << strlen2 << endl;
        cout << "Combined String: " << fullStr << endl;
    }
    else{
        cout << "String 1 length: " << strlen1 << endl; // if not enough capacity
        cout << "String 2 length: " << strlen1 << endl; //print this meassage
        cout << "ERROR: The strings can not be joined."
    }
    return 0;
}

Oct 10, 2013 at 7:22pm
1) post errors you are getting.
2) You cannot have a variable name with a space inside.
3) You should either declare or define your function before you use it.
Oct 10, 2013 at 8:21pm
1)
The errors I am getting are:

$g++ main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp:7:12: error: expected initializer before ':' token
int strlen1:
^
main.cpp: In function 'int main()':
main.cpp:35:46: error: 'my_string_cat' was not declared in this scope
my_string_cat(string1, string2, concatLen);
^
main.cpp: At global scope:
main.cpp:38:49: error: conflicting declaration 'int dest'
void my_string_cat(char dest[], char src[], int dest size){
^
main.cpp:38:30: error: 'dest' has a previous declaration as 'char* dest'
void my_string_cat(char dest[], char src[], int dest size){
^
main.cpp:38:54: error: expected ',' or '...' before 'size'
void my_string_cat(char dest[], char src[], int dest size){
^
main.cpp: In function 'void my_string_cat(char*, char*)':
main.cpp:40:48: error: expected primary-expression before '<=' token
if (my_string_len(dest)+my_string_len(src))<=dest size{
^
main.cpp:40:55: error: expected ';' before 'size'
if (my_string_len(dest)+my_string_len(src))<=dest size{
^
main.cpp:51:40: error: 'strlen1' was not declared in this scope
cout << "String 1 length: " << strlen1 << endl; // if not enough capacity

2)
The variable name error 'dest size' is from my word for word copy of the function name that we are asked to use in the assignment question:
void my string cat(char dest[], char src[], int dest size)

I wonder if this is a misprint in the wording of the assignment?

3)
The function:
int my_string_len(char str[])

is defined on line 7 and the function:
void my_string_cat(char dest[], char src[], int dest size

is declared at line 35.

I have integer varaiables, strlen1 and strlen2 declared as global variables so I am not sure what the problem is there also.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* A program to demonstrate string concatenation */

#include <iostream>
#include <string.h>
using namespace std;

int strlen1:
int strlen2;

int my_string_len(char str[]){ // function to calculate length of a chracter array
    int len = 0;
    int i = 0;
    while (str[i]!='\0'){
        len = len +1;
        i++;
    }
    return len;
}

int main(){

    char string1[20]; //declare 2 strings
    char string2[20];

    cout << "Please enter the first string: "; // request user to input 2 strings
    cin >> string1;
    cout << "\n Please enter the second string: ";
    cin >> string2;

    int strlen1= my_string_len(string1); //calculate length of each string
    int strlen2= my_string_len(string2);
    // calculate total length of two strings
    int concatLen =  strlen1 + strlen2;

    my_string_cat(string1, string2, concatLen);
}
 //join 2 strings if there is enough capacity in the destination array
void my_string_cat(char dest[], char src[], int dest size){

    if (my_string_len(dest)+my_string_len(src))<=dest size{
        char fullStr [concatLen];
        strcat(fullStr, dest)
        strcat(fullStr, " ");
        strcat(fullStr, src);

        cout << "String 1 length: " << strlen1 << endl;
        cout << "String 2 length: " << strlen2 << endl;
        cout << "Combined String: " << fullStr << endl;
    }
    else{
        cout << "String 1 length: " << strlen1 << endl; // if not enough capacity
        cout << "String 2 length: " << strlen2 << end2; //print this meassage
        cout << "ERROR: The strings can not be joined.";
    }
}

Oct 10, 2013 at 8:27pm
I wonder if this is a misprint in the wording of the assignment?
It is probably dest_size

is declared at line 35.
It is defined on line 38. You are using it on line 35
Quick run on how compiler works: You use function on line 35. Now delete everything after that line.
Now try to find function my_string_cat. Cannot? Compiler cannot too.
You have strle1 and strle2 declared as global and local. Remove global declarations: they are incorrect also.
Oct 10, 2013 at 9:54pm
That's been a great help. Thanks again MiiNiPaa. It compiles and runs now OK. I've learned a lot from your advice and made a lot of progress with my code also.

I just need to figure out this part of the assignment now:
It should then concatenated the source string onto the end of the destination string, if and only if the destination string has the capacity to store both strings. If the destination string does not have the capacity it should not alter either, print and error, and return to the calling function.


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* A program to demonstrate string concatenation */

#include <iostream>
#include <string.h>
using namespace std;


int my_string_len(char str[]){ // function to calculate length of a chracter array
    int len = 0;
    int i = 0;
    while (str[i]!='\0'){
        len = len +1;
        i++;
    }
    return len;
}


 //join 2 strings if there is enough capacity in the destination array
void my_string_cat(char dest[], char src[], int dest_size){

    int strlen1 = my_string_len(dest);
    int strlen2 = my_string_len(src);

    //if (strlen1+strlen2;)<=dest_size){
        char fullStr [dest_size];

        strcat(fullStr, dest);
        strcat(fullStr, src);
        cout << "String 1 length: " << strlen1 << endl;
        cout << "String 2 length: " << strlen2 << endl;
        cout << "Combined string length: " << dest_size << endl;
        cout << fullStr;
    //}
    //else{
      //  cout << "String 1 length: " << strlen1 << endl; // if not enough capacity
        //cout << "String 2 length: " << strlen2 << endl; //print this meassage
        //cout << "ERROR: The strings can not be joined.";
//    }
}

int main(){

    char string1[20]; //declare 2 strings
    char string2[20];

    cout << "Please enter the first string: "; // request user to input 2 strings
    cin >> string1;
    cout << "Please enter the second string: ";
    cin >> string2;

    int strlen1= my_string_len(string1); //calculate length of each string
    int strlen2= my_string_len(string2);
    // calculate total length of two strings
    int concatLen = strlen1+strlen2;
    my_string_cat(string1, string2, concatLen);
}
Oct 11, 2013 at 3:52am
You almost done. You just need to move commented out condition and else branch into main() abd place it around line 56
Oct 11, 2013 at 9:45pm
I have finally managed this far with my code. It accepts 2 different strings and checks to see if the they can be concatenated together and assigned to another fixed length character array, providing they don't exceed the capacity of that array. ?????
That is the best interpretation that I can make from the the assignment below.
But I will know for sure when it gets corrected next week. :)
Thanks again MiiNiPaa for helping me get this far.


Write two functions with the following function prototypes:
int my string len(char str[])
void my string cat(char dest[], char src[], int dest size)
Both functions should take zero-terminated strings as input. The first function should return the length of the string to the calling function. The second function should take a source and a destination string and the total size of the array pointed to by dest. It should then concatenated the source string onto the end of the destination string, if and only if the destination string has the capacity to store both strings. If the destination string does not have the capacity it should not alter either, print and error, and return to the calling function. Demonstrate the use both the above functions by using them in a program in which you initialise two character arrays with two strings, print out their length, print out the combined length, and print out the combined string.


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Program to input 2 strings and check to see if their combined length can be concatenated into a
destination array. And print results to screen and error message if they exceed the capacity of
the destination array
*/

#include <iostream>
#include <string.h>
using namespace std;

int my_string_len(char str[]){ // function to calculate length of a chracter array
    int len = 0;
    int i = 0;
    while (str[i]!='\0'){
        len = len +1;
        i++;
    }
    return len;
}

void my_string_cat(char dest[], char src[], int dest_size){

    int strlen1 = my_string_len(dest);
    int strlen2 = my_string_len(src);
    char fullStr [20];
    int fullSize = sizeof(fullStr);

    if (dest_size<=fullSize){  // check if two char arrays will fit into char array 'fullStr'
        strcat(fullStr, dest);
        strcat(fullStr, src);
        cout << "String 1 length: " << strlen1 << endl;
        cout << "String 2 length: " << strlen2 << endl;
        cout << "Combined string length: " << dest_size << endl;
        string sFullStr = fullStr;
        cout << fullStr;
    }
    else{     // if combined string lengthd exceed capacity of destination array 'fullStr'
        cout << "String 1 length: " << strlen1 << endl; // print out string lengths
        cout << "String 2 length: " << strlen2 << endl;
        cout << "ERROR: The strings can not be joined!" << endl; // print error message
        cout << "The combined length of the first and second string exceed the capacity " << endl;
        cout << "of the destination array which has a limit of only 20 characters." << endl;
    }
}
int main(){

    char string1[20]; //declare 2 char arrays
    char string2[20];
    cout << "Please enter the first string: "; // request user to input 2 strings
    cin >> string1;
    cout << "Please enter the second string: ";
    cin >> string2;

    int strlen1= my_string_len(string1); //calculate length of each string
    int strlen2= my_string_len(string2);
    // calculate total length of two strings
    int concatLen = strlen1+strlen2;
    //pass 2 x strings and their combined length to 'my_string_cat function'    
    my_string_cat(string1, string2, concatLen); 
}


Topic archived. No new replies allowed.