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.";
}
}
|