On Mon, Sep 22, 2008 at 8:59 AM, KBill <[EMAIL PROTECTED]> wrote:

> I haven't posted here in a while because I have been away from
> programing. Due to financial constraints, I am not able to attend
> school at this time. I appreciate everyone bearing with me trying to
> keep everything fresh. Anyway, I can't compile a simple program that
> outputs the length of a string with this code. It doesn't compile
> correctly.
>
> //hello.cpp
> #include <string>
> #include <iostream>
>
> using namespace std;
>
> int main()
> {
> char mystring = string str("Hello Universe!");

What is 'char mystring' supposed to be? It seems unnecessary (and at
any rate, you are only creating a single char, not a string of char).
string str("...") should be enough to create a new string object.
Unless you absolutely need it, stay away from old C style strings in
C++. Your life will be a lot less stressful.

At any rate, the above is an invalid statement.

> cout << "The size of the string " << mystring << "is: " << str.size()
> << endl;
> return 0;
> }
>
> However if I eliminate the variable "mystring" it will compile without
> a problem.

The compiler does not lie. :-) Eliminate mystring, you don't need it
in this program. Just use str.

> My questions are:
>
> 1)How can I compile the program correctly by declaring a variable
> "mystring = string str("Hello World!")"?

string str("Hello World");

> 2)How come this program doesn't compile as I have it written?

Because it's an invalid assignment.

-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
 If I were to divulge it, it would overturn the world."
 -- Jelaleddin Rumi

Reply via email to