I need help fixing the errors in this program. It is just demonstrating the five C++ functions: length, = (string copy), + (concatenate), == (comparison, and find. I don't know for sure how to construct these as I am just learning it. Any advice will help, thank you.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char str[] = "Awesome";
cout << "The length() function returns ";
cout << "the length of a string." << endl;
cout << str << ":" << str.length() << endl;
char string1[] = "Blonde";
char string2[] = "Blond";
cout << "The = (or copy) function returns ";
cout << "the copy of a string." << endl;
cout << "String 2 was " << string2 << "now it is ";
cout << string1 "=" string2;
cout << "." << endl;
char str1[] = "Candy";
char str2[] = "Bar";
cout << "The + (concatenate) function returns ";
cout << "the addition of two or more strings." << endl;
char str3[] = str1 "+" str2;
cout << str3;
char st1[] = "ABC";
char st2[] = "abc";
cout << "The == (comparison) function returns ";
cout << "the comparison of strings." << endl;
if (st1 == st2)
cout << st1 << " is NOT EQUAL " << "to " << st2 << endl;
else
cout << "equal" << endl;
char string[] = "Prince Charming";
cout << "The find() function returns ";
cout << "the piece of information found in a string " << endl;
cout << "and creates a substring." << endl;
char *loc;
loc = string.find("arm");
cout << "Found: " << loc << endl;
return 0;
}
These are the errors I get when I compile:
C++string.cpp: In function ‘int main()’:
C++string.cpp:10: error: request for member ‘length’ in ‘str’, which is of non-c lass type ‘char [8]’
C++string.cpp:17: error: expected ‘;’ before string constant
C++string.cpp:24: error: initializer fails to determine size of ‘str3’
C++string.cpp:24: error: expected ‘,’ or ‘;’ before string constant
C++string.cpp:41: error: request for member ‘find’ in ‘string’, which is of non- class type ‘char [16]’
I'm still having problems with the operators in my copy and concatenate functions. Im not sure how to structure these.
Maybe you should spend some time reading documentation on the C++ string library. You seem to be thinking of C-style char strings, talking about functions.