I have a quick question.

I use getline to read from a file and put the line in a string s

I call cout<<s
and the output is this:

"HELLO, PLEASE \n "ENTER" A NUMBER \n"

why are the \n ignored and treated as strings?

thanks

I have a quick question.

I use getline to read from a file and put the line in a string s

I call cout<<s
and the output is this:

"HELLO, PLEASE \n "ENTER" A NUMBER \n"

why are the \n ignored and treated as strings?

thanks

cout << endl;

cout << endl;

emm.... your point being???

emm.... your point being???

cout << "HELLO, PLEASE" << endl << "ENTER" A NUMBER" << endl;

>why are the \n ignored and treated as strings?
My guess would be the file contains literal "\n" instead of actual newlines. It's hard to say without more detail from you, like the contents of the file as well as the code you're using to read and print the string.

>cout << "HELLO, PLEASE" << endl << "ENTER" A NUMBER" << endl;
I'm going to go out on a limb and suggest that you didn't fully understand the question. But barring that, I'd recommend not using endl unless you actually want to flush the stream as well as print a newline. This is essentially what endl does:

ostream& endl ( ostream& out )
{
  out<<'\n';
  out.flush();
}

That flush is generally an unnecessary overhead because the default behavior is almost always sufficient for getting output flushed at the right time without explicit requests on your part.

cout << "HELLO, PLEASE" << endl << "ENTER" A NUMBER" << endl;

Well thats not actually what I want to do.. I want cout to do it for me:DD And it should since when i declare a string

string s="I am late \n today \n";
cout<< s

The output is :
I am late
today

So why is not doing this when I use getline?

>I'm going to go out on a limb and suggest that you didn't fully understand the question.

Yeah I'm being a bit of a stupid cow tonight >.<

One question to OP,

Is there a reason you have additional quote marks in?

"HELLO, PLEASE \n "ENTER" A NUMBER \n";
vs
"HELLO, PLEASE \n ENTER A NUMBER \n";

As second one would be valid string, top one wouldn't as a single string from my understanding anyway.

Lilly

My guess would be the file contains literal "\n" instead of actual newlines. It's hard to say without more detail from you, like the contents of the file as well as the code you're using to read and print the string.

Yeah actually the file contains literal \n it's something like this:

10 PRINT "HELLO, PLEASE \n ENTER A NUMBER \n"

its a line from a Basic language code, I am building a Basic Interpreter.

if I use this

while(!file.eof()){
	getline (file,s);
     cout << s << endl;
}

i get this output
10 PRINT "HELLO, PLEASE \n ENTER A NUMBER \n"

if I use this

string s="10 PRINT "HELLO, PLEASE \n ENTER A NUMBER \n\"";
cout<<s;

i get this output
10 PRINT "HELLO, PLEASE
ENTER A NUMBER
"
My questions is why??

>Yeah actually the file contains literal \n it's something like this:
>10 PRINT "HELLO, PLEASE \n ENTER A NUMBER \n"

Okay, then that's the problem. Assuming you're on a Windows system, newlines are represented as two adjacent characters: CR(13) and LF(10). When cin sees that combination and is in text mode, it converts the combo into a single character: '\n'. In your case the file contains the actual characters '\\'(92) and 'n'(110). cin doesn't recognize that combination as a newline and no conversion takes place.

If you want to translate "\\n" (the two characters that represent the escape character) into '\n' (the actual escape character value), you're on your own. The stream won't do it for you. Now if you change the file to look like this, the stream will do the conversion:

10 PRINT "HELLO, PLEASE
ENTER A NUMBER
"

This is the difference between a value and the textual representation of a value.

Thanks for your help /n :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.