C++ string functions

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]’
closed account (E0p9LyTq)
You are creating C-style char strings, not C++ std::strings.

Here's the first part of your code written to work with C++ std::string:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
   std::string str = "Awesome";
   std::cout << "The length() function returns ";
   std::cout << "the length of a string.\n";
   std::cout << str << ": " << str.length() << "\n";
}


PLEASE learn to use code tags, it makes reading your code easier.
http://www.cplusplus.com/articles/jEywvCM9/

Hint: you can edit your post and add code tags.
Thank you. I was also wondering if I need to compile it differently than g++ filename for string functions?
Also would you include only iostream and string libraries or other ones?
closed account (E0p9LyTq)
brooklucy wrote:
Also would you include only iostream and string libraries or other ones?


It is not wrong to include other, unneeded headers, they are optimized away.

Including unneeded headers is IMO sloppy, increasing compile time unnecessarily.
I'm still having problems with the operators in my copy and concatenate functions. Im not sure how to structure these.
closed account (E0p9LyTq)
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.

std::string assignment:
http://www.cplusplus.com/reference/string/basic_string/operator=/

std::string concatenation:
http://www.cplusplus.com/reference/string/basic_string/operator+/
http://www.cplusplus.com/reference/string/basic_string/operator+=/

There are examples at the links.

A good tutorial on C++ string classes:
http://www.learncpp.com/cpp-tutorial/17-1-stdstring-and-stdwstring/
Last edited on
Topic archived. No new replies allowed.