468,170 Members | 2,064 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,170 developers. It's quick & easy.

2 basic questions about types and strings

Hello,

first of all I have a question about types:
When I have a method like this...

TYPE getStringValue() {
string str = "test";
return str;
}

....TYPE can't be "string" because "string" is no basic type.
I made some workaround with creatng a class Stringtype and using this
class as return type while Stringtyp contains the real string;
How is this to solve in the right way?
My second question is about string itself:

In a string I want to add the new line mark of the current operating
system to a string.

string str += "\n"; would work, but how is this to solve with std::endl?

Thanks,
Markus
Aug 8 '07 #1
8 1062
On Aug 8, 9:53 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Hello,

first of all I have a question about types:
When I have a method like this...

TYPE getStringValue() {
string str = "test";
return str;

}

...TYPE can't be "string" because "string" is no basic type.
So?
#include <string>
std::string getStringValue()
{
std::string str = "test";
return str;
}
What is problem with this?
I made some workaround with creatng a class Stringtype and using this
class as return type while Stringtyp contains the real string;
How is this to solve in the right way?

My second question is about string itself:

In a string I want to add the new line mark of the current operating
system to a string.

string str += "\n"; would work, but how is this to solve with std::endl?
what exactly you want to solve with std::endl and why? std::endl is a
manipulator - which means it is used to control the formatting during
the output. To be precise, std::endl writes a newline and also flushes
the buffer.

Thanks,
Markus

Aug 8 '07 #2
Neelesh Bodas wrote:
...TYPE can't be "string" because "string" is no basic type.
>
So?
#include <string>
std::string getStringValue()
{
std::string str = "test";
return str;
}
What is problem with this?
Oh, I thought it works with "string" instead of "std::string" while
using "using namespace std".
A really stupid mistake of mine. Thanks, this works now.

what exactly you want to solve with std::endl and why?
Actually I want to have my string in the following structure:

X X X X
X X X X
X X X X

That's why I want to add new line characters in the end of every line.
Markus
Aug 8 '07 #3
Markus Pitha wrote:
Actually I want to have my string in the following structure:

X X X X
X X X X
X X X X

That's why I want to add new line characters in the end of every line.
And using "\n" for the newlines is wrong exactly why?
Aug 8 '07 #4
On 2007-08-08 15:20:44 -0400, Juha Nieminen <no****@thanks.invalidsaid:
Markus Pitha wrote:
>Actually I want to have my string in the following structure:

X X X X
X X X X
X X X X

That's why I want to add new line characters in the end of every line.

And using "\n" for the newlines is wrong exactly why?
Because it's a string. '\n' is the appropriate character.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 8 '07 #5
On Aug 8, 11:04 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
Neelesh Bodas wrote:

..TYPE can't be "string" because "string" is no basic type.
So?
#include <string>
std::string getStringValue()
{
std::string str = "test";
return str;
}
What is problem with this?

Oh, I thought it works with "string" instead of "std::string" while
using "using namespace std".
It *does* work with string instead of std::string while "using
namespace std;"

-N

Aug 9 '07 #6
Pete Becker wrote:
Because it's a string. '\n' is the appropriate character.
I don't think that's relevant in this case.
Aug 9 '07 #7
Juha Nieminen wrote:
And using "\n" for the newlines is wrong exactly why?
I thought due to this platform independent issue.
M.

Aug 9 '07 #8

Juha Nieminen <no****@thanks.invalidwrote in message...
Pete Becker wrote:
Because it's a string. '\n' is the appropriate character.

I don't think that's relevant in this case.
Don't know about the relevance. I do know

std::string line;
line.push_back( "\n" );

.....don't go on my compiler, and

line.push_back( '\n' );

..... does. <G>

--
Bob R
POVrookie
Aug 9 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steven O. | last post: by
44 posts views Thread by Truble | last post: by
41 posts views Thread by Psykarrd | last post: by
3 posts views Thread by p988 | last post: by
14 posts views Thread by luis | last post: by
4 posts views Thread by Peter Morris [Droopy eyes software] | last post: by
23 posts views Thread by TefJlives | last post: by
12 posts views Thread by Gilbert | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.