2 basic questions about types and strings

M

Markus Pitha

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
 
N

Neelesh Bodas

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.
 
M

Markus Pitha

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
 
J

Juha Nieminen

Markus said:
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?
 
N

Neelesh Bodas

Neelesh Bodas wrote:

..TYPE can't be "string" because "string" is no basic type.




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
 
B

BobR

Juha Nieminen said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top